Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix request cookies lost during multiple redirects #291

Merged
merged 3 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/needle.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
}

debug('Making request #' + count, request_opts);
var request_cookieStr = request_opts.headers.cookie
if (request_cookieStr && count === 1) {
config.stored_cookies = cookies.read(request_cookieStr)
}
var request = protocol.request(request_opts, function(resp) {

var headers = resp.headers;
Expand Down
9 changes: 7 additions & 2 deletions test/cookies_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ var needle = require('../'),
should = require('should'),
assert = require('assert');

var WEIRD_COOKIE_NAME = 'wc',
var REQUEST_COOKIE_NAME = 'rc',
WEIRD_COOKIE_NAME = 'wc',
BASE64_COOKIE_NAME = 'bc',
FORBIDDEN_COOKIE_NAME = 'fc',
NUMBER_COOKIE_NAME = 'nc';

var WEIRD_COOKIE_VALUE = '!\'*+#()&-./0123456789:<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~',
var REQUEST_COOKIE_VALUE = 'a492b257b5e9eab8b1bb7421e394c893afa08b21',
WEIRD_COOKIE_VALUE = '!\'*+#()&-./0123456789:<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~',
BASE64_COOKIE_VALUE = 'Y29va2llCg==',
FORBIDDEN_COOKIE_VALUE = ' ;"\\,',
NUMBER_COOKIE_VALUE = 12354342;
Expand Down Expand Up @@ -194,12 +196,15 @@ describe('cookies', function() {

describe('and follow_set_cookies is true', function() {
var opts = {
cookies: { 'rc': REQUEST_COOKIE_VALUE },
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(REQUEST_COOKIE_NAME)
resp.cookies[REQUEST_COOKIE_NAME].should.eql(REQUEST_COOKIE_VALUE)
resp.cookies.should.have.property(WEIRD_COOKIE_NAME);
resp.cookies.should.have.property(BASE64_COOKIE_NAME);
resp.cookies.should.have.property(FORBIDDEN_COOKIE_NAME);
Expand Down