fix cookies' secure detect
This commit is contained in:
parent
bcada5bde9
commit
39f058e11c
3 changed files with 39 additions and 1 deletions
|
@ -145,7 +145,10 @@ module.exports = class Application extends Emitter {
|
||||||
response.request = request;
|
response.request = request;
|
||||||
context.onerror = context.onerror.bind(context);
|
context.onerror = context.onerror.bind(context);
|
||||||
context.originalUrl = request.originalUrl = req.url;
|
context.originalUrl = request.originalUrl = req.url;
|
||||||
context.cookies = new Cookies(req, res, this.keys);
|
context.cookies = new Cookies(req, res, {
|
||||||
|
keys: this.keys,
|
||||||
|
secure: request.secure
|
||||||
|
});
|
||||||
context.accept = request.accept = accepts(req);
|
context.accept = request.accept = accepts(req);
|
||||||
context.state = {};
|
context.state = {};
|
||||||
return context;
|
return context;
|
||||||
|
|
|
@ -74,4 +74,37 @@ describe('ctx.cookies.set()', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('with secure', () => {
|
||||||
|
it('should get secure from request', done => {
|
||||||
|
const app = new Koa();
|
||||||
|
|
||||||
|
app.proxy = true;
|
||||||
|
app.keys = ['a', 'b'];
|
||||||
|
|
||||||
|
app.use(ctx => {
|
||||||
|
ctx.cookies.set('name', 'jon', { signed: true });
|
||||||
|
ctx.status = 204;
|
||||||
|
});
|
||||||
|
|
||||||
|
const server = app.listen();
|
||||||
|
|
||||||
|
request(server)
|
||||||
|
.get('/')
|
||||||
|
.set('x-forwarded-proto', 'https') // mock secure
|
||||||
|
.expect(204)
|
||||||
|
.end((err, res) => {
|
||||||
|
if (err) return done(err);
|
||||||
|
|
||||||
|
const cookies = res.headers['set-cookie'];
|
||||||
|
cookies.some(cookie => /^name=/.test(cookie)).should.be.ok;
|
||||||
|
|
||||||
|
cookies.some(cookie => /^name\.sig=/.test(cookie)).should.be.ok;
|
||||||
|
|
||||||
|
cookies.every(cookie => /secure/.test(cookie)).should.be.ok;
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,6 +8,8 @@ module.exports = (req, res) => {
|
||||||
const socket = new Stream.Duplex();
|
const socket = new Stream.Duplex();
|
||||||
req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype };
|
req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype };
|
||||||
res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype };
|
res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype };
|
||||||
|
req.socket = req.socket || socket;
|
||||||
|
res.socket = res.socket || socket;
|
||||||
res.getHeader = k => res._headers[k.toLowerCase()];
|
res.getHeader = k => res._headers[k.toLowerCase()];
|
||||||
res.setHeader = (k, v) => res._headers[k.toLowerCase()] = v;
|
res.setHeader = (k, v) => res._headers[k.toLowerCase()] = v;
|
||||||
res.removeHeader = (k, v) => delete res._headers[k.toLowerCase()];
|
res.removeHeader = (k, v) => delete res._headers[k.toLowerCase()];
|
||||||
|
|
Loading…
Reference in a new issue