Mirror: A Node.js fetch shim using built-in Request, Response, and Headers (but without native fetch)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: Prevent overriding `Content-Type` when it's already set (#30)

authored by

Phil Pluckthun and committed by
GitHub
03e45576 448fdd62

+20 -2
+5
.changeset/nice-masks-buy.md
··· 1 + --- 2 + 'fetch-nodeshim': patch 3 + --- 4 + 5 + Fix `Content-Type` being overridden for string inputs when it's already set
+10
src/__tests__/fetch.test.ts
··· 122 122 }); 123 123 }); 124 124 125 + it('should send custom Content-Type with body', async () => { 126 + const response = await fetch(new URL('inspect', baseURL), { 127 + headers: { 'content-type': 'some/magic' }, 128 + body: 'test', 129 + }); 130 + expect(await response.json()).toMatchObject({ 131 + headers: expect.objectContaining({ 'content-type': 'some/magic' }), 132 + }); 133 + }); 134 + 125 135 it('should prefer init headers when Request is passed', async () => { 126 136 const request = new Request(new URL('inspect', baseURL), { 127 137 headers: { 'x-custom-header': 'abc' },
+5 -2
src/fetch.ts
··· 287 287 288 288 outgoing.on('error', destroy); 289 289 290 - if (!requestHeaders.has('Accept')) requestHeaders.set('Accept', '*/*'); 291 - if (requestBody.contentType) 290 + if (!requestHeaders.has('Accept')) { 291 + requestHeaders.set('Accept', '*/*'); 292 + } 293 + if (!requestHeaders.has('Content-Type') && requestBody.contentType) { 292 294 requestHeaders.set('Content-Type', requestBody.contentType); 295 + } 293 296 294 297 if ( 295 298 requestBody.body == null &&