Skip to content

Commit

Permalink
fix cookies' secure detect
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Dec 2, 2015
1 parent 9f80296 commit abf666c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ app.createContext = function(req, res){
response.request = request;
context.onerror = context.onerror.bind(context);
context.originalUrl = request.originalUrl = req.url;
context.cookies = new Cookies(req, res, this.keys);
// pass `Request` because of `Cookies` need to read `request.protocol`
context.cookies = new Cookies(request, res, this.keys);
context.accept = request.accept = accepts(req);
context.state = {};
return context;
Expand Down
1 change: 1 addition & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ delegate(proto, 'request')
.access('querystring')
.access('idempotent')
.access('socket')
.access('connection')
.access('search')
.access('method')
.access('query')
Expand Down
10 changes: 10 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ module.exports = {
return this.req.socket;
},

/**
* Return the request connection.
*
* @return {connection}
* @api public
*/
get connection() {
return this.req.connection;
},

/**
* Get the charset when present or undefined.
*
Expand Down
2 changes: 1 addition & 1 deletion test/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var koa = require('..');

exports = module.exports = function(req, res){
var socket = new Stream.Duplex();
req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype };
req = req || { headers: {}, socket: socket, connection: socket, __proto__: Stream.Readable.prototype };
res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype };
res.getHeader = function(k){ return res._headers[k.toLowerCase()] };
res.setHeader = function(k, v){ res._headers[k.toLowerCase()] = v };
Expand Down
41 changes: 41 additions & 0 deletions test/context/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,45 @@ describe('ctx.cookies.set()', function(){
})
})
})

describe('with secure', function(){
it('should get secure from request', function(done){
var app = koa();

app.keys = ['a', 'b'];
app.proxy = true;

app.use(function *(next){
// mock secure
this.req.headers['x-forwarded-proto'] = 'https';
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;

cookies.every(function(cookie){
return /secure/.test(cookie);
}).should.be.ok;

done();
})
})
})
})
12 changes: 12 additions & 0 deletions test/request/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

'use strict';

var request = require('../context').request;
var Stream = require('stream');

describe('req.connection', function(){
it('should return the request connection object', function(){
var req = request();
req.connection.should.be.instanceOf(Stream);
})
})

0 comments on commit abf666c

Please sign in to comment.