Skip to content

Commit

Permalink
Merge branch 'master' into test-cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Jan 11, 2015
2 parents 1bbe6f0 + 99aae5d commit 33d47f4
Show file tree
Hide file tree
Showing 11 changed files with 451 additions and 234 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ fetch('/users')
})
```

### Response URL caveat

The `Response` object has a URL attribute for the final responded resource.
Usually this is the same as the `Request` url, but in the case of a redirect,
its all transparent. Newer versions of XHR include a `responseURL` attribute
that returns this value. But not every browser supports this. The compromise
requires setting a special server side header to tell the browser what URL it
just requested (yeah, I know browsers).

``` ruby
response.headers['X-Request-URL'] = request.url
```

If you want `response.url` to be reliable, you'll want to set this header. The
day that you ditch this polyfill and use native fetch only, you can remove the
header hack.

## Browser Support

![Chrome](https://raw.github.com/alrra/browser-logos/master/chrome/chrome_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/firefox/firefox_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/internet-explorer/internet-explorer_48x48.png) | ![Opera](https://raw.github.com/alrra/browser-logos/master/opera/opera_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/safari/safari_48x48.png)
Expand Down
3 changes: 1 addition & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"version": "0.4.0",
"main": "fetch.js",
"devDependencies": {
"es6-promise": "1.0.0",
"qunit": "1.14.0"
"es6-promise": "1.0.0"
},
"ignore": [
".*",
Expand Down
13 changes: 9 additions & 4 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@
}
}

this.formData = function() {
var rejected = consumed(this)
return rejected ? rejected : Promise.resolve(decode(this._body))
if (self.FormData) {
this.formData = function() {
var rejected = consumed(this)
return rejected ? rejected : Promise.resolve(decode(this._body))
}
}

this.json = function() {
Expand Down Expand Up @@ -180,7 +182,8 @@
var options = {
status: status,
statusText: xhr.statusText,
headers: headers(xhr)
headers: headers(xhr),
url: xhr.responseURL || xhr.getResponseHeader('X-Request-URL')
}
resolve(new Response(xhr.responseText, options))
}
Expand Down Expand Up @@ -210,6 +213,7 @@
this.status = options.status
this.statusText = options.statusText
this.headers = options.headers
this.url = options.url || ''
}

Body.call(Response.prototype)
Expand All @@ -221,4 +225,5 @@
self.fetch = function (url, options) {
return new Request(url, options).fetch()
}
self.fetch.polyfill = true
})();
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"private": true,
"devDependencies": {
"bower": "1.3.8",
"chai": "1.10.0",
"jshint": "2.5.2",
"node-qunit-phantomjs": "0.2.2"
"mocha-phantomjs": "3.5.2",
"mocha": "2.1.0",
"phantomjs": "1.9.13"
}
}
21 changes: 4 additions & 17 deletions test/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,14 @@
"browser": true,
"worker": true,
"globals": {
"MockXHR": false,
"QUnit": false,
"fetch": false,
"Headers": false,
"Request": false,
"Response": false,
"module": false,
"mocha": false,
"chai": false,
"suite": false,
"test": false,
"asyncTest": false,
"promiseTest": false,
"testDone": false,
"expect": false,
"start": false,
"stop": false,
"ok": false,
"equal": false,
"notEqual": false,
"deepEqual": false,
"notDeepEqual": false,
"strictEqual": false,
"notStrictEqual": false,
"raises": false
"assert": false
}
}
3 changes: 2 additions & 1 deletion test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ node ./test/server.js $port &>/dev/null &
server_pid=$!
trap "kill $server_pid" INT EXIT

node ./node_modules/.bin/node-qunit-phantomjs "http://localhost:$port/test/test.html"
node ./node_modules/.bin/mocha-phantomjs -s localToRemoteUrlAccessEnabled=true -s webSecurityEnabled=false "http://localhost:$port/test/test.html"
node ./node_modules/.bin/mocha-phantomjs -s localToRemoteUrlAccessEnabled=true -s webSecurityEnabled=false "http://localhost:$port/test/test-worker.html"
32 changes: 28 additions & 4 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,33 @@ var routes = {
}));
})
},
'/hello': function(res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
'/hello': function(res, req) {
res.writeHead(200, {
'Content-Type': 'text/plain',
'X-Request-URL': 'http://' + req.headers.host + req.url
});
res.end('hi');
},
'/redirect/301': function(res) {
res.writeHead(301, {'Location': '/hello'});
res.end();
},
'/redirect/302': function(res) {
res.writeHead(302, {'Location': '/hello'});
res.end();
},
'/redirect/303': function(res) {
res.writeHead(303, {'Location': '/hello'});
res.end();
},
'/redirect/307': function(res) {
res.writeHead(307, {'Location': '/hello'});
res.end();
},
'/redirect/308': function(res) {
res.writeHead(308, {'Location': '/hello'});
res.end();
},
'/boom': function(res) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('boom');
Expand All @@ -49,12 +72,13 @@ var routes = {
res.end('not json {');
},
'/cookie': function(res, req) {
var setCookie, cookie
var params = querystring.parse(url.parse(req.url).query);
if (params.value && params.value) {
var setCookie = [params.name, params.value].join('=');
setCookie = [params.name, params.value].join('=');
}
if (params.name) {
var cookie = querystring.parse(req.headers['cookie'], '; ')[params.name];
cookie = querystring.parse(req.headers['cookie'], '; ')[params.name];
}
res.writeHead(200, {'Content-Type': 'text/plain', 'Set-Cookie': setCookie});
res.end(cookie);
Expand Down
43 changes: 43 additions & 0 deletions test/test-worker.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Fetch Worker Tests</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="../node_modules/mocha/mocha.js"></script>

<script>
mocha.setup('tdd')

var worker = new Worker('worker.js')

worker.addEventListener('message', function(e) {
switch (e.data.name) {
case 'pass':
test(e.data.title, function() {})
break
case 'pending':
test(e.data.title)
break
case 'fail':
test(e.data.title, function() {
var err = new Error(e.data.message)
err.stack = e.data.stack
throw err
})
break
case 'end':
if (self.mochaPhantomJS) {
mochaPhantomJS.run()
} else {
mocha.run()
}
break
}
})
</script>
</body>
</html>
33 changes: 17 additions & 16 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@
<html>
<head>
<meta charset="utf-8">
<title>Test Suite</title>
<link rel="stylesheet" href="../bower_components/qunit/qunit/qunit.css">
<title>Fetch Tests</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<div id="mocha"></div>
<script src="../node_modules/chai/chai.js"></script>
<script src="../node_modules/mocha/mocha.js"></script>
<script>
mocha.setup('tdd')
self.assert = chai.assert
</script>

<script src="../bower_components/es6-promise/promise.js"></script>
<script src="../fetch.js"></script>
<script src="../bower_components/qunit/qunit/qunit.js"></script>

<script src="test.js"></script>

<script>
QUnit.promiseTest = function(testName, expected, callback) {
QUnit.test(testName, expected, function() {
stop();
Promise.resolve().then(callback).then(start, function(error) {
ok(false, error);
start();
});
});
if (self.mochaPhantomJS) {
mochaPhantomJS.run()
} else {
mocha.run()
}
window.promiseTest = QUnit.promiseTest;
</script>
<script>QUnit.config.testTimeout = 1000</script>
<script src="./test.js"></script>
</body>
</html>
Loading

0 comments on commit 33d47f4

Please sign in to comment.