Comparing version 1.0.1 to 2.0.0
'use strict'; | ||
module.exports = function(grunt) { | ||
// Project configuration. | ||
grunt.initConfig({ | ||
jshint: { | ||
all: ['lib/*.js', 'test/*.js', 'Gruntfile.js'], | ||
options: { | ||
jshintrc: '.jshintrc' | ||
} | ||
eslint: { | ||
all: ['lib/*.js', 'test/*.js', 'Gruntfile.js'] | ||
}, | ||
@@ -25,7 +21,7 @@ | ||
// Load the plugin(s) | ||
grunt.loadNpmTasks('grunt-contrib-jshint'); | ||
grunt.loadNpmTasks('grunt-eslint'); | ||
grunt.loadNpmTasks('grunt-mocha-test'); | ||
// Tasks | ||
grunt.registerTask('default', ['jshint', 'mochaTest']); | ||
grunt.registerTask('default', ['eslint', 'mochaTest']); | ||
}; |
'use strict'; | ||
var urllib = require('url'); | ||
var psl = require('psl'); | ||
const urllib = require('url'); | ||
const psl = require('psl'); | ||
const SESSION_TIMEOUT = 1800; // 30 min | ||
module.exports = Biskviit; | ||
/** | ||
@@ -16,8 +14,9 @@ * Creates a biskviit cookie jar for managing cookie values in memory | ||
*/ | ||
function Biskviit(options) { | ||
this.options = options || {}; | ||
this.cookies = []; | ||
} | ||
class Biskviit { | ||
constructor(options) { | ||
this.options = options || {}; | ||
this.cookies = []; | ||
} | ||
/** | ||
/** | ||
* Stores a cookie string to the cookie storage | ||
@@ -28,40 +27,40 @@ * | ||
*/ | ||
Biskviit.prototype.set = function(cookieStr, url) { | ||
var urlparts = urllib.parse(url || ''); | ||
var cookie = this.parse(cookieStr); | ||
set(cookieStr, url) { | ||
let urlparts = urllib.parse(url || ''); | ||
let cookie = this.parse(cookieStr); | ||
if (cookie.domain) { | ||
let domain = cookie.domain.replace(/^\./, ''); | ||
if (cookie.domain) { | ||
let domain = cookie.domain.replace(/^\./, ''); | ||
// do not allow generic TLDs, except unlisted | ||
if (psl.parse(domain).listed && !psl.isValid(domain)) { | ||
// do not allow generic TLDs, except unlisted | ||
if (psl.parse(domain).listed && !psl.isValid(domain)) { | ||
cookie.domain = urlparts.hostname; | ||
} | ||
// do not allow cross origin cookies | ||
if ( | ||
// can't be valid if the requested domain is shorter than current hostname | ||
urlparts.hostname.length < domain.length || | ||
// prefix domains with dot to be sure that partial matches are not used | ||
('.' + urlparts.hostname).substr(-domain.length + 1) !== '.' + domain | ||
) { | ||
cookie.domain = urlparts.hostname; | ||
} | ||
} else { | ||
cookie.domain = urlparts.hostname; | ||
} | ||
// do not allow cross origin cookies | ||
if ( | ||
// can't be valid if the requested domain is shorter than current hostname | ||
urlparts.hostname.length < domain.length || | ||
if (!cookie.path) { | ||
cookie.path = this.getPath(urlparts.pathname); | ||
} | ||
// prefix domains with dot to be sure that partial matches are not used | ||
('.' + urlparts.hostname).substr(-domain.length + 1) !== ('.' + domain)) { | ||
cookie.domain = urlparts.hostname; | ||
// if no expire date, then use sessionTimeout value | ||
if (!cookie.expires) { | ||
cookie.expires = new Date(Date.now() + (Number(this.options.sessionTimeout || SESSION_TIMEOUT) || SESSION_TIMEOUT) * 1000); | ||
} | ||
} else { | ||
cookie.domain = urlparts.hostname; | ||
} | ||
if (!cookie.path) { | ||
cookie.path = this.getPath(urlparts.pathname); | ||
return this.add(cookie); | ||
} | ||
// if no expire date, then use sessionTimeout value | ||
if (!cookie.expires) { | ||
cookie.expires = new Date(Date.now() + (Number(this.options.sessionTimeout || SESSION_TIMEOUT) || SESSION_TIMEOUT) * 1000); | ||
} | ||
return this.add(cookie); | ||
}; | ||
/** | ||
/** | ||
* Returns cookie string for the 'Cookie:' header. | ||
@@ -72,9 +71,7 @@ * | ||
*/ | ||
Biskviit.prototype.get = function(url) { | ||
return this.list(url).map(function(cookie) { | ||
return cookie.name + '=' + cookie.value; | ||
}).join('; '); | ||
}; | ||
get(url) { | ||
return this.list(url).map(cookie => cookie.name + '=' + cookie.value).join('; '); | ||
} | ||
/** | ||
/** | ||
* Lists all valied cookie objects for the specified URL | ||
@@ -85,22 +82,22 @@ * | ||
*/ | ||
Biskviit.prototype.list = function(url) { | ||
var result = []; | ||
list(url) { | ||
let result = []; | ||
for (let i = this.cookies.length - 1; i >= 0; i--) { | ||
let cookie = this.cookies[i]; | ||
for (let i = this.cookies.length - 1; i >= 0; i--) { | ||
let cookie = this.cookies[i]; | ||
if (this.isExpired(cookie)) { | ||
this.cookies.splice(i, i); | ||
continue; | ||
if (this.isExpired(cookie)) { | ||
this.cookies.splice(i, i); | ||
continue; | ||
} | ||
if (this.match(cookie, url)) { | ||
result.unshift(cookie); | ||
} | ||
} | ||
if (this.match(cookie, url)) { | ||
result.unshift(cookie); | ||
} | ||
return result; | ||
} | ||
return result; | ||
}; | ||
/** | ||
/** | ||
* Parses cookie string from the 'Set-Cookie:' header | ||
@@ -111,61 +108,60 @@ * | ||
*/ | ||
Biskviit.prototype.parse = function(cookieStr) { | ||
var cookie = {}; | ||
parse(cookieStr) { | ||
let cookie = {}; | ||
(cookieStr || '').toString().split(';').forEach(function(cookiePart) { | ||
var valueParts = cookiePart.split('='); | ||
var key = valueParts.shift().trim().toLowerCase(); | ||
var value = valueParts.join('=').trim(); | ||
(cookieStr || '').toString().split(';').forEach(cookiePart => { | ||
let valueParts = cookiePart.split('='); | ||
let key = valueParts.shift().trim().toLowerCase(); | ||
let value = valueParts.join('=').trim(); | ||
if (!key) { | ||
// skip empty parts | ||
return; | ||
} | ||
if (!key) { | ||
// skip empty parts | ||
return; | ||
} | ||
switch (key) { | ||
switch (key) { | ||
case 'expires': | ||
value = new Date(value); | ||
// ignore date if can not parse it | ||
if (value.toString() !== 'Invalid Date') { | ||
cookie.expires = value; | ||
} | ||
break; | ||
case 'expires': | ||
value = new Date(value); | ||
// ignore date if can not parse it | ||
if (value.toString() !== 'Invalid Date') { | ||
cookie.expires = value; | ||
} | ||
break; | ||
case 'path': | ||
cookie.path = value; | ||
break; | ||
case 'path': | ||
cookie.path = value; | ||
break; | ||
case 'domain': | ||
let domain = value.toLowerCase(); | ||
if (domain.length && domain.charAt(0) !== '.') { | ||
domain = '.' + domain; // ensure preceeding dot for user set domains | ||
case 'domain': { | ||
let domain = value.toLowerCase(); | ||
if (domain.length && domain.charAt(0) !== '.') { | ||
domain = '.' + domain; // ensure preceeding dot for user set domains | ||
} | ||
cookie.domain = domain; | ||
break; | ||
} | ||
cookie.domain = domain; | ||
break; | ||
case 'max-age': | ||
cookie.expires = new Date(Date.now() + (Number(value) || 0) * 1000); | ||
break; | ||
case 'max-age': | ||
cookie.expires = new Date(Date.now() + (Number(value) || 0) * 1000); | ||
break; | ||
case 'secure': | ||
cookie.secure = true; | ||
break; | ||
case 'secure': | ||
cookie.secure = true; | ||
break; | ||
case 'httponly': | ||
cookie.httponly = true; | ||
break; | ||
case 'httponly': | ||
cookie.httponly = true; | ||
break; | ||
default: | ||
if (!cookie.name) { | ||
cookie.name = key; | ||
cookie.value = value; | ||
} | ||
} | ||
}); | ||
default: | ||
if (!cookie.name) { | ||
cookie.name = key; | ||
cookie.value = value; | ||
} | ||
} | ||
}); | ||
return cookie; | ||
} | ||
return cookie; | ||
}; | ||
/** | ||
/** | ||
* Checks if a cookie object is valid for a specified URL | ||
@@ -177,26 +173,29 @@ * | ||
*/ | ||
Biskviit.prototype.match = function(cookie, url) { | ||
var urlparts = urllib.parse(url || ''); | ||
match(cookie, url) { | ||
let urlparts = urllib.parse(url || ''); | ||
// check if hostname matches | ||
// .foo.com also matches subdomains, foo.com does not | ||
if (urlparts.hostname !== cookie.domain && (cookie.domain.charAt(0) !== '.' || ('.' + urlparts.hostname).substr(-cookie.domain.length) !== cookie.domain)) { | ||
return false; | ||
} | ||
// check if hostname matches | ||
// .foo.com also matches subdomains, foo.com does not | ||
if ( | ||
urlparts.hostname !== cookie.domain && | ||
(cookie.domain.charAt(0) !== '.' || ('.' + urlparts.hostname).substr(-cookie.domain.length) !== cookie.domain) | ||
) { | ||
return false; | ||
} | ||
// check if path matches | ||
let path = this.getPath(urlparts.pathname); | ||
if (path.substr(0, cookie.path.length) !== cookie.path) { | ||
return false; | ||
} | ||
// check if path matches | ||
let path = this.getPath(urlparts.pathname); | ||
if (path.substr(0, cookie.path.length) !== cookie.path) { | ||
return false; | ||
} | ||
// check secure argument | ||
if (cookie.secure && urlparts.protocol !== 'https:') { | ||
return false; | ||
// check secure argument | ||
if (cookie.secure && urlparts.protocol !== 'https:') { | ||
return false; | ||
} | ||
return true; | ||
} | ||
return true; | ||
}; | ||
/** | ||
/** | ||
* Adds (or updates/removes if needed) a cookie object to the cookie storage | ||
@@ -206,32 +205,31 @@ * | ||
*/ | ||
Biskviit.prototype.add = function(cookie) { | ||
// nothing to do here | ||
if (!cookie || !cookie.name) { | ||
return false; | ||
} | ||
add(cookie) { | ||
// nothing to do here | ||
if (!cookie || !cookie.name) { | ||
return false; | ||
} | ||
// overwrite if has same params | ||
for (let i = 0, len = this.cookies.length; i < len; i++) { | ||
if (this.compare(this.cookies[i], cookie)) { | ||
// overwrite if has same params | ||
for (let i = 0, len = this.cookies.length; i < len; i++) { | ||
if (this.compare(this.cookies[i], cookie)) { | ||
// check if the cookie needs to be removed instead | ||
if (this.isExpired(cookie)) { | ||
this.cookies.splice(i, 1); // remove expired/unset cookie | ||
return false; | ||
} | ||
// check if the cookie needs to be removed instead | ||
if (this.isExpired(cookie)) { | ||
this.cookies.splice(i, 1); // remove expired/unset cookie | ||
return false; | ||
this.cookies[i] = cookie; | ||
return true; | ||
} | ||
} | ||
this.cookies[i] = cookie; | ||
return true; | ||
// add as new if not already expired | ||
if (!this.isExpired(cookie)) { | ||
this.cookies.push(cookie); | ||
} | ||
} | ||
// add as new if not already expired | ||
if (!this.isExpired(cookie)) { | ||
this.cookies.push(cookie); | ||
return true; | ||
} | ||
return true; | ||
}; | ||
/** | ||
/** | ||
* Checks if two cookie objects are the same | ||
@@ -243,7 +241,7 @@ * | ||
*/ | ||
Biskviit.prototype.compare = function(a, b) { | ||
return a.name === b.name && a.path === b.path && a.domain === b.domain && a.secure === b.secure && a.httponly === a.httponly; | ||
}; | ||
compare(a, b) { | ||
return a.name === b.name && a.path === b.path && a.domain === b.domain && a.secure === b.secure && a.httponly === a.httponly; | ||
} | ||
/** | ||
/** | ||
* Checks if a cookie is expired | ||
@@ -254,7 +252,7 @@ * | ||
*/ | ||
Biskviit.prototype.isExpired = function(cookie) { | ||
return (cookie.expires && cookie.expires < new Date()) || !cookie.value; | ||
}; | ||
isExpired(cookie) { | ||
return (cookie.expires && cookie.expires < new Date()) || !cookie.value; | ||
} | ||
/** | ||
/** | ||
* Returns normalized cookie path for an URL path argument | ||
@@ -265,18 +263,21 @@ * | ||
*/ | ||
Biskviit.prototype.getPath = function(pathname) { | ||
var path = (pathname || '/').split('/'); | ||
path.pop(); // remove filename part | ||
path = path.join('/').trim(); | ||
getPath(pathname) { | ||
let path = (pathname || '/').split('/'); | ||
path.pop(); // remove filename part | ||
path = path.join('/').trim(); | ||
// ensure path prefix / | ||
if (path.charAt(0) !== '/') { | ||
path = '/' + path; | ||
} | ||
// ensure path prefix / | ||
if (path.charAt(0) !== '/') { | ||
path = '/' + path; | ||
} | ||
// ensure path suffix / | ||
if (path.substr(-1) !== '/') { | ||
path += '/'; | ||
// ensure path suffix / | ||
if (path.substr(-1) !== '/') { | ||
path += '/'; | ||
} | ||
return path; | ||
} | ||
} | ||
return path; | ||
}; | ||
module.exports = Biskviit; |
{ | ||
"name": "biskviit", | ||
"version": "1.0.1", | ||
"description": "Yet another module for http cookie handling", | ||
"main": "lib/biskviit.js", | ||
"scripts": { | ||
"test": "grunt" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/andris9/biskviit.git" | ||
}, | ||
"keywords": [ | ||
"HTTP", | ||
"cookie", | ||
"cookies" | ||
], | ||
"author": "Andris Reinman", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/andris9/biskviit/issues" | ||
}, | ||
"homepage": "https://github.com/andris9/biskviit#readme", | ||
"devDependencies": { | ||
"chai": "^3.2.0", | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-jshint": "^0.11.3", | ||
"grunt-mocha-test": "^0.12.7", | ||
"mocha": "^2.3.2" | ||
}, | ||
"dependencies": { | ||
"psl": "^1.1.7" | ||
}, | ||
"engines": { | ||
"node": ">=1.0.0" | ||
} | ||
} | ||
"name": "biskviit", | ||
"version": "2.0.0", | ||
"description": "Yet another module for http cookie handling", | ||
"main": "lib/biskviit.js", | ||
"scripts": { | ||
"test": "grunt" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/andris9/biskviit.git" | ||
}, | ||
"keywords": ["HTTP", "cookie", "cookies"], | ||
"author": "Andris Reinman", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/andris9/biskviit/issues" | ||
}, | ||
"homepage": "https://github.com/andris9/biskviit#readme", | ||
"devDependencies": { | ||
"chai": "^4.1.1", | ||
"eslint-config-nodemailer": "^1.2.0", | ||
"grunt": "^1.0.1", | ||
"grunt-cli": "^1.2.0", | ||
"grunt-eslint": "^20.0.0", | ||
"grunt-mocha-test": "^0.13.2", | ||
"mocha": "^3.5.0" | ||
}, | ||
"dependencies": { | ||
"psl": "^1.1.19" | ||
}, | ||
"engines": { | ||
"node": ">=1.0.0" | ||
} | ||
} |
# biskviit | ||
Yet another node module for handling http cookies. This module parses `Set-Cookie` header, stores the data to memory and returns valid value for `Cookie` header once needed based on the stored cookie data. | ||
> **NB** Requires iojs or Node v4+ to support some ES6 features used by this module. Might work with older Node versions as well but not tested | ||
Yet another node module for handling http cookies. This module parses `Set-Cookie` header, stores the data to memory and returns valid value for `Cookie` header based on the stored cookie data. | ||
> **NB** Requires Node.js v6+ | ||
## Usage | ||
@@ -15,3 +16,3 @@ | ||
```javascript | ||
var Biskviit = require('biskviit'); | ||
const Biskviit = require('biskviit'); | ||
``` | ||
@@ -22,3 +23,3 @@ | ||
```javascript | ||
var biskviit = new Biskviit(options); | ||
const biskviit = new Biskviit(options); | ||
``` | ||
@@ -34,4 +35,4 @@ | ||
```javascript | ||
var Biskviit = require('biskviit'); | ||
var biskviit = new Biskviit({ | ||
const Biskviit = require('biskviit'); | ||
const biskviit = new Biskviit({ | ||
sessionTimeout: 5 * 60 // expire cookies after 5 minutes | ||
@@ -66,3 +67,3 @@ }); | ||
```javascript | ||
var cookiesString = biskviit.get(url); | ||
const cookiesString = biskviit.get(url); | ||
``` | ||
@@ -77,3 +78,3 @@ | ||
```javascript | ||
var cookiesString = biskviit.get('http://example.com/'); | ||
const cookiesString = biskviit.get('http://example.com/'); | ||
// theme=light; sessionToken=abc123 | ||
@@ -87,3 +88,3 @@ ``` | ||
```javascript | ||
var cookiesString = biskviit.list(url); | ||
const cookiesString = biskviit.list(url); | ||
``` | ||
@@ -98,3 +99,3 @@ | ||
```javascript | ||
var cookiesString = biskviit.list('http://example.com/'); | ||
const cookiesString = biskviit.list('http://example.com/'); | ||
// [{key: 'theme', value: 'light', expires: ...}, {key: 'sessionToken', value: 'abc123', expires: ...}] | ||
@@ -105,2 +106,2 @@ ``` | ||
**MIT** | ||
**MIT** |
@@ -0,21 +1,23 @@ | ||
/* eslint no-unused-expressions:0 */ | ||
/* globals afterEach, beforeEach, describe, it */ | ||
'use strict'; | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
const chai = require('chai'); | ||
const expect = chai.expect; | ||
//var http = require('http'); | ||
var Biskviit = require('../lib/biskviit'); | ||
const Biskviit = require('../lib/biskviit'); | ||
chai.config.includeStack = true; | ||
describe('Biskviit Unit Tests', function() { | ||
var biskviit; | ||
describe('Biskviit Unit Tests', () => { | ||
let biskviit; | ||
beforeEach(function() { | ||
beforeEach(() => { | ||
biskviit = new Biskviit(); | ||
}); | ||
describe('#getPath', function() { | ||
it('should return root path', function() { | ||
describe('#getPath', () => { | ||
it('should return root path', () => { | ||
expect(biskviit.getPath('/')).to.equal('/'); | ||
@@ -26,106 +28,136 @@ expect(biskviit.getPath('')).to.equal('/'); | ||
it('should return without file', function() { | ||
it('should return without file', () => { | ||
expect(biskviit.getPath('/path/to/file')).to.equal('/path/to/'); | ||
}); | ||
}); | ||
describe('#isExpired', function() { | ||
it('should match expired cookie', function() { | ||
expect(biskviit.isExpired({ | ||
name: 'a', | ||
value: 'b', | ||
expires: new Date(Date.now() + 10000) | ||
})).to.be.false; | ||
describe('#isExpired', () => { | ||
it('should match expired cookie', () => { | ||
expect( | ||
biskviit.isExpired({ | ||
name: 'a', | ||
value: 'b', | ||
expires: new Date(Date.now() + 10000) | ||
}) | ||
).to.be.false; | ||
expect(biskviit.isExpired({ | ||
name: 'a', | ||
value: '', | ||
expires: new Date(Date.now() + 10000) | ||
})).to.be.true; | ||
expect( | ||
biskviit.isExpired({ | ||
name: 'a', | ||
value: '', | ||
expires: new Date(Date.now() + 10000) | ||
}) | ||
).to.be.true; | ||
expect(biskviit.isExpired({ | ||
name: 'a', | ||
value: 'b', | ||
expires: new Date(Date.now() - 10000) | ||
})).to.be.true; | ||
expect( | ||
biskviit.isExpired({ | ||
name: 'a', | ||
value: 'b', | ||
expires: new Date(Date.now() - 10000) | ||
}) | ||
).to.be.true; | ||
}); | ||
}); | ||
describe('#compare', function() { | ||
it('should match similar cookies', function() { | ||
expect(biskviit.compare({ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
}, { | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
})).to.be.true; | ||
describe('#compare', () => { | ||
it('should match similar cookies', () => { | ||
expect( | ||
biskviit.compare( | ||
{ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
}, | ||
{ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
} | ||
) | ||
).to.be.true; | ||
expect(biskviit.compare({ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
}, { | ||
name: 'yyy', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
})).to.be.false; | ||
expect( | ||
biskviit.compare( | ||
{ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
}, | ||
{ | ||
name: 'yyy', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
} | ||
) | ||
).to.be.false; | ||
expect(biskviit.compare({ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
}, { | ||
name: 'zzz', | ||
path: '/amp', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
})).to.be.false; | ||
expect( | ||
biskviit.compare( | ||
{ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
}, | ||
{ | ||
name: 'zzz', | ||
path: '/amp', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
} | ||
) | ||
).to.be.false; | ||
expect(biskviit.compare({ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
}, { | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'examples.com', | ||
secure: false, | ||
httponly: false | ||
})).to.be.false; | ||
expect( | ||
biskviit.compare( | ||
{ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
}, | ||
{ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'examples.com', | ||
secure: false, | ||
httponly: false | ||
} | ||
) | ||
).to.be.false; | ||
expect(biskviit.compare({ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
}, { | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: true, | ||
httponly: false | ||
})).to.be.false; | ||
expect( | ||
biskviit.compare( | ||
{ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: false, | ||
httponly: false | ||
}, | ||
{ | ||
name: 'zzz', | ||
path: '/', | ||
domain: 'example.com', | ||
secure: true, | ||
httponly: false | ||
} | ||
) | ||
).to.be.false; | ||
}); | ||
}); | ||
describe('#add', function() { | ||
it('should append new cookie', function() { | ||
describe('#add', () => { | ||
it('should append new cookie', () => { | ||
expect(biskviit.cookies.length).to.equal(0); | ||
@@ -146,3 +178,3 @@ biskviit.add({ | ||
it('should update existing cookie', function() { | ||
it('should update existing cookie', () => { | ||
expect(biskviit.cookies.length).to.equal(0); | ||
@@ -173,5 +205,5 @@ biskviit.add({ | ||
describe('#match', function() { | ||
it('should check if a cookie matches particular domain and path', function() { | ||
var cookie = { | ||
describe('#match', () => { | ||
it('should check if a cookie matches particular domain and path', () => { | ||
let cookie = { | ||
name: 'zzz', | ||
@@ -189,4 +221,4 @@ value: 'abc', | ||
it('should check if a cookie matches particular domain and path', function() { | ||
var cookie = { | ||
it('should check if a cookie matches particular domain and path', () => { | ||
let cookie = { | ||
name: 'zzz', | ||
@@ -204,4 +236,4 @@ value: 'abc', | ||
it('should check if a cookie is secure', function() { | ||
var cookie = { | ||
it('should check if a cookie is secure', () => { | ||
let cookie = { | ||
name: 'zzz', | ||
@@ -220,5 +252,4 @@ value: 'abc', | ||
describe('#parse', function() { | ||
it('should parse Set-Cookie value', function() { | ||
describe('#parse', () => { | ||
it('should parse Set-Cookie value', () => { | ||
expect(biskviit.parse('theme=plain')).to.deep.equal({ | ||
@@ -238,6 +269,5 @@ name: 'theme', | ||
}); | ||
}); | ||
it('should ignore invalid expire header', function() { | ||
it('should ignore invalid expire header', () => { | ||
expect(biskviit.parse('theme=plain; Expires=Wed, 13 Jan 2021 22:23:01 GMT')).to.deep.equal({ | ||
@@ -256,51 +286,6 @@ name: 'theme', | ||
describe('Listing', function() { | ||
beforeEach(function() { | ||
biskviit.cookies = [{ | ||
name: 'ssid1', | ||
value: 'Ap4P….GTEq1', | ||
domain: '.foo.com', | ||
path: '/', | ||
httponly: true, | ||
secure: true, | ||
expires: new Date('Wed, 13 Jan 2021 22:23:01 GMT') | ||
}, { | ||
name: 'ssid2', | ||
value: 'Ap4P….GTEq2', | ||
domain: '.foo.com', | ||
path: '/', | ||
httponly: true, | ||
secure: true, | ||
expires: new Date('Wed, 13 Jan 1900 22:23:01 GMT') | ||
}, { | ||
name: 'ssid3', | ||
value: 'Ap4P….GTEq3', | ||
domain: 'foo.com', | ||
path: '/', | ||
httponly: true, | ||
secure: true, | ||
expires: new Date('Wed, 13 Jan 2021 22:23:01 GMT') | ||
}, { | ||
name: 'ssid4', | ||
value: 'Ap4P….GTEq4', | ||
domain: 'www.foo.com', | ||
path: '/', | ||
httponly: true, | ||
secure: true, | ||
expires: new Date('Wed, 13 Jan 2021 22:23:01 GMT') | ||
}, { | ||
name: 'ssid5', | ||
value: 'Ap4P….GTEq5', | ||
domain: 'broo.com', | ||
path: '/', | ||
httponly: true, | ||
secure: true, | ||
expires: new Date('Wed, 13 Jan 2021 22:23:01 GMT') | ||
}]; | ||
}); | ||
describe('#list', function() { | ||
it('should return matching cookies for an URL', function() { | ||
expect(biskviit.list('https://www.foo.com')).to.deep.equal([{ | ||
describe('Listing', () => { | ||
beforeEach(() => { | ||
biskviit.cookies = [ | ||
{ | ||
name: 'ssid1', | ||
@@ -313,3 +298,22 @@ value: 'Ap4P….GTEq1', | ||
expires: new Date('Wed, 13 Jan 2021 22:23:01 GMT') | ||
}, { | ||
}, | ||
{ | ||
name: 'ssid2', | ||
value: 'Ap4P….GTEq2', | ||
domain: '.foo.com', | ||
path: '/', | ||
httponly: true, | ||
secure: true, | ||
expires: new Date('Wed, 13 Jan 1900 22:23:01 GMT') | ||
}, | ||
{ | ||
name: 'ssid3', | ||
value: 'Ap4P….GTEq3', | ||
domain: 'foo.com', | ||
path: '/', | ||
httponly: true, | ||
secure: true, | ||
expires: new Date('Wed, 13 Jan 2021 22:23:01 GMT') | ||
}, | ||
{ | ||
name: 'ssid4', | ||
@@ -322,8 +326,42 @@ value: 'Ap4P….GTEq4', | ||
expires: new Date('Wed, 13 Jan 2021 22:23:01 GMT') | ||
}]); | ||
}, | ||
{ | ||
name: 'ssid5', | ||
value: 'Ap4P….GTEq5', | ||
domain: 'broo.com', | ||
path: '/', | ||
httponly: true, | ||
secure: true, | ||
expires: new Date('Wed, 13 Jan 2021 22:23:01 GMT') | ||
} | ||
]; | ||
}); | ||
describe('#list', () => { | ||
it('should return matching cookies for an URL', () => { | ||
expect(biskviit.list('https://www.foo.com')).to.deep.equal([ | ||
{ | ||
name: 'ssid1', | ||
value: 'Ap4P….GTEq1', | ||
domain: '.foo.com', | ||
path: '/', | ||
httponly: true, | ||
secure: true, | ||
expires: new Date('Wed, 13 Jan 2021 22:23:01 GMT') | ||
}, | ||
{ | ||
name: 'ssid4', | ||
value: 'Ap4P….GTEq4', | ||
domain: 'www.foo.com', | ||
path: '/', | ||
httponly: true, | ||
secure: true, | ||
expires: new Date('Wed, 13 Jan 2021 22:23:01 GMT') | ||
} | ||
]); | ||
}); | ||
}); | ||
describe('#get', function() { | ||
it('should return matching cookies for an URL', function() { | ||
describe('#get', () => { | ||
it('should return matching cookies for an URL', () => { | ||
expect(biskviit.get('https://www.foo.com')).to.equal('ssid1=Ap4P….GTEq1; ssid4=Ap4P….GTEq4'); | ||
@@ -334,4 +372,4 @@ }); | ||
describe('#set', function() { | ||
it('should set cookie', function() { | ||
describe('#set', () => { | ||
it('should set cookie', () => { | ||
// short | ||
@@ -348,47 +386,62 @@ biskviit.set('theme=plain', 'https://foo.com/'); | ||
biskviit.set('invalid_3=date; Expires=zzzz', 'https://foo.com/'); | ||
// invalid tld | ||
biskviit.set('invalid_4=cors; domain=.co.uk', 'https://foo.co.uk/'); | ||
// should not be added | ||
biskviit.set('expired_1=date; Expires=1999-01-01 01:01:01 GMT', 'https://foo.com/'); | ||
expect(biskviit.cookies.map(function(cookie) { | ||
delete cookie.expires; | ||
return cookie; | ||
})).to.deep.equal([{ | ||
name: 'theme', | ||
value: 'plain', | ||
domain: 'foo.com', | ||
path: '/' | ||
}, { | ||
name: 'ssid', | ||
value: 'Ap4P….GTEq', | ||
domain: 'foo.com', | ||
path: '/test', | ||
secure: true, | ||
httponly: true | ||
}, { | ||
name: 'ssid', | ||
value: 'Ap4P….GTEq', | ||
domain: 'www.foo.com', | ||
path: '/', | ||
secure: true, | ||
httponly: true | ||
}, { | ||
name: 'invalid_1', | ||
value: 'cors', | ||
domain: 'foo.com', | ||
path: '/' | ||
}, { | ||
name: 'invalid_2', | ||
value: 'cors', | ||
domain: 'foo.com', | ||
path: '/' | ||
}, { | ||
name: 'invalid_3', | ||
value: 'date', | ||
domain: 'foo.com', | ||
path: '/' | ||
}]); | ||
expect( | ||
biskviit.cookies.map(cookie => { | ||
delete cookie.expires; | ||
return cookie; | ||
}) | ||
).to.deep.equal([ | ||
{ | ||
name: 'theme', | ||
value: 'plain', | ||
domain: 'foo.com', | ||
path: '/' | ||
}, | ||
{ | ||
name: 'ssid', | ||
value: 'Ap4P….GTEq', | ||
domain: 'foo.com', | ||
path: '/test', | ||
secure: true, | ||
httponly: true | ||
}, | ||
{ | ||
name: 'ssid', | ||
value: 'Ap4P….GTEq', | ||
domain: 'www.foo.com', | ||
path: '/', | ||
secure: true, | ||
httponly: true | ||
}, | ||
{ | ||
name: 'invalid_1', | ||
value: 'cors', | ||
domain: 'foo.com', | ||
path: '/' | ||
}, | ||
{ | ||
name: 'invalid_2', | ||
value: 'cors', | ||
domain: 'foo.com', | ||
path: '/' | ||
}, | ||
{ | ||
name: 'invalid_3', | ||
value: 'date', | ||
domain: 'foo.com', | ||
path: '/' | ||
}, | ||
{ | ||
name: 'invalid_4', | ||
value: 'cors', | ||
domain: 'foo.co.uk', | ||
path: '/' | ||
} | ||
]); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27637
657
100
7
Updatedpsl@^1.1.19