| 
									
										
										
										
											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.onerror(err)', function () { | 
					
						
							|  |  |  |   it('should respond', 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.body = 'something else'; | 
					
						
							|  |  |  |       this.throw(418, 'boom'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 15:47:17 +02:00
										 |  |  |     const server = app.listen(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     request(server) | 
					
						
							|  |  |  |     .get('/') | 
					
						
							|  |  |  |     .expect(418) | 
					
						
							|  |  |  |     .expect('Content-Type', 'text/plain; charset=utf-8') | 
					
						
							|  |  |  |     .expect('Content-Length', '4') | 
					
						
							|  |  |  |     .end(done); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should unset all headers', 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.set('Vary', 'Accept-Encoding'); | 
					
						
							|  |  |  |       this.set('X-CSRF-Token', 'asdf'); | 
					
						
							|  |  |  |       this.body = 'response'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       this.throw(418, 'boom'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 15:47:17 +02:00
										 |  |  |     const server = app.listen(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     request(server) | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |       .get('/') | 
					
						
							|  |  |  |       .expect(418) | 
					
						
							|  |  |  |       .expect('Content-Type', 'text/plain; charset=utf-8') | 
					
						
							|  |  |  |       .expect('Content-Length', '4') | 
					
						
							|  |  |  |       .end(function (err, res) { | 
					
						
							|  |  |  |         if (err) { | 
					
						
							|  |  |  |           return done(err); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.headers.should.not.have.property('vary'); | 
					
						
							|  |  |  |         res.headers.should.not.have.property('x-csrf-token'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('when invalid err.status', function () { | 
					
						
							|  |  |  |     describe('not number', function () { | 
					
						
							|  |  |  |       it('should respond 500', 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.body = 'something else'; | 
					
						
							|  |  |  |           const err = new Error('some error'); | 
					
						
							|  |  |  |           err.status = 'notnumber'; | 
					
						
							|  |  |  |           throw err; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 15:47:17 +02:00
										 |  |  |         const server = app.listen(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         request(server) | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           .get('/') | 
					
						
							|  |  |  |           .expect(500) | 
					
						
							|  |  |  |           .expect('Content-Type', 'text/plain; charset=utf-8') | 
					
						
							|  |  |  |           .expect('Internal Server Error', done); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('not http status code', function () { | 
					
						
							|  |  |  |       it('should respond 500', 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.body = 'something else'; | 
					
						
							|  |  |  |           const err = new Error('some error'); | 
					
						
							|  |  |  |           err.status = 9999; | 
					
						
							|  |  |  |           throw err; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 15:47:17 +02:00
										 |  |  |         const server = app.listen(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         request(server) | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           .get('/') | 
					
						
							|  |  |  |           .expect(500) | 
					
						
							|  |  |  |           .expect('Content-Type', 'text/plain; charset=utf-8') | 
					
						
							|  |  |  |           .expect('Internal Server Error', done); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('when non-error thrown', function () { | 
					
						
							|  |  |  |     it('should response non-error thrown message', 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 * () { | 
					
						
							|  |  |  |         throw 'string error'; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 15:47:17 +02:00
										 |  |  |       const server = app.listen(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       request(server) | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         .get('/') | 
					
						
							|  |  |  |         .expect(500) | 
					
						
							|  |  |  |         .expect('Content-Type', 'text/plain; charset=utf-8') | 
					
						
							|  |  |  |         .expect('Internal Server Error', done); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |