How to test downloading a file to the server?

Hi!

Write a test to upload a file to the server. Here is the code:
describe('POST request', () => {
 describe('post file.ext', () => {
 it('returns 200 & the file', done => {
 let content = fs.readFileSync(`${fixtures}/small.png`, {encoding: 'utf-8'});

 var formData = {
 /*attachments: [
fs.createReadStream(`${fixtures}/small.png`)
],
*/
 /*my_file: fs.createReadStream(`${fixtures}/small.png`),
*/
 custom_file: {
 value: fs.createReadStream(`${fixtures}/small.png`),
 options: {
 filename: 'small.png',
 contentType: mime.lookup(`${fixtures}/small.png`)
}
}
};
 request.post({url: `${url}/small.png`, formData: formData}, (error, response, body) => {
 if (error) return done(error);
expect(response.statusCode).to.equal(200);

 var data = fs.readFileSync(`${filesDirectory}/small.png`, {encoding: 'utf-8'});
 if (!data) return done(error);

expect(data).to.deep.equal(content);
done();
});
});
});
 });


Test fails - the downloaded file is not the same as the original.
In the beginning of the received file is appended garbage like:
----------------------------628275255943632608493620
Content-Disposition: form-data; name="custom_file"; filename="small.png"
Content-Type: image/png

How to make a file at the server has not changed?
July 4th 19 at 23:52
1 answer
July 4th 19 at 23:54
Solution
It's not trash, it's part of the http Protocol
as a result the file download You have done wrong, if the subtitles into the file

UPD:
request.post({
 url: `${url}/small.png`,
 body: fs.createReadStream(`${fixtures}/small.png`),
 headers: {
 'Content-type': mime.lookup(`${fixtures}/small.png`),
 'x-filename': 'small.png'
}
}, (error, response, body) => { /* Your code */ });
approximately
Tell me how to fix it? - keith.Ullrich74 commented on July 4th 19 at 23:57
: show the server code that works with file upload, then it will be possible to say something - geovanni_Hegmann commented on July 5th 19 at 00:00
: something like this:
'use strict';

const fs = require('fs');

module.exports = (fileName, req, res) => {
 const writeStream = new fs.WriteStream(fileName, {flags: 'wx'});

req
 .on('data', chunk => {/* ... check file size */})
 .on('close', () => {/* ... if the client has fallen off */})
.pipe(writeStream);

writeStream
 .on('error', err => {/* ... error handling */})
 .on('close', () => {
Res. end("OK");
});
};
- keith.Ullrich74 commented on July 5th 19 at 00:03
: just to be clear, the server itself works fine - files from the client are accepted unchanged. But when testing neponyatka - keith.Ullrich74 commented on July 5th 19 at 00:06
You are on the server in the file is written in the request body as a whole, and in the test you send the file as multipart/formdata - geovanni_Hegmann commented on July 5th 19 at 00:09
: How to test the right to write tell me, pls. - keith.Ullrich74 commented on July 5th 19 at 00:12
: added response example - geovanni_Hegmann commented on July 5th 19 at 00:15
Hurray, everything works))
Another look at the documentation ( https://www.npmjs.com/package/request ) about the fact that you can specify body found nothing - keith.Ullrich74 commented on July 5th 19 at 00:18
Although, I lied, everything is there: https://www.npmjs.com/package/request#requestoptio...
Thanks again. - keith.Ullrich74 commented on July 5th 19 at 00:21

Find more questions by tags JavaScriptNode.js