From 9a6a7312f276c757db917d8c280f5a0d26acac74 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 2 May 2017 16:36:36 +0200 Subject: [PATCH] Fixes #3 - Only send cookies for the request host. --- cometd-nodejs-client.js | 42 ++++++++---- package.json | 6 ++ test/cookies.js | 147 ++++++++++++++++++++++++++++++++++++++++ test/library.js | 8 +++ 4 files changed, 188 insertions(+), 15 deletions(-) create mode 100644 test/cookies.js create mode 100644 test/library.js diff --git a/cometd-nodejs-client.js b/cometd-nodejs-client.js index 1aa364a..f9a849a 100644 --- a/cometd-nodejs-client.js +++ b/cometd-nodejs-client.js @@ -40,16 +40,20 @@ module.exports = { }; this.send = function(data) { - var cookies = ''; - for (var i = 0; i < _cookies.length; ++i) { - if (i > 0) { - cookies += '; '; + var list = _cookies[_config.hostname]; + if (list) { + var cookies = ''; + for (var i = 0; i < list.length; ++i) { + if (i > 0) { + cookies += '; '; + } + cookies += list[i]; + } + if (cookies) { + _config.headers['Cookie'] = cookies; } - cookies += _cookies[i]; - } - if (cookies) { - _config.headers['Cookie'] = cookies; } + var self = this; _request = http.request(_config, function(response) { var success = false; @@ -58,13 +62,21 @@ module.exports = { self.readyState = window.XMLHttpRequest.HEADERS_RECEIVED; var headers = response.headers; for (var name in headers) { - if (/^set-cookie$/i.test(name)) { - var header = headers[name]; - for (var i = 0; i < header.length; ++i) { - var whole = header[i]; - var parts = whole.split(';'); - var cookie = parts[0]; - _cookies.push(cookie); + if (headers.hasOwnProperty(name)) { + if (/^set-cookie$/i.test(name)) { + var header = headers[name]; + for (var i = 0; i < header.length; ++i) { + var whole = header[i]; + var parts = whole.split(';'); + var cookie = parts[0]; + + var host = _config.hostname; + var list = _cookies[host]; + if (list === undefined) { + _cookies[host] = list = []; + } + list.push(cookie); + } } } } diff --git a/package.json b/package.json index c6fcc72..8bf1c39 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,11 @@ "repository": { "type": "git", "url": "https://github.com/cometd/cometd-nodejs-client.git" + }, + "scripts": { + "test": "mocha" + }, + "devDependencies": { + "mocha": "*" } } diff --git a/test/cookies.js b/test/cookies.js new file mode 100644 index 0000000..44cfe49 --- /dev/null +++ b/test/cookies.js @@ -0,0 +1,147 @@ +var assert = require('assert'); +var cometd = require('..'); +var http = require('http'); + +describe('cookies', function() { + var _server; + + beforeEach(function() { + cometd.adapt(); + }); + + afterEach(function() { + if (_server) { + _server.close(); + } + }); + + it('receives, stores and sends cookie', function(done) { + var cookie = 'a=b'; + _server = http.createServer(function(request, response) { + if (/\/1$/.test(request.url)) { + response.setHeader('Set-Cookie', cookie); + response.end(); + } else if (/\/2$/.test(request.url)) { + assert.strictEqual(request.headers['cookie'], cookie); + response.end(); + } + }); + _server.listen(0, 'localhost', function() { + var port = _server.address().port; + console.log('listening on localhost:' + port); + var uri = 'http://localhost:' + port; + + var xhr1 = new window.XMLHttpRequest(); + xhr1.open('GET', uri + '/1'); + xhr1.onload = function() { + assert.strictEqual(xhr1.status, 200); + var xhr2 = new window.XMLHttpRequest(); + xhr2.open('GET', uri + '/2'); + xhr2.onload = function() { + assert.strictEqual(xhr2.status, 200); + done(); + }; + xhr2.send(); + }; + xhr1.send(); + }); + }); + + it('sends multiple cookies', function(done) { + var cookie1 = 'a=b'; + var cookie2 = 'c=d'; + var cookies = cookie1 + '; ' + cookie2; + _server = http.createServer(function(request, response) { + if (/\/1$/.test(request.url)) { + response.setHeader('Set-Cookie', cookie1); + response.end(); + } else if (/\/2$/.test(request.url)) { + response.setHeader('Set-Cookie', cookie2); + response.end(); + } else if (/\/3$/.test(request.url)) { + assert.strictEqual(request.headers['cookie'], cookies); + response.end(); + } + }); + _server.listen(0, 'localhost', function() { + var port = _server.address().port; + console.log('listening on localhost:' + port); + var uri = 'http://localhost:' + port; + + var xhr1 = new window.XMLHttpRequest(); + xhr1.open('GET', uri + '/1'); + xhr1.onload = function() { + assert.strictEqual(xhr1.status, 200); + var xhr2 = new window.XMLHttpRequest(); + xhr2.open('GET', uri + '/2'); + xhr2.onload = function() { + assert.strictEqual(xhr2.status, 200); + var xhr3 = new window.XMLHttpRequest(); + xhr3.open('GET', uri + '/3'); + xhr3.onload = function() { + assert.strictEqual(xhr3.status, 200); + done(); + }; + xhr3.send(); + }; + xhr2.send(); + }; + xhr1.send(); + }); + }); + + it('handles cookies from different hosts', function(done) { + var cookieA = 'a=b'; + var cookieB = 'b=c'; + _server = http.createServer(function(request, response) { + if (/\/hostA\//.test(request.url)) { + if (/\/1$/.test(request.url)) { + response.setHeader('Set-Cookie', cookieA); + response.end(); + } else if (/\/2$/.test(request.url)) { + assert.strictEqual(request.headers['cookie'], cookieA); + response.end(); + } + } else if (/\/hostB\//.test(request.url)) { + if (/\/1$/.test(request.url)) { + response.setHeader('Set-Cookie', cookieB); + response.end(); + } else if (/\/2$/.test(request.url)) { + assert.strictEqual(request.headers['cookie'], cookieB); + response.end(); + } + } + }); + _server.listen(0, 'localhost', function() { + var port = _server.address().port; + console.log('listening on localhost:' + port); + + var xhrA1 = new window.XMLHttpRequest(); + xhrA1.open('GET', 'http://localhost:' + port + '/hostA/1'); + xhrA1.onload = function() { + assert.strictEqual(xhrA1.status, 200); + var xhrA2 = new window.XMLHttpRequest(); + xhrA2.open('GET', 'http://localhost:' + port + '/hostA/2'); + xhrA2.onload = function() { + assert.strictEqual(xhrA2.status, 200); + + var xhrB1 = new window.XMLHttpRequest(); + xhrB1.open('GET', 'http://127.0.0.1:' + port + '/hostB/1'); + xhrB1.onload = function() { + assert.strictEqual(xhrB1.status, 200); + var xhrB2 = new window.XMLHttpRequest(); + xhrB2.open('GET', 'http://127.0.0.1:' + port + '/hostB/2'); + xhrB2.onload = function() { + assert.strictEqual(xhrB2.status, 200); + done(); + }; + xhrB2.send(); + }; + xhrB1.send(); + }; + xhrA2.send(); + }; + xhrA1.send(); + }); + }); +}); diff --git a/test/library.js b/test/library.js new file mode 100644 index 0000000..10624d0 --- /dev/null +++ b/test/library.js @@ -0,0 +1,8 @@ +var assert = require('assert'); +var cometd = require('..'); + +describe('library', function() { + it('adapter method exported', function() { + assert.ok(cometd.adapt); + }); +});