From eb02458d45e9a3f4202a2c9c69e88cf6c7b6d0f2 Mon Sep 17 00:00:00 2001 From: Sebastian Mayr Date: Sat, 3 Oct 2015 21:04:47 +0200 Subject: [PATCH] Implement loose cookie parsing This enables correct execution of code like `document.cookie = 'foo'`. --- lib/jsdom.js | 4 ++-- lib/jsdom/level1/core.js | 2 +- package.json | 2 +- test/level2/html.js | 2 +- test/living-html/cookie.js | 30 ++++++++++++++++++++---------- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/jsdom.js b/lib/jsdom.js index b0c5d3436a..2f16d666ef 100644 --- a/lib/jsdom.js +++ b/lib/jsdom.js @@ -36,7 +36,7 @@ exports.getVirtualConsole = function (window) { }; exports.createCookieJar = function () { - return new CookieJar(); + return new CookieJar(null, { looseMode: true }); }; exports.nodeLocation = function (node) { @@ -200,7 +200,7 @@ exports.env = exports.jsdom.env = function () { proxy: config.proxy || null }; - config.cookieJar = config.cookieJar || new CookieJar(); + config.cookieJar = config.cookieJar || exports.createCookieJar(); resourceLoader.download(config.url, options, config.cookieJar, null, (err, responseText, res) => { if (err) { diff --git a/lib/jsdom/level1/core.js b/lib/jsdom/level1/core.js index 020095c243..98a9b1e0e8 100644 --- a/lib/jsdom/level1/core.js +++ b/lib/jsdom/level1/core.js @@ -753,7 +753,7 @@ core.Document = function Document(options) { this._attached = true; this._readonly = false; this._currentScript = null; - this._cookieJar = options.cookieJar === undefined ? new CookieJar() : options.cookieJar; + this._cookieJar = options.cookieJar === undefined ? new CookieJar(null, { looseMode: true }) : options.cookieJar; this._contentType = options.contentType; if (this._contentType === undefined) { diff --git a/package.json b/package.json index e192e2ee85..c7ee299443 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "parse5": "^1.4.2", "request": "^2.55.0", "symbol-tree": ">= 3.1.0 < 4.0.0", - "tough-cookie": "^2.0.0", + "tough-cookie": "^2.2.0", "webidl-conversions": "^2.0.0", "whatwg-url-compat": "~0.6.5", "xml-name-validator": ">= 2.0.1 < 3.0.0", diff --git a/test/level2/html.js b/test/level2/html.js index 995062c0b7..1706fe9d47 100644 --- a/test/level2/html.js +++ b/test/level2/html.js @@ -2488,7 +2488,7 @@ exports.tests = { var ret = doc.cookie = null; test.equal(ret, null, "cookieLink"); - test.equal(doc.cookie, vcookie, "cookieLink"); + test.equal(doc.cookie, vcookie + "; null", "cookieLink"); // yes, this is actually how this should behave test.done(); diff --git a/test/living-html/cookie.js b/test/living-html/cookie.js index 29d2cc610c..001c897a06 100644 --- a/test/living-html/cookie.js +++ b/test/living-html/cookie.js @@ -17,7 +17,8 @@ const testCookies = [ "Test7=Secure; expires=Wed, 13-Jan-2051 22:23:01 GMT; path=/; Secure", "Test8=Expired; expires=Wed, 13-Jan-1977 22:23:01 GMT; path=/", "Test9=Duplicate; One=More; expires=Wed, 13-Jan-2051 22:23:01 GMT; path=/", - "Test10={\"prop1\":5,\"prop2\":\"value\"}; expires=Wed, 13-Jan-2051 22:23:01 GMT; path=/" + "Test10={\"prop1\":5,\"prop2\":\"value\"}; expires=Wed, 13-Jan-2051 22:23:01 GMT; path=/", + "Malformed; expires=Wed, 13-Jan-2051 22:23:01 GMT; path=/" ]; function assertCookies(t, actualCookieStr, expectedCookies) { @@ -104,7 +105,8 @@ exports["Set cookie by client"] = t => { "Test1=Basic", "Test2=PathMatch", "Test9=Duplicate", - "Test10={\"prop1\":5,\"prop2\":\"value\"}" + "Test10={\"prop1\":5,\"prop2\":\"value\"}", + "Malformed" ]); t.done(); } @@ -121,7 +123,8 @@ exports["Set cookie by page request"] = t => { "Test1=Basic", "Test2=PathMatch", "Test9=Duplicate", - "Test10={\"prop1\":5,\"prop2\":\"value\"}" + "Test10={\"prop1\":5,\"prop2\":\"value\"}", + "Malformed" ]); t.done(); } @@ -145,7 +148,8 @@ exports["Set cookie by resource request"] = t => { "Test1=Basic", "Test2=PathMatch", "Test9=Duplicate", - "Test10={\"prop1\":5,\"prop2\":\"value\"}" + "Test10={\"prop1\":5,\"prop2\":\"value\"}", + "Malformed" ]); t.done(); }; @@ -168,7 +172,8 @@ exports["Set cookie by XHR"] = t => { "Test1=Basic", "Test2=PathMatch", "Test9=Duplicate", - "Test10={\"prop1\":5,\"prop2\":\"value\"}" + "Test10={\"prop1\":5,\"prop2\":\"value\"}", + "Malformed" ]); t.done(); }; @@ -212,7 +217,8 @@ exports["Send Cookies header via resource request"] = t => { "Test2=PathMatch", "Test6=HttpOnly", "Test9=Duplicate", - "Test10={\"prop1\":5,\"prop2\":\"value\"}" + "Test10={\"prop1\":5,\"prop2\":\"value\"}", + "Malformed" ]); t.done(); }; @@ -236,7 +242,8 @@ exports["Send Cookies header via XHR"] = t => { "Test2=PathMatch", "Test6=HttpOnly", "Test9=Duplicate", - "Test10={\"prop1\":5,\"prop2\":\"value\"}" + "Test10={\"prop1\":5,\"prop2\":\"value\"}", + "Malformed" ]); t.done(); }; @@ -265,7 +272,8 @@ exports["Share cookies with