Can I override Supertest headers after attach/field?

31 Views Asked by At

I've been trying to test an invalid multipart/form-data submission by messing up with the content headers. Altho, when I use attach or field Supertest computes and replaces my set headers, making the request a valid multipart request.

Example:

mockGateway.post(url)
           .attach('file', Buffer.from('hello world'), 'file.txt')
           .set({'content-type': 'multipart/form-data; boundary=----------0193874649'})
           .set({'content-length': 1})
           .end(function (err, response) {
             console.log(response); // <- returning a valid response
             assert.ifError(err);
             done();
           });

I tried replacing attach with field and changing the order (headers first), and got no results. Any ideas? Thank you in advance!

Edit: I eventually found the answer to my problem. Instead of using attach or field I'm using the form-data library, and using send directly on Supertest to submit the form. This I can fake the headers.


const form = new FormData();
form.append('field1', 'value1');

mockGateway.post(`/public/access_tokens`)
           .set({'content-type': 'multipart/form-data; boundary=----------0193874649'})
           // make the request invalid by setting an incorrect length
           .set({'content-length': 1})
           .send(form.getBuffer())
           .end(function (err, response) {
             assert.ifError(err);
             // assertions
             done();
           });
0

There are 0 best solutions below