| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const request = require('supertest'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 15:47:17 +02:00
										 |  |  | const Koa = require('../..').server; | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('ctx.cookies.set()', function () { | 
					
						
							|  |  |  |   it('should set an unsigned cookie', function (done) { | 
					
						
							| 
									
										
										
										
											2015-10-16 15:47:17 +02:00
										 |  |  |     const app = new Koa(); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     app.use(function * () { | 
					
						
							|  |  |  |       this.cookies.set('name', 'jon'); | 
					
						
							|  |  |  |       this.status = 204; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 15:47:17 +02:00
										 |  |  |     const server = app.listen(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     request(server) | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |       .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) { | 
					
						
							| 
									
										
										
										
											2015-10-16 15:47:17 +02:00
										 |  |  |         const app = new Koa(); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         app.use(function * () { | 
					
						
							|  |  |  |           try { | 
					
						
							|  |  |  |             this.cookies.set('foo', 'bar', { | 
					
						
							|  |  |  |               signed: true | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |           } catch (err) { | 
					
						
							|  |  |  |             this.body = err.message; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         request(app.listen()) | 
					
						
							|  |  |  |           .get('/') | 
					
						
							|  |  |  |           .expect('.keys required for signed cookies', done); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should send a signed cookie', function (done) { | 
					
						
							| 
									
										
										
										
											2015-10-16 15:47:17 +02:00
										 |  |  |       const app = new Koa(); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       app.keys = ['a', 'b']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       app.use(function * () { | 
					
						
							|  |  |  |         this.cookies.set('name', 'jon', { | 
					
						
							|  |  |  |           signed: true | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         this.status = 204; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 15:47:17 +02:00
										 |  |  |       const server = app.listen(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       request(server) | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         .get('/') | 
					
						
							|  |  |  |         .expect(204) | 
					
						
							|  |  |  |         .end(function (err, res) { | 
					
						
							|  |  |  |           if (err) { | 
					
						
							|  |  |  |             return done(err); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           const 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(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |