2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
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
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.cookies.set()', () => {
|
|
|
|
it('should set an unsigned cookie', done => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.cookies.set('name', 'jon');
|
|
|
|
ctx.status = 204;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-11-15 18:03:40 +00:00
|
|
|
|
|
|
|
request(server)
|
2015-10-28 07:53:49 +00:00
|
|
|
.get('/')
|
|
|
|
.expect(204)
|
2015-10-25 07:54:57 +00:00
|
|
|
.end((err, res) => {
|
2015-10-28 07:53:49 +00:00
|
|
|
if (err) return done(err);
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-28 07:53:49 +00:00
|
|
|
res.headers['set-cookie'].some(cookie => /^name=/.test(cookie)).should.be.ok;
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-28 07:53:49 +00:00
|
|
|
done();
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('with .signed', () => {
|
|
|
|
describe('when no .keys are set', () => {
|
|
|
|
it('should error', done => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2013-11-15 18:03:40 +00:00
|
|
|
try {
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.cookies.set('foo', 'bar', { signed: true });
|
2013-11-15 18:03:40 +00:00
|
|
|
} catch (err) {
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.body = err.message;
|
2013-11-15 18:03:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app.listen())
|
2015-10-28 07:53:49 +00:00
|
|
|
.get('/')
|
|
|
|
.expect('.keys required for signed cookies', done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should send a signed cookie', done => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-11-15 18:03:40 +00:00
|
|
|
|
|
|
|
app.keys = ['a', 'b'];
|
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.cookies.set('name', 'jon', { signed: true });
|
|
|
|
ctx.status = 204;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-11-15 18:03:40 +00:00
|
|
|
|
|
|
|
request(server)
|
2015-10-28 07:53:49 +00:00
|
|
|
.get('/')
|
|
|
|
.expect(204)
|
2015-10-25 07:54:57 +00:00
|
|
|
.end((err, res) => {
|
2015-10-28 07:53:49 +00:00
|
|
|
if (err) return done(err);
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-28 07:53:49 +00:00
|
|
|
const cookies = res.headers['set-cookie'];
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-28 07:53:49 +00:00
|
|
|
cookies.some(cookie => /^name=/.test(cookie)).should.be.ok;
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-28 07:53:49 +00:00
|
|
|
cookies.some(cookie => /^name\.sig=/.test(cookie)).should.be.ok;
|
2013-11-15 18:03:40 +00:00
|
|
|
|
2015-10-28 07:53:49 +00:00
|
|
|
done();
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|