Skip to content

Commit

Permalink
Merge pull request JakeChampion#430 from mislav/array-buffer-view
Browse files Browse the repository at this point in the history
Support ArrayBufferView types as POST body
  • Loading branch information
mislav authored Nov 12, 2016
2 parents 87fd1cd + 25f7646 commit 4528229
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ sauce_connect/bin/sc:
curl -fsSL http://saucelabs.com/downloads/sc-4.3.16-linux.tar.gz | tar xz -C sauce_connect --strip-components 1
endif

.PHONY: build clean lint test saucelabs travis
phantomjs/bin/phantomjs:
mkdir -p phantomjs
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 -O- | tar xj -C phantomjs --strip-components 1

.PHONY: build clean lint test
27 changes: 26 additions & 1 deletion fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@
arrayBuffer: 'ArrayBuffer' in self
}

if (support.arrayBuffer) {
var viewClasses = [
'[object Int8Array]',
'[object Uint8Array]',
'[object Uint8ClampedArray]',
'[object Int16Array]',
'[object Uint16Array]',
'[object Int32Array]',
'[object Uint32Array]',
'[object Float32Array]',
'[object Float64Array]'
]

var isDataView = function(obj) {
return obj && DataView.prototype.isPrototypeOf(obj)
}

var isArrayBufferView = ArrayBuffer.isView || function(obj) {
return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1
}
}

function normalizeName(name) {
if (typeof name !== 'string') {
name = String(name)
Expand Down Expand Up @@ -177,7 +199,10 @@
this._bodyText = body.toString()
} else if (!body) {
this._bodyText = ''
} else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
} else if (support.arrayBuffer && support.blob && isDataView(body)) {
// IE 10-11 can't handle a DataView body.
this._bodyInit = new Blob([body.buffer])
} else if (support.arrayBuffer && (ArrayBuffer.prototype.isPrototypeOf(body) || isArrayBufferView(body))) {
// Only support ArrayBuffers for POST method.
// Receiving ArrayBuffers happens via Blobs, instead.
} else {
Expand Down
5 changes: 5 additions & 0 deletions script/phantomjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ STATUS=0
reporter=dot
[ -z "$CI" ] || reporter=spec

if [ -n "$TRAVIS" ]; then
make phantomjs/bin/phantomjs
export PATH="$PWD/phantomjs/bin:$PATH"
fi

run() {
phantomjs ./node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js \
"$1" $reporter "{\"useColors\":true, \"hooks\":\"$PWD/test/mocha-phantomjs-hooks.js\"}" \
Expand Down
44 changes: 35 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,15 +868,41 @@ suite('fetch method', function() {
})
})

featureDependent(test, support.arrayBuffer, 'sends ArrayBuffer body', function() {
return fetch('/request', {
method: 'post',
body: arrayBufferFromText('name=Hubot')
}).then(function(response) {
return response.json()
}).then(function(request) {
assert.equal(request.method, 'POST')
assert.equal(request.data, 'name=Hubot')
featureDependent(suite, support.arrayBuffer, 'ArrayBuffer', function() {
test('ArrayBuffer body', function() {
return fetch('/request', {
method: 'post',
body: arrayBufferFromText('name=Hubot')
}).then(function(response) {
return response.json()
}).then(function(request) {
assert.equal(request.method, 'POST')
assert.equal(request.data, 'name=Hubot')
})
})

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

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

Expand Down

0 comments on commit 4528229

Please sign in to comment.