Skip to content

Commit

Permalink
Add tests for url validation
Browse files Browse the repository at this point in the history
  • Loading branch information
colincasey committed Apr 26, 2024
1 parent 50e69bf commit a48fb3a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand Down
37 changes: 37 additions & 0 deletions test/cookie_jar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a48fb3a

Please sign in to comment.