diff --git a/packages/happy-dom/src/fetch/Request.ts b/packages/happy-dom/src/fetch/Request.ts index 9784f974f..cfe241ca3 100644 --- a/packages/happy-dom/src/fetch/Request.ts +++ b/packages/happy-dom/src/fetch/Request.ts @@ -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; diff --git a/packages/happy-dom/test/fetch/Request.test.ts b/packages/happy-dom/test/fetch/Request.test.ts index 46068f12e..fcceb42af 100644 --- a/packages/happy-dom/test/fetch/Request.test.ts +++ b/packages/happy-dom/test/fetch/Request.test.ts @@ -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');