Comparing version 1.2.0 to 1.3.0
@@ -51,2 +51,3 @@ // | ||
function parseSetCookieHeader(header) { | ||
if (!header) return {}; | ||
header = (header instanceof Array) ? header : [header]; | ||
@@ -53,0 +54,0 @@ |
@@ -36,2 +36,5 @@ ////////////////////////////////////////// | ||
// see if we have Object.assign. otherwise fall back to util._extend | ||
var extend = Object.assign ? Object.assign : require('util')._extend; | ||
////////////////////////////////////////// | ||
@@ -418,4 +421,6 @@ // decompressors for gzip/deflate bodies | ||
if (headers['set-cookie'] && config.parse_cookies) { | ||
resp.cookies = cookies.read(headers['set-cookie']); | ||
// if we got cookies, parse them unless we were instructed not to. make sure to include any | ||
// cookies that might have been set on previous redirects. | ||
if (config.parse_cookies && (headers['set-cookie'] || config.stored_cookies)) { | ||
resp.cookies = extend(config.stored_cookies || {}, cookies.read(headers['set-cookie'])); | ||
debug('Got cookies', resp.cookies); | ||
@@ -437,4 +442,7 @@ } | ||
if (config.follow_set_cookies && resp.cookies) | ||
// if follow_set_cookies is true, make sure to put any cookies in the next request's headers. | ||
if (config.follow_set_cookies && resp.cookies) { | ||
config.stored_cookies = resp.cookies; | ||
config.headers['cookie'] = cookies.write(resp.cookies); | ||
} | ||
@@ -441,0 +449,0 @@ if (config.follow_set_referer) |
{ | ||
"name": "needle", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "The leanest and most handsome HTTP client in the Nodelands.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -1,6 +0,7 @@ | ||
var needle = require('../'), | ||
sinon = require('sinon'), | ||
http = require('http'), | ||
should = require('should'), | ||
assert = require('assert'); | ||
var needle = require('../'), | ||
cookies = require('../lib/cookies'), | ||
sinon = require('sinon'), | ||
http = require('http'), | ||
should = require('should'), | ||
assert = require('assert'); | ||
@@ -22,14 +23,6 @@ var WEIRD_COOKIE_NAME = 'wc', | ||
FORBIDDEN_COOKIE + '; ' + NUMBER_COOKIE, | ||
TEST_HOST = 'localhost'; | ||
NO_COOKIES_TEST_PORT = 11112, ALL_COOKIES_TEST_PORT = 11113; | ||
TEST_HOST = 'localhost', | ||
NO_COOKIES_TEST_PORT = 11112, | ||
ALL_COOKIES_TEST_PORT = 11113; | ||
function decode(str) { | ||
return decodeURIComponent(str); | ||
} | ||
function encode(str) { | ||
str = str.toString().replace(/[\x00-\x1F\x7F]/g, encodeURIComponent); | ||
return str.replace(/[\s\"\,;\\%]/g, encodeURIComponent); | ||
} | ||
describe('cookies', function() { | ||
@@ -39,2 +32,11 @@ | ||
function decode(str) { | ||
return decodeURIComponent(str); | ||
} | ||
function encode(str) { | ||
str = str.toString().replace(/[\x00-\x1F\x7F]/g, encodeURIComponent); | ||
return str.replace(/[\s\"\,;\\%]/g, encodeURIComponent); | ||
} | ||
before(function() { | ||
@@ -125,6 +127,75 @@ setCookieHeader = [ | ||
describe('and response is a redirect', function() { | ||
var redirectServer, testPort = 22222; | ||
var responseCookies = [ | ||
[ // first req | ||
WEIRD_COOKIE_NAME + '=' + encode(WEIRD_COOKIE_VALUE) + ';', | ||
BASE64_COOKIE_NAME + '=' + encode(BASE64_COOKIE_VALUE) + ';', | ||
'FOO=123;' | ||
], [ // second req | ||
FORBIDDEN_COOKIE_NAME + '=' + encode(FORBIDDEN_COOKIE_VALUE) + ';', | ||
NUMBER_COOKIE_NAME + '=' + encode(NUMBER_COOKIE_VALUE) + ';' | ||
], [ // third red | ||
'FOO=BAR;' | ||
] | ||
] | ||
before(function() { | ||
redirectServer = http.createServer(function(req, res) { | ||
var number = parseInt(req.url.replace('/', '')); | ||
var nextUrl = 'http://' + TEST_HOST + ':' + testPort + '/' + (number + 1); | ||
if (responseCookies[number]) { // got cookies | ||
res.statusCode = 302; | ||
res.setHeader('Set-Cookie', responseCookies[number]); | ||
res.setHeader('Location', nextUrl); | ||
} else if (number == 3) { | ||
res.statusCode = 302; // redirect but without cookies | ||
res.setHeader('Location', nextUrl); | ||
} | ||
res.end('OK'); | ||
}).listen(22222, TEST_HOST); | ||
}); | ||
after(function(done) { | ||
redirectServer.close(done); | ||
}) | ||
describe('and follow_set_cookies is false', function() { | ||
it('no cookie header set on redirection request', function() {}); | ||
var opts = { | ||
follow_set_cookies: false, | ||
follow_max: 4 | ||
}; | ||
it('no cookie header set on redirection request', function(done) { | ||
var spy = sinon.spy(cookies, 'write'); | ||
needle.get(TEST_HOST + ':' + testPort + '/0', opts, function(err, resp) { | ||
spy.callCount.should.eql(0); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('and follow_set_cookies is true', function() {}); | ||
describe('and follow_set_cookies is true', function() { | ||
var opts = { | ||
follow_set_cookies: true, | ||
follow_max: 4 | ||
}; | ||
it('should have all the cookies', function(done) { | ||
needle.get(TEST_HOST + ':' + testPort + '/0', opts, function(err, resp) { | ||
resp.cookies.should.have.property(WEIRD_COOKIE_NAME); | ||
resp.cookies.should.have.property(BASE64_COOKIE_NAME); | ||
resp.cookies.should.have.property(FORBIDDEN_COOKIE_NAME); | ||
resp.cookies.should.have.property(NUMBER_COOKIE_NAME); | ||
resp.cookies.should.have.property('FOO'); | ||
resp.cookies.FOO.should.eql('BAR'); // should overwrite previous one | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -217,2 +288,2 @@ | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
186740
4094