From a48fb3a6ba2bbce41595a20e1db56543d974057b Mon Sep 17 00:00:00 2001 From: Colin Casey Date: Fri, 26 Apr 2024 16:36:41 -0300 Subject: [PATCH] Add tests for url validation --- lib/validators.js | 5 ++++- test/cookie_jar_test.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/validators.js b/lib/validators.js index 00668c6b..3dd46495 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -65,7 +65,10 @@ function isInstanceStrict(data, prototype) { function isUrlStringOrObject(data) { return ( isNonEmptyString(data) || - isObject(data) || // TODO: Check for URL properties that are used. + (isObject(data) && + "hostname" in data && + "pathname" in data && + "protocol" in data) || isInstanceStrict(data, URL) ); } diff --git a/test/cookie_jar_test.js b/test/cookie_jar_test.js index 50205bce..beee62c9 100644 --- a/test/cookie_jar_test.js +++ b/test/cookie_jar_test.js @@ -67,6 +67,32 @@ vows assert.ok(!c.isPersistent()); } }, + "Setting a basic cookie (URL)": { + topic: function() { + const cj = new CookieJar(); + const c = Cookie.parse("a=b; Domain=example.com; Path=/"); + assert.strictEqual(c.hostOnly, null); + assert.instanceOf(c.creation, Date); + assert.strictEqual(c.lastAccessed, null); + c.creation = new Date(Date.now() - 10000); + cj.setCookie( + c, + new URL("http://example.com/index.html"), + this.callback + ); + }, + works: function(c) { + assert.instanceOf(c, Cookie); + }, // C is for Cookie, good enough for me + "gets timestamped": function(c) { + assert.ok(c.creation); + assert.ok(Date.now() - c.creation.getTime() < 5000); // recently stamped + assert.ok(c.lastAccessed); + assert.equal(c.creation, c.lastAccessed); + assert.equal(c.TTL(), Infinity); + assert.ok(!c.isPersistent()); + } + }, "Setting a no-path cookie": { topic: function() { const cj = new CookieJar(); @@ -366,6 +392,17 @@ vows assert.equal(cookie.domain, "nodejs.org"); } }, + "then retrieving for http://nodejs.org (URL)": { + topic: function(cj, oldResults) { + assert.ok(oldResults); + cj.getCookies(new URL("http://nodejs.org"), this.callback); + }, + "get a nodejs cookie": function(cookies) { + assert.lengthOf(cookies, 1); + const cookie = cookies[0]; + assert.equal(cookie.domain, "nodejs.org"); + } + }, "then retrieving for https://example.com": { topic: function(cj, oldResults) { assert.ok(oldResults);