Skip to content

Commit

Permalink
Merge pull request #56 from github/test-cookies
Browse files Browse the repository at this point in the history
Support credentials
  • Loading branch information
josh committed Jan 11, 2015
2 parents 99aae5d + 33d47f4 commit 6617c40
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
3 changes: 2 additions & 1 deletion fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
options = options || {}
this.url = url
this._body = options.body
this.credentials = options.credentials || null
this.credentials = options.credentials || 'omit'
this.headers = new Headers(options.headers)
this.method = normalizeMethod(options.method || 'GET')
this.mode = options.mode || null
Expand Down Expand Up @@ -225,4 +225,5 @@
self.fetch = function (url, options) {
return new Request(url, options).fetch()
}
self.fetch.polyfill = true
})();
13 changes: 13 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var port = Number(process.argv[2] || 3000)
var fs = require('fs')
var http = require('http');
var url = require('url');
var querystring = require('querystring');

var routes = {
'/request': function(res, req) {
Expand Down Expand Up @@ -70,6 +71,18 @@ var routes = {
res.writeHead(200, {'Content-Type': 'application/json'});
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) {
setCookie = [params.name, params.value].join('=');
}
if (params.name) {
cookie = querystring.parse(req.headers['cookie'], '; ')[params.name];
}
res.writeHead(200, {'Content-Type': 'text/plain', 'Set-Cookie': setCookie});
res.end(cookie);
},
'/headers': function(res) {
res.writeHead(200, {
'Date': 'Mon, 13 Oct 2014 21:02:27 GMT',
Expand Down
61 changes: 61 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,64 @@ suite('Atomic HTTP redirect handling', function() {
})
})
})

// https://fetch.spec.whatwg.org/#concept-request-credentials-mode
suite('credentials mode', function() {
var omitSupported = !self.fetch.polyfill

;(omitSupported ? suite : suite.skip)('omit', function() {
test('request credentials defaults to omit', function() {
var request = new Request('')
assert.equal(request.credentials, 'omit')
})

test('does not send cookies with implicit omit credentials', function() {
return fetch('/cookie?name=foo&value=bar').then(function() {
return fetch('/cookie?name=foo');
}).then(function(response) {
return response.text()
}).then(function(data) {
assert.equal(data, '')
})
})

test('does not send cookies with omit credentials', function() {
return fetch('/cookie?name=foo&value=bar').then(function() {
return fetch('/cookie?name=foo', {credentials: 'omit'})
}).then(function(response) {
return response.text()
}).then(function(data) {
assert.equal(data, '')
})
})
})

suite('same-origin', function() {
test('request credentials uses inits member', function() {
var request = new Request('', {credentials: 'same-origin'})
assert.equal(request.credentials, 'same-origin')
})

test('send cookies with same-origin credentials', function() {
return fetch('/cookie?name=foo&value=bar').then(function() {
return fetch('/cookie?name=foo', {credentials: 'same-origin'})
}).then(function(response) {
return response.text()
}).then(function(data) {
assert.equal(data, 'bar')
})
})
})

suite('include', function() {
test('send cookies with include credentials', function() {
return fetch('/cookie?name=foo&value=bar').then(function() {
return fetch('/cookie?name=foo', {credentials: 'include'})
}).then(function(response) {
return response.text()
}).then(function(data) {
assert.equal(data, 'bar')
})
})
})
})

0 comments on commit 6617c40

Please sign in to comment.