Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b5dc9c

Browse files
committed
Cleanup in determining implicit content-type
1 parent 46a8e77 commit 3b5dc9c

2 files changed

Lines changed: 41 additions & 27 deletions

File tree

fetch.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,10 @@
128128
this._bodyInit = body
129129
if (typeof body === 'string') {
130130
this._bodyText = body
131-
return 'text/plain;charset=UTF-8'
132131
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
133132
this._bodyBlob = body
134-
if (body.type !== '') {
135-
return body.type
136-
}
137133
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
138134
this._bodyFormData = body
139-
// Since we don't know what the multipart/form-data boundary string
140-
// will be, we can't set a Content-Type here
141135
} else if (!body) {
142136
this._bodyText = ''
143137
} else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
@@ -146,6 +140,14 @@
146140
} else {
147141
throw new Error('unsupported BodyInit type')
148142
}
143+
144+
if (!this.headers.get('content-type')) {
145+
if (typeof body === 'string') {
146+
this.headers.set('content-type', 'text/plain;charset=UTF-8')
147+
} else if (this._bodyBlob && this._bodyBlob.type) {
148+
this.headers.set('content-type', this._bodyBlob.type)
149+
}
150+
}
149151
}
150152

151153
if (support.blob) {
@@ -213,7 +215,6 @@
213215
function Request(input, options) {
214216
options = options || {}
215217
var body = options.body
216-
var bodyFromOptions = true
217218
if (Request.prototype.isPrototypeOf(input)) {
218219
if (input.bodyUsed) {
219220
throw new TypeError('Already read')
@@ -228,7 +229,6 @@
228229
if (!body) {
229230
body = input._bodyInit
230231
input.bodyUsed = true
231-
bodyFromOptions = false
232232
}
233233
} else {
234234
this.url = input
@@ -245,10 +245,7 @@
245245
if ((this.method === 'GET' || this.method === 'HEAD') && body) {
246246
throw new TypeError('Body not allowed for GET or HEAD requests')
247247
}
248-
var contentType = this._initBody(body)
249-
if (bodyFromOptions && contentType && !this.headers.get('Content-Type')) {
250-
this.headers.set('Content-Type', contentType)
251-
}
248+
this._initBody(body)
252249
}
253250

254251
Request.prototype.clone = function() {
@@ -287,16 +284,13 @@
287284
options = {}
288285
}
289286

290-
this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers)
291-
var contentType = this._initBody(bodyInit)
292-
if (contentType && !this.headers.get('Content-Type')) {
293-
this.headers.set('Content-Type', contentType)
294-
}
295287
this.type = 'default'
296288
this.status = options.status
297289
this.ok = this.status >= 200 && this.status < 300
298290
this.statusText = options.statusText
291+
this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers)
299292
this.url = options.url || ''
293+
this._initBody(bodyInit)
300294
}
301295

302296
Body.call(Response.prototype)

test/test.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,15 @@ suite('Request', function() {
312312
body: 'I work out',
313313
headers: {
314314
accept: 'application/json',
315-
'Content-Type': 'text/plain'
315+
'X-Request-ID': '123'
316316
}
317317
})
318318
var request2 = new Request(request1, {
319319
headers: { 'x-test': '42' }
320320
})
321321

322322
assert.equal(request2.headers.get('accept'), undefined)
323-
assert.equal(request2.headers.get('content-type'), undefined)
323+
assert.equal(request2.headers.get('x-request-id'), undefined)
324324
assert.equal(request2.headers.get('x-test'), '42')
325325
})
326326

@@ -356,6 +356,18 @@ suite('Request', function() {
356356
})
357357
})
358358

359+
test('GET should not have implicit Content-Type', function() {
360+
var req = new Request('https://fetch.spec.whatwg.org/')
361+
assert.equal(req.headers.get('content-type'), undefined)
362+
})
363+
364+
test('POST with blank body should not have implicit Content-Type', function() {
365+
var req = new Request('https://fetch.spec.whatwg.org/', {
366+
method: 'post'
367+
})
368+
assert.equal(req.headers.get('content-type'), undefined)
369+
})
370+
359371
test('construct with string body sets Content-Type header', function() {
360372
var req = new Request('https://fetch.spec.whatwg.org/', {
361373
method: 'post',
@@ -365,25 +377,33 @@ suite('Request', function() {
365377
assert.equal(req.headers.get('content-type'), 'text/plain;charset=UTF-8')
366378
})
367379

368-
;(Request.prototype.blob ? test : test.skip)('construct with Blob body and type sets Content-Type header', function() {
380+
featureDependent(test, support.blob, 'construct with Blob body and type sets Content-Type header', function() {
369381
var req = new Request('https://fetch.spec.whatwg.org/', {
370382
method: 'post',
371-
body: new Blob(['test'], { type: 'text/plain' }),
383+
body: new Blob(['test'], { type: 'image/png' })
372384
})
373385

374-
assert.equal(req.headers.get('content-type'), 'text/plain')
386+
assert.equal(req.headers.get('content-type'), 'image/png')
375387
})
376388

377389
test('construct with body and explicit header uses header', function() {
378390
var req = new Request('https://fetch.spec.whatwg.org/', {
379391
method: 'post',
380-
headers: {
381-
'Content-Type': 'text/plain'
382-
},
392+
headers: { 'Content-Type': 'image/png' },
383393
body: 'I work out'
384394
})
385395

386-
assert.equal(req.headers.get('content-type'), 'text/plain')
396+
assert.equal(req.headers.get('content-type'), 'image/png')
397+
})
398+
399+
featureDependent(test, support.blob, 'construct with Blob body and explicit Content-Type header', function() {
400+
var req = new Request('https://fetch.spec.whatwg.org/', {
401+
method: 'post',
402+
headers: { 'Content-Type': 'image/png' },
403+
body: new Blob(['test'], { type: 'text/plain' })
404+
})
405+
406+
assert.equal(req.headers.get('content-type'), 'image/png')
387407
})
388408

389409
test('clone request', function() {
@@ -564,7 +584,7 @@ suite('Response', function() {
564584
assert.equal(r.headers.get('content-type'), 'text/plain;charset=UTF-8')
565585
})
566586

567-
;(Response.prototype.blob ? test : test.skip)('construct with Blob body and type sets Content-Type header', function() {
587+
featureDependent(test, support.blob, 'construct with Blob body and type sets Content-Type header', function() {
568588
var r = new Response(new Blob(['test'], { type: 'text/plain' }))
569589
assert.equal(r.headers.get('content-type'), 'text/plain')
570590
})

0 commit comments

Comments
 (0)