From 39b2be47a7c3f9d7c761e1714a65557490e5fd78 Mon Sep 17 00:00:00 2001 From: amine Date: Mon, 27 Mar 2017 15:28:31 +0200 Subject: [PATCH] Better document cookie addition Update README file and tests file to better document the cookie addition process, that is the pure cookie appending --- readme.md | 22 +++++++++++----------- tests/test.js | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 31 deletions(-) mode change 100755 => 100644 tests/test.js diff --git a/readme.md b/readme.md index 3d39ec6..9682ec4 100644 --- a/readme.md +++ b/readme.md @@ -1,14 +1,14 @@ -#CookieJar +# CookieJar Simple robust cookie library -##Exports +## Exports -###CookieAccessInfo(domain,path,secure,script) +### CookieAccessInfo(domain,path,secure,script) class to determine matching qualities of a cookie -#####Properties +##### Properties * String domain - domain to match * String path - path to match @@ -16,7 +16,7 @@ Simple robust cookie library * Boolean script - access is from a script -###Cookie(cookiestr_or_cookie, request_domain, request_path) +### Cookie(cookiestr_or_cookie, request_domain, request_path) turns input into a Cookie (singleton if given a Cookie) the `request_domain` argument is used to default the domain if it is not explicit in the cookie string @@ -24,7 +24,7 @@ Simple robust cookie library explicit domains/paths will cascade, implied domains/paths must *exactly* match (see http://en.wikipedia.org/wiki/HTTP_cookie#Domain_and_Pat) -#####Properties +##### Properties * String name - name of the cookie * String value - string associated with the cookie @@ -36,7 +36,7 @@ Simple robust cookie library * Boolean secure - should it only be transmitted over secure means * Number expiration_date - number of millis since 1970 at which this should be removed -#####Methods +##### Methods * String toString() - the __set-cookie:__ string for this cookie * String toValueString() - the __cookie:__ string for this cookie @@ -45,13 +45,13 @@ Simple robust cookie library * Boolean collidesWith(cookie) - returns true if the cookies cannot exist in the same space (domain and path match) -###CookieJar() +### CookieJar() class to hold numerous cookies from multiple domains correctly -#####Methods +##### Methods -* Cookie setCookie(cookie, request_domain, request_path) - add a cookie to the jar -* Cookie[] setCookies(cookiestr_or_list, request_domain, request_path) - add a large number of cookies to the jar +* Cookie setCookie(cookie, request_domain, request_path) - modify (or add if not already-existing) a cookie to the jar +* Cookie[] setCookies(cookiestr_or_list, request_domain, request_path) - modify (or add if not already-existing) a large number of cookies to the jar * Cookie getCookie(cookie_name,access_info) - get a cookie with the name and access_info matching * Cookie[] getCookies(access_info) - grab all cookies matching this access_info diff --git a/tests/test.js b/tests/test.js old mode 100755 new mode 100644 index dfa19fc..d4ea27f --- a/tests/test.js +++ b/tests/test.js @@ -43,15 +43,21 @@ assert.equal(String(test_jar.getCookies(CookieAccessInfo("test.com","/"))), "a=1 cookie=Cookie("a=1;domain=test.com;path=/;HttpOnly"); assert.ok(cookie.noscript, "HttpOnly flag parsing failed\n" + cookie.toString()); -var test_jar = CookieJar(); -test_jar.setCookies([ - "a=1;domain=.test.com;path=/" - , "a=1;domain=.test.com;path=/" - , "a=2;domain=.test.com;path=/" - , "b=3;domain=.test.com;path=/"]); -var cookies=test_jar.getCookies(CookieAccessInfo("test.com","/")) -assert.equal(cookies.length, 2); -assert.equal(cookies[0].value, 2); +var test_jar2 = CookieJar(); +test_jar2.setCookies([ + "a=1;domain=.test.com;path=/" + , "a=1;domain=.test.com;path=/" + , "a=2;domain=.test.com;path=/" + , "b=3;domain=.test.com;path=/"]); +var cookies2=test_jar2.getCookies(CookieAccessInfo("test.com","/")) +assert.equal(cookies2.length, 2); +assert.equal(cookies2[0].value, 2); + +// Test pure appending +test_jar2.setCookie("d=4;domain=.test.com;path=/"); +cookies2=test_jar2.getCookies(CookieAccessInfo("test.com","/")) +assert.equal(cookies2.length, 3); +assert.equal(cookies2[2].value, 4); // Test Ignore Trailing Semicolons (Github Issue #6) var cookie = new Cookie("a=1;domain=.test.com;path=/;;;;"); @@ -62,26 +68,26 @@ assert.equal(cookie.path, "/"); assert.deepEqual(cookie, new Cookie("a=1;domain=.test.com;path=/")); // Test request_path and request_domain -test_jar.setCookie(new Cookie("sub=4;path=/", "test.com")); -var cookie = test_jar.getCookie("sub", CookieAccessInfo("sub.test.com", "/")); +test_jar2.setCookie(new Cookie("sub=4;path=/", "test.com")); +var cookie = test_jar2.getCookie("sub", CookieAccessInfo("sub.test.com", "/")); assert.equal(cookie, undefined); -var cookie = test_jar.getCookie("sub", CookieAccessInfo("test.com", "/")); +var cookie = test_jar2.getCookie("sub", CookieAccessInfo("test.com", "/")); assert.equal(cookie.name, "sub"); assert.equal(cookie.domain, "test.com"); -test_jar.setCookie(new Cookie("sub=4;path=/accounts", "test.com", "/accounts")); -var cookie = test_jar.getCookie("sub", CookieAccessInfo("test.com", "/foo")); +test_jar2.setCookie(new Cookie("sub=4;path=/accounts", "test.com", "/accounts")); +var cookie = test_jar2.getCookie("sub", CookieAccessInfo("test.com", "/foo")); assert.equal(cookie, undefined); -var cookie = test_jar.getCookie("sub", CookieAccessInfo("test.com", "/accounts")); +var cookie = test_jar2.getCookie("sub", CookieAccessInfo("test.com", "/accounts")); assert.equal(cookie.path, "/accounts"); -test_jar.setCookie(new Cookie("sub=5;path=/", "test.com", "/accounts")); -var cookies = test_jar.getCookies(CookieAccessInfo("test.com")); -assert.equal(cookies.length, 3); +test_jar2.setCookie(new Cookie("sub=5;path=/", "test.com", "/accounts")); +var cookies = test_jar2.getCookies(CookieAccessInfo("test.com")); +assert.equal(cookies.length, 4); -test_jar.setCookie(new Cookie("sub=5;path=/", "test.com", "/accounts")); -var cookie = test_jar.getCookie('sub', CookieAccessInfo.All); +test_jar2.setCookie(new Cookie("sub=5;path=/", "test.com", "/accounts")); +var cookie = test_jar2.getCookie('sub', CookieAccessInfo.All); assert(cookie); assert.equal(cookie.name, 'sub');