Skip to content

Commit

Permalink
Merge pull request #1017 from orionmiz/task/1016-request-content-type…
Browse files Browse the repository at this point in the history
…-header

#1016@minor: Add Content-Type header for Request.
  • Loading branch information
capricorn86 committed Aug 15, 2023
2 parents 0b20252 + 04ae97b commit 015346d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/happy-dom/src/fetch/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export default class Request implements IRequest {
}

if (contentType) {
if (!this.headers.has('Content-Type')) {
this.headers.set('Content-Type', contentType);
}
this._contentType = contentType;
} else if (input instanceof Request && input._contentType) {
this._contentType = input._contentType;
Expand Down
10 changes: 10 additions & 0 deletions packages/happy-dom/test/fetch/Request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ describe('Request', () => {
expect(request._contentType).toBe('text/plain;charset=UTF-8');
});

it('Supports content type header from Request object.', () => {
const request = new Request(new Request(TEST_URL, { method: 'POST', body: 'Hello World' }));
expect(request.headers.get('Content-Type')).toBe('text/plain;charset=UTF-8');
});

it('Supports content type header from init object.', () => {
const request = new Request(TEST_URL, { method: 'POST', body: 'Hello World' });
expect(request.headers.get('Content-Type')).toBe('text/plain;charset=UTF-8');
});

it('Supports redirect from Request object.', () => {
const request = new Request(new Request(TEST_URL, { redirect: 'manual' }));
expect(request.redirect).toBe('manual');
Expand Down

0 comments on commit 015346d

Please sign in to comment.