Skip to content

Commit a48fb3a

Browse files
committed
Add tests for url validation
1 parent 50e69bf commit a48fb3a

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/validators.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ function isInstanceStrict(data, prototype) {
6565
function isUrlStringOrObject(data) {
6666
return (
6767
isNonEmptyString(data) ||
68-
isObject(data) || // TODO: Check for URL properties that are used.
68+
(isObject(data) &&
69+
"hostname" in data &&
70+
"pathname" in data &&
71+
"protocol" in data) ||
6972
isInstanceStrict(data, URL)
7073
);
7174
}

test/cookie_jar_test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,32 @@ vows
6767
assert.ok(!c.isPersistent());
6868
}
6969
},
70+
"Setting a basic cookie (URL)": {
71+
topic: function() {
72+
const cj = new CookieJar();
73+
const c = Cookie.parse("a=b; Domain=example.com; Path=/");
74+
assert.strictEqual(c.hostOnly, null);
75+
assert.instanceOf(c.creation, Date);
76+
assert.strictEqual(c.lastAccessed, null);
77+
c.creation = new Date(Date.now() - 10000);
78+
cj.setCookie(
79+
c,
80+
new URL("http://example.com/index.html"),
81+
this.callback
82+
);
83+
},
84+
works: function(c) {
85+
assert.instanceOf(c, Cookie);
86+
}, // C is for Cookie, good enough for me
87+
"gets timestamped": function(c) {
88+
assert.ok(c.creation);
89+
assert.ok(Date.now() - c.creation.getTime() < 5000); // recently stamped
90+
assert.ok(c.lastAccessed);
91+
assert.equal(c.creation, c.lastAccessed);
92+
assert.equal(c.TTL(), Infinity);
93+
assert.ok(!c.isPersistent());
94+
}
95+
},
7096
"Setting a no-path cookie": {
7197
topic: function() {
7298
const cj = new CookieJar();
@@ -366,6 +392,17 @@ vows
366392
assert.equal(cookie.domain, "nodejs.org");
367393
}
368394
},
395+
"then retrieving for http://nodejs.org (URL)": {
396+
topic: function(cj, oldResults) {
397+
assert.ok(oldResults);
398+
cj.getCookies(new URL("http://nodejs.org"), this.callback);
399+
},
400+
"get a nodejs cookie": function(cookies) {
401+
assert.lengthOf(cookies, 1);
402+
const cookie = cookies[0];
403+
assert.equal(cookie.domain, "nodejs.org");
404+
}
405+
},
369406
"then retrieving for https://example.com": {
370407
topic: function(cj, oldResults) {
371408
assert.ok(oldResults);

0 commit comments

Comments
 (0)