Skip to content

Commit

Permalink
feat: strip explicit content-type header on multipart/form-data requests
Browse files Browse the repository at this point in the history
  • Loading branch information
eddienubes committed Aug 25, 2024
1 parent 40e694d commit 2dd462a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Sage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ export class Sage<T> {
/**
* Method is designed to work only with FormData requests.
* Cannot be combined with .send().
* If file is a string, it will be treated as a path to a file starting from the working directory (process.cwd()).
* If file is a relative path, it will be treated starting from the working directory (process.cwd()).
* When using form-data requests, you can skip setting the Content-Type header.
* It will be set automatically.
* @throws SageException if body is already set
* @param field
* @param file a Blob, Buffer, Readable stream or a file path either staring from the working directory, or an absolute path
Expand Down Expand Up @@ -281,6 +283,12 @@ export class Sage<T> {
return this;
}

/**
* When using form-data requests, you can skip setting the Content-Type header.
* It will be set automatically.
* @param field
* @param value
*/
field(field: string, value: string | string[] | number | boolean): this {
if (!this.request.formData) {
this.request.formData = new FormData();
Expand Down Expand Up @@ -333,6 +341,15 @@ export class Sage<T> {
this.request.path = `${this.config.baseUrl}${this.request.path}`;
}

// unidici will set the Content-Type header automatically for FormData
if (
this.request.formData &&
this.request.headers &&
'content-type' in this.request.headers
) {
delete this.request.headers['content-type'];
}

try {
const res = await this.client.request({
method: this.request.method as HttpMethod,
Expand Down
42 changes: 42 additions & 0 deletions test/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,27 @@ describe('request', () => {
});

describe('multipart/form-data', () => {
it('should strip content-type header', async () => {
const res = await request(expressApp)
.post('/upload')
.attach('picture', 'test/fixtures/cat.jpg')
.set('content-type', 'multipart/form-data; boundary=fail-boundary');

expect(res).toMatchObject({
...expectedExpressResponse,
body: {
...expectedExpressResponse.body,
reqHeaders: {
...expectedExpressResponse.body.reqHeaders,
'content-type': expect.stringContaining('multipart/form-data')
}
}
});
expect(res.body.reqHeaders['content-type']).not.toContain(
'fail-boundary'
);
});

it('should work with filename', async () => {
const res = await request(expressApp)
.post('/upload')
Expand Down Expand Up @@ -693,6 +714,27 @@ describe('request', () => {
});

describe('multipart/form-data', () => {
it('should strip content-type header', async () => {
const res = await request(fastifyApp.server)
.post('/upload')
.attach('picture', 'test/fixtures/cat.jpg')
.set('content-type', 'multipart/form-data; boundary=fail-boundary');

expect(res).toMatchObject({
...expectedFastifyResponse,
body: {
...expectedFastifyResponse.body,
reqHeaders: {
...expectedFastifyResponse.body.reqHeaders,
'content-type': expect.stringContaining('multipart/form-data')
}
}
});
expect(res.body.reqHeaders['content-type']).not.toContain(
'fail-boundary'
);
});

it('should work with filename', async () => {
const res = await request(fastifyApp.server)
.post('/upload')
Expand Down

0 comments on commit 2dd462a

Please sign in to comment.