koa-lite/test/context/cookies.js

123 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2013-11-15 18:03:40 +00:00
'use strict';
2013-11-15 18:03:40 +00:00
var request = require('supertest');
2014-11-16 15:18:37 +00:00
var koa = require('../..');
2013-11-15 18:03:40 +00:00
describe('ctx.cookies.set()', function(){
it('should set an unsigned cookie', function(done){
var app = koa();
app.use(function *(next){
this.cookies.set('name', 'jon');
this.status = 204;
})
var server = app.listen();
request(server)
.get('/')
.expect(204)
.end(function(err, res){
if (err) return done(err);
res.headers['set-cookie'].some(function(cookie){
return /^name=/.test(cookie);
}).should.be.ok;
done();
})
})
describe('with .signed', function(){
describe('when no .keys are set', function(){
it('should error', function(done){
var app = koa();
app.use(function *(next){
try {
this.cookies.set('foo', 'bar', { signed: true });
} catch (err) {
this.body = err.message;
}
});
request(app.listen())
.get('/')
2013-11-16 01:07:45 +00:00
.expect('.keys required for signed cookies', done);
2013-11-15 18:03:40 +00:00
})
})
it('should send a signed cookie', function(done){
var app = koa();
app.keys = ['a', 'b'];
app.use(function *(next){
this.cookies.set('name', 'jon', { signed: true });
this.status = 204;
})
var server = app.listen();
request(server)
.get('/')
.expect(204)
.end(function(err, res){
if (err) return done(err);
var cookies = res.headers['set-cookie'];
cookies.some(function(cookie){
return /^name=/.test(cookie);
}).should.be.ok;
cookies.some(function(cookie){
return /^name\.sig=/.test(cookie);
}).should.be.ok;
done();
})
})
})
2015-12-02 15:13:01 +00:00
describe('with secure', function(){
it('should get secure from request', function(done){
var app = koa();
app.proxy = true;
app.keys = ['a', 'b'];
app.use(function *(next){
this.cookies.set('name', 'jon', { signed: true });
this.status = 204;
})
var server = app.listen();
request(server)
.get('/')
.set('x-forwarded-proto', 'https') // mock secure
.expect(204)
.end(function(err, res){
if (err) return done(err);
var cookies = res.headers['set-cookie'];
cookies.some(function(cookie){
return /^name=/.test(cookie);
}).should.be.ok;
cookies.some(function(cookie){
return /^name\.sig=/.test(cookie);
}).should.be.ok;
cookies.every(function(cookie){
return /secure/.test(cookie);
}).should.be.ok;
done();
})
})
})
2014-11-16 13:52:14 +00:00
})