Created
          February 20, 2015 00:49 
        
      - 
      
 - 
        
Save chrisveness/9b6ff01ddaef8f5e3aa4 to your computer and use it in GitHub Desktop.  
    supertest fails when using cookie domain / header host
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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'); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | { | |
| "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" | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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(); | |
| }); | |
| }); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
This fixes it: