Skip to content

Commit

Permalink
Merge pull request #651 from github/body-unsupported-type
Browse files Browse the repository at this point in the history
Match spec behavior re: unsupported body type
  • Loading branch information
mislav authored Sep 4, 2018
2 parents fde38d5 + 08bc742 commit 0ae0da3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function Body() {
} else if (support.arrayBuffer && (ArrayBuffer.prototype.isPrototypeOf(body) || isArrayBufferView(body))) {
this._bodyArrayBuffer = bufferClone(body)
} else {
throw new Error('unsupported BodyInit type')
this._bodyText = body = Object.prototype.toString.call(body)
}

if (!this.headers.get('content-type')) {
Expand Down
47 changes: 47 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,29 @@ exercise.forEach(function(exerciseMode) {
}
)

test('construct with unsupported body type', function() {
var req = new Request('https://fetch.spec.whatwg.org/', {
method: 'post',
body: {}
})

assert.equal(req.headers.get('content-type'), 'text/plain;charset=UTF-8')
return req.text().then(function(bodyText) {
assert.equal(bodyText, '[object Object]')
})
})

test('construct with null body', function() {
var req = new Request('https://fetch.spec.whatwg.org/', {
method: 'post'
})

assert.isNull(req.headers.get('content-type'))
return req.text().then(function(bodyText) {
assert.equal(bodyText, '')
})
})

test('clone GET request', function() {
var req = new Request('https://fetch.spec.whatwg.org/', {
headers: {'content-type': 'text/plain'}
Expand Down Expand Up @@ -647,6 +670,30 @@ exercise.forEach(function(exerciseMode) {

assert.equal(r.headers.get('content-type'), 'text/plain')
})

test('init object as first argument', function() {
var r = new Response({
status: 201,
headers: {
'Content-Type': 'text/html'
}
})

assert.equal(r.status, 200)
assert.equal(r.headers.get('content-type'), 'text/plain;charset=UTF-8')
return r.text().then(function(bodyText) {
assert.equal(bodyText, '[object Object]')
})
})

test('null as first argument', function() {
var r = new Response(null)

assert.isNull(r.headers.get('content-type'))
return r.text().then(function(bodyText) {
assert.equal(bodyText, '')
})
})
})

// https://fetch.spec.whatwg.org/#body-mixin
Expand Down

0 comments on commit 0ae0da3

Please sign in to comment.