Skip to content

Instantly share code, notes, and snippets.

@chrisveness
Created February 20, 2015 00:49
Show Gist options
  • Save chrisveness/9b6ff01ddaef8f5e3aa4 to your computer and use it in GitHub Desktop.
Save chrisveness/9b6ff01ddaef8f5e3aa4 to your computer and use it in GitHub Desktop.
supertest fails when using cookie domain / header host
var koa = require('koa');
var session = require('koa-session');
var app = module.exports = koa();
app.keys = ['some secret hurr'];
app.use(session({ domain: '.app.localhost' }, app)); // THIS WORKS IN BROWSER BUT FAILS IN SUPERTEST
//app.use(session(app)); // THIS WORKS EITHER WAY
app.use(function*() {
var n = this.session.views || 0;
this.session.views = ++n;
this.body = n + ' views';
console.log(this.host, n);
});
app.listen(3000);
console.log('listening on port 3000');
{
"name": "supertest-session-test",
"version": "0.0.0",
"main": "app.js",
"scripts": {
"start": "node --harmony app.js",
"test": "mocha --harmony test.js"
},
"dependencies": {
"koa": "^0.18.0",
"koa-session": "^3.1.0"
},
"devDependencies": {
"chai": "^2.0.0",
"mocha": "^2.1.0",
"supertest": "^0.15.0"
}
}
var request = require('supertest');
var expect = require('chai').expect;
var app = require('./app.js');
request = request.agent(app.listen());
describe('session test', function() {
it('gets first view', function(done) {
request
.get('/')
.set({ Host: 'www.app.localhost' })
.end(function(err, result) {
expect(result.text).to.equal('1 views');
done();
});
});
it('gets second view', function(done) {
request
.get('/')
.set({ Host: 'www.app.localhost' })
.end(function(err, result) {
expect(result.text).to.equal('2 views');
done();
});
});
});
@chrisveness
Copy link
Author

This fixes it:

var cookieOpt = module.parent ? { } : { domain: '.app.localhost' };
app.use(session(cookieOpt, app));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment