2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
const assert = require('assert');
|
2015-10-05 18:23:47 +00:00
|
|
|
const request = require('supertest');
|
2015-10-13 06:19:42 +00:00
|
|
|
const Koa = require('../..');
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
describe('ctx.cookies', () => {
|
|
|
|
describe('ctx.cookies.set()', () => {
|
2019-06-26 03:15:22 +00:00
|
|
|
it('should set an unsigned cookie', async() => {
|
2018-07-11 03:18:39 +00:00
|
|
|
const app = new Koa();
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
app.use((ctx, next) => {
|
|
|
|
ctx.cookies.set('name', 'jon');
|
|
|
|
ctx.status = 204;
|
|
|
|
});
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
const server = app.listen();
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
const res = await request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(204);
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
const cookie = res.headers['set-cookie'].some(cookie => /^name=/.test(cookie));
|
|
|
|
assert.equal(cookie, true);
|
|
|
|
});
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
describe('with .signed', () => {
|
|
|
|
describe('when no .keys are set', () => {
|
|
|
|
it('should error', () => {
|
|
|
|
const app = new Koa();
|
|
|
|
|
|
|
|
app.use((ctx, next) => {
|
|
|
|
try {
|
|
|
|
ctx.cookies.set('foo', 'bar', { signed: true });
|
|
|
|
} catch (err) {
|
|
|
|
ctx.body = err.message;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return request(app.callback())
|
|
|
|
.get('/')
|
|
|
|
.expect('.keys required for signed cookies');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-06-26 03:15:22 +00:00
|
|
|
it('should send a signed cookie', async() => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
app.keys = ['a', 'b'];
|
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2018-07-11 03:18:39 +00:00
|
|
|
ctx.cookies.set('name', 'jon', { signed: true });
|
|
|
|
ctx.status = 204;
|
2013-11-15 18:03:40 +00:00
|
|
|
});
|
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
const server = app.listen();
|
|
|
|
|
|
|
|
const res = await request(server)
|
2015-10-28 07:53:49 +00:00
|
|
|
.get('/')
|
2018-07-11 03:18:39 +00:00
|
|
|
.expect(204);
|
|
|
|
|
|
|
|
const cookies = res.headers['set-cookie'];
|
|
|
|
|
|
|
|
assert.equal(cookies.some(cookie => /^name=/.test(cookie)), true);
|
|
|
|
assert.equal(cookies.some(cookie => /(,|^)name\.sig=/.test(cookie)), true);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
describe('with secure', () => {
|
2019-06-26 03:15:22 +00:00
|
|
|
it('should get secure from request', async() => {
|
2018-07-11 03:18:39 +00:00
|
|
|
const app = new Koa();
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
app.proxy = true;
|
|
|
|
app.keys = ['a', 'b'];
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
app.use(ctx => {
|
|
|
|
ctx.cookies.set('name', 'jon', { signed: true });
|
|
|
|
ctx.status = 204;
|
|
|
|
});
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
const server = app.listen();
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
const res = await request(server)
|
|
|
|
.get('/')
|
|
|
|
.set('x-forwarded-proto', 'https') // mock secure
|
|
|
|
.expect(204);
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
const cookies = res.headers['set-cookie'];
|
|
|
|
assert.equal(cookies.some(cookie => /^name=/.test(cookie)), true);
|
|
|
|
assert.equal(cookies.some(cookie => /(,|^)name\.sig=/.test(cookie)), true);
|
|
|
|
assert.equal(cookies.every(cookie => /secure/.test(cookie)), true);
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2015-12-02 15:13:01 +00:00
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
describe('ctx.cookies=', () => {
|
2019-06-26 03:15:22 +00:00
|
|
|
it('should override cookie work', async() => {
|
2015-12-02 15:13:01 +00:00
|
|
|
const app = new Koa();
|
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
app.use((ctx, next) => {
|
|
|
|
ctx.cookies = {
|
|
|
|
set(key, value){
|
|
|
|
ctx.set(key, value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ctx.cookies.set('name', 'jon');
|
2015-12-02 15:13:01 +00:00
|
|
|
ctx.status = 204;
|
|
|
|
});
|
|
|
|
|
|
|
|
const server = app.listen();
|
|
|
|
|
2018-07-11 03:18:39 +00:00
|
|
|
await request(server)
|
2017-05-11 03:30:32 +00:00
|
|
|
.get('/')
|
2018-07-11 03:18:39 +00:00
|
|
|
.expect('name', 'jon')
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect(204);
|
2015-12-02 15:13:01 +00:00
|
|
|
});
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|