Skip to content

Commit

Permalink
Merge pull request JakeChampion#227 from joshuawarner32/master
Browse files Browse the repository at this point in the history
Allow making a POST request with an ArrayBuffer body
  • Loading branch information
mislav committed Nov 2, 2015
2 parents 0cb730d + 1529c83 commit 4db9f7b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
return false
}
})(),
formData: 'FormData' in self
formData: 'FormData' in self,
arrayBuffer: 'ArrayBuffer' in self
}

function Body() {
Expand All @@ -133,6 +134,9 @@
this._bodyFormData = body
} else if (!body) {
this._bodyText = ''
} else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
// Only support ArrayBuffers for POST method.
// Receiving ArrayBuffers happens via Blobs, instead.
} else {
throw new Error('unsupported BodyInit type')
}
Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,27 @@ suite('Request', function() {
})
})

;(ArrayBuffer in self ? test : test.skip)('sends ArrayBuffer body', function() {
var text = 'name=Hubot'

var buf = new ArrayBuffer(text.length)
var view = new Uint8Array(buf)

for(var i = 0; i < text.length; i++) {
view[i] = text.charCodeAt(i)
}

return fetch('/request', {
method: 'post',
body: buf
}).then(function(response) {
return response.json()
}).then(function(request) {
assert.equal(request.method, 'POST')
assert.equal(request.data, 'name=Hubot')
})
})

test('construct with url', function() {
var request = new Request('https://fetch.spec.whatwg.org/')
assert.equal(request.url, 'https://fetch.spec.whatwg.org/')
Expand Down

0 comments on commit 4db9f7b

Please sign in to comment.