Permalink
Browse files

Merge pull request #801 from lalitkapoor/794-ignore-cookie-parsing-an…

…d-domain-errors

794 ignore cookie parsing and domain errors
  • Loading branch information...
2 parents 22cfb9a + d6b2b1c commit 650e64bbee12d925608ac32f7a80e6ed3011fc35 @lalitkapoor lalitkapoor committed Feb 6, 2014
Showing with 53 additions and 4 deletions.
  1. +2 −2 lib/cookies.js
  2. +1 −1 request.js
  3. +42 −1 tests/test-cookies.js
  4. +8 −0 tests/test-headers.js
View
@@ -17,8 +17,8 @@ exports.parse = function(str) {
function RequestJar() {
this._jar = new CookieJar();
}
-RequestJar.prototype.setCookie = function(cookieOrStr, uri) {
- return this._jar.setCookieSync(cookieOrStr, uri);
+RequestJar.prototype.setCookie = function(cookieOrStr, uri, options) {
+ return this._jar.setCookieSync(cookieOrStr, uri, options || {});
};
RequestJar.prototype.getCookieString = function(uri) {
return this._jar.getCookieStringSync(uri);
View
@@ -656,7 +656,7 @@ Request.prototype.onResponse = function (response) {
var addCookie = function (cookie) {
//set the cookie if it's domain in the href's domain.
try {
- targetCookieJar.setCookie(cookie, self.uri.href);
+ targetCookieJar.setCookie(cookie, self.uri.href, {ignoreError: true});
} catch (e) {
self.emit('error', e);
}
View
@@ -7,6 +7,7 @@ try {
}
var assert = require('assert')
+ , http = require('http')
, request = require('../index')
@@ -16,4 +17,44 @@ function simpleCookieCreationTest() {
assert(cookie.value === 'bar')
}
-simpleCookieCreationTest()
+simpleCookieCreationTest()
+
+var requests = 0;
+var validUrl = 'http://localhost:6767/valid';
+var invalidUrl = 'http://localhost:6767/invalid';
+
+var server = http.createServer(function (req, res) {
+ requests++;
+ if (req.url === '/valid')
+ res.setHeader('set-cookie', 'foo=bar');
+ else if (req.url === '/invalid')
+ res.setHeader('set-cookie', 'foo=bar; Domain=foo.com');
+ res.end('okay');
+ if (requests === 2) server.close();
+});
+server.listen(6767);
+
+var jar1 = request.jar();
+request({
+ method: 'GET',
+ url: validUrl,
+ jar: jar1
+},
+function (error, response, body) {
+ if (error) throw error;
+ assert.equal(jar1.getCookieString(validUrl), 'foo=bar');
+ assert.equal(body, 'okay');
+});
+
+
+var jar2 = request.jar();
+request({
+ method: 'GET',
+ url: invalidUrl,
+ jar: jar2
+},
+function (error, response, body) {
+ if (error) throw error;
+ assert.equal(jar2.getCookieString(validUrl), '');
+ assert.equal(body, 'okay');
+});
View
@@ -52,6 +52,14 @@ s.listen(s.port, function () {
assert.equal(req.headers.cookie, 'foo=bar; quux=baz')
})
+ // Issue #794 add ability to ignore cookie parsing and domain errors
+ var jar2 = request.jar()
+ jar2.setCookie('quux=baz; Domain=foo.bar.com', serverUri, {ignoreError: true});
+ createTest({jar: jar2, headers: {cookie: 'foo=bar'}}, function (req, res) {
+ assert.ok(req.headers.cookie)
+ assert.equal(req.headers.cookie, 'foo=bar')
+ })
+
// Issue #784: override content-type when json is used
// https://github.com/mikeal/request/issues/784
createTest({

0 comments on commit 650e64b

Please sign in to comment.