Skip to content

Commit

Permalink
Merge branch 'master' into array-buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Nov 14, 2016
2 parents 211d5ca + 4949171 commit 7021a0c
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 28 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
sudo: false
language: node_js
before_script: make
script: make test
deploy:
provider: npm
email: [email protected]
Expand Down
13 changes: 6 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
build: node_modules/ bower_components/

test: node_modules/ build lint
test: node_modules/ lint
./script/test

lint: node_modules/
./node_modules/.bin/jshint *.js test/*.js

bower_components/: node_modules/
./node_modules/.bin/bower install

node_modules/:
npm install

Expand All @@ -27,4 +22,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: clean lint test
3 changes: 0 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"name": "fetch",
"main": "fetch.js",
"devDependencies": {
"es6-promise": "1.0.0"
},
"ignore": [
".*",
"*.md",
Expand Down
31 changes: 28 additions & 3 deletions 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))) {
if (body.slice) {
this._bodyArrayBuffer = new Uint8Array(body.slice(0))
} else {
Expand Down Expand Up @@ -341,9 +366,9 @@
}

this.type = 'default'
this.status = options.status
this.status = 'status' in options ? options.status : 200
this.ok = this.status >= 200 && this.status < 300
this.statusText = options.statusText
this.statusText = 'statusText' in options ? options.statusText : 'OK'
this.headers = new Headers(options.headers)
this.url = options.url || ''
this._initBody(bodyInit)
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
"repository": "github/fetch",
"license": "MIT",
"devDependencies": {
"bower": "1.3.8",
"chai": "1.10.0",
"jshint": "2.8.0",
"mocha": "2.1.0",
"mocha-phantomjs-core": "2.0.1",
"promise-polyfill": "6.0.2",
"url-search-params": "0.6.1"
},
"files": [
"LICENSE",
"fetch.js"
]
],
"scripts": {
"test": "make"
}
}
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
2 changes: 1 addition & 1 deletion test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
</script>

<script src="/bower_components/es6-promise/promise.js"></script>
<script src="/node_modules/promise-polyfill/promise.js"></script>
<script src="/test/test.js"></script>
<script src="/fetch.js"></script>

Expand Down
51 changes: 42 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,13 @@ suite('Request', function() {

// https://fetch.spec.whatwg.org/#response-class
suite('Response', function() {
test('default status is 200 OK', function() {
var res = new Response()
assert.equal(res.status, 200)
assert.equal(res.statusText, 'OK')
assert.isTrue(res.ok)
})

// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
suite('BodyInit extract', function() {
featureDependent(suite, support.blob, 'type Blob', function() {
Expand Down Expand Up @@ -912,15 +919,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
2 changes: 1 addition & 1 deletion test/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ importScripts('/node_modules/mocha/mocha.js')
mocha.setup('tdd')
self.assert = chai.assert

importScripts('/bower_components/es6-promise/promise.js')
importScripts('/node_modules/promise-polyfill/promise.js')
importScripts('/test/test.js')
importScripts('/fetch.js')

Expand Down

0 comments on commit 7021a0c

Please sign in to comment.