2018-06-25 17:42:50 +00:00
|
|
|
const { CookieJar } = require('tough-cookie');
|
|
|
|
const { cookieToString, jarFromCookies, cookiesFromJar } = require('..');
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
describe('jarFromCookies()', () => {
|
|
|
|
it('returns valid cookies', done => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const jar = jarFromCookies([
|
|
|
|
{
|
|
|
|
key: 'foo',
|
|
|
|
value: 'bar',
|
|
|
|
domain: 'google.com'
|
|
|
|
}
|
|
|
|
]);
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
jar.store.getAllCookies((err, cookies) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(cookies[0].domain).toEqual('google.com');
|
|
|
|
expect(cookies[0].key).toEqual('foo');
|
|
|
|
expect(cookies[0].value).toEqual('bar');
|
|
|
|
expect(cookies[0].creation instanceof Date).toEqual(true);
|
|
|
|
expect(cookies[0].expires).toEqual('Infinity');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles malformed JSON', () => {
|
|
|
|
const jar = jarFromCookies('not a jar');
|
|
|
|
expect(jar.constructor.name).toBe('CookieJar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('cookiesFromJar()', () => {
|
|
|
|
it('returns valid jar', async () => {
|
|
|
|
const d = new Date();
|
2018-06-25 17:42:50 +00:00
|
|
|
const initialCookies = [
|
|
|
|
{
|
|
|
|
key: 'bar',
|
|
|
|
value: 'baz',
|
|
|
|
domain: 'insomnia.rest',
|
|
|
|
expires: d
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// This one will fail to parse, and be skipped
|
|
|
|
bad: 'cookie'
|
|
|
|
}
|
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const jar = CookieJar.fromJSON({ cookies: initialCookies });
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const cookies = await cookiesFromJar(jar);
|
|
|
|
|
|
|
|
expect(cookies[0].domain).toBe('insomnia.rest');
|
|
|
|
expect(cookies[0].key).toBe('bar');
|
|
|
|
expect(cookies[0].value).toBe('baz');
|
2018-06-25 17:42:50 +00:00
|
|
|
expect(cookies[0].creation).toMatch(
|
|
|
|
/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/
|
|
|
|
);
|
2017-11-26 20:45:40 +00:00
|
|
|
expect(cookies[0].expires).toEqual(d.toISOString());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles bad jar', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const jar = CookieJar.fromJSON({ cookies: [] });
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
// MemoryStore never actually throws errors, so lets mock the
|
|
|
|
// function to force it to this time.
|
|
|
|
jar.store.getAllCookies = cb => cb(new Error('Dummy Error'));
|
|
|
|
const cookies = await cookiesFromJar(jar);
|
|
|
|
|
|
|
|
// Cookies failed to p
|
|
|
|
expect(cookies.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|