Skip to content

Commit 6617c40

Browse files
committed
Merge pull request #56 from github/test-cookies
Support credentials
2 parents 99aae5d + 33d47f4 commit 6617c40

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

fetch.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
options = options || {}
136136
this.url = url
137137
this._body = options.body
138-
this.credentials = options.credentials || null
138+
this.credentials = options.credentials || 'omit'
139139
this.headers = new Headers(options.headers)
140140
this.method = normalizeMethod(options.method || 'GET')
141141
this.mode = options.mode || null
@@ -225,4 +225,5 @@
225225
self.fetch = function (url, options) {
226226
return new Request(url, options).fetch()
227227
}
228+
self.fetch.polyfill = true
228229
})();

test/server.js

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var port = Number(process.argv[2] || 3000)
55
var fs = require('fs')
66
var http = require('http');
77
var url = require('url');
8+
var querystring = require('querystring');
89

910
var routes = {
1011
'/request': function(res, req) {
@@ -70,6 +71,18 @@ var routes = {
7071
res.writeHead(200, {'Content-Type': 'application/json'});
7172
res.end('not json {');
7273
},
74+
'/cookie': function(res, req) {
75+
var setCookie, cookie
76+
var params = querystring.parse(url.parse(req.url).query);
77+
if (params.value && params.value) {
78+
setCookie = [params.name, params.value].join('=');
79+
}
80+
if (params.name) {
81+
cookie = querystring.parse(req.headers['cookie'], '; ')[params.name];
82+
}
83+
res.writeHead(200, {'Content-Type': 'text/plain', 'Set-Cookie': setCookie});
84+
res.end(cookie);
85+
},
7386
'/headers': function(res) {
7487
res.writeHead(200, {
7588
'Date': 'Mon, 13 Oct 2014 21:02:27 GMT',

test/test.js

+61
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,64 @@ suite('Atomic HTTP redirect handling', function() {
283283
})
284284
})
285285
})
286+
287+
// https://fetch.spec.whatwg.org/#concept-request-credentials-mode
288+
suite('credentials mode', function() {
289+
var omitSupported = !self.fetch.polyfill
290+
291+
;(omitSupported ? suite : suite.skip)('omit', function() {
292+
test('request credentials defaults to omit', function() {
293+
var request = new Request('')
294+
assert.equal(request.credentials, 'omit')
295+
})
296+
297+
test('does not send cookies with implicit omit credentials', function() {
298+
return fetch('/cookie?name=foo&value=bar').then(function() {
299+
return fetch('/cookie?name=foo');
300+
}).then(function(response) {
301+
return response.text()
302+
}).then(function(data) {
303+
assert.equal(data, '')
304+
})
305+
})
306+
307+
test('does not send cookies with omit credentials', function() {
308+
return fetch('/cookie?name=foo&value=bar').then(function() {
309+
return fetch('/cookie?name=foo', {credentials: 'omit'})
310+
}).then(function(response) {
311+
return response.text()
312+
}).then(function(data) {
313+
assert.equal(data, '')
314+
})
315+
})
316+
})
317+
318+
suite('same-origin', function() {
319+
test('request credentials uses inits member', function() {
320+
var request = new Request('', {credentials: 'same-origin'})
321+
assert.equal(request.credentials, 'same-origin')
322+
})
323+
324+
test('send cookies with same-origin credentials', function() {
325+
return fetch('/cookie?name=foo&value=bar').then(function() {
326+
return fetch('/cookie?name=foo', {credentials: 'same-origin'})
327+
}).then(function(response) {
328+
return response.text()
329+
}).then(function(data) {
330+
assert.equal(data, 'bar')
331+
})
332+
})
333+
})
334+
335+
suite('include', function() {
336+
test('send cookies with include credentials', function() {
337+
return fetch('/cookie?name=foo&value=bar').then(function() {
338+
return fetch('/cookie?name=foo', {credentials: 'include'})
339+
}).then(function(response) {
340+
return response.text()
341+
}).then(function(data) {
342+
assert.equal(data, 'bar')
343+
})
344+
})
345+
})
346+
})

0 commit comments

Comments
 (0)