2013-08-17 07:15:57 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const request = require('supertest');
|
|
|
|
const statuses = require('statuses');
|
|
|
|
const assert = require('assert');
|
2015-10-13 06:19:42 +00:00
|
|
|
const Koa = require('../..');
|
2015-10-05 18:23:47 +00:00
|
|
|
const fs = require('fs');
|
2014-09-08 22:53:46 +00:00
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
describe('app.respond', function(){
|
2014-01-24 22:38:40 +00:00
|
|
|
describe('when this.respond === false', function(){
|
|
|
|
it('should bypass app.respond', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-01-24 22:38:40 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.body = 'Hello';
|
|
|
|
this.respond = false;
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = this.res;
|
2014-04-29 04:17:30 +00:00
|
|
|
res.statusCode = 200;
|
2014-03-24 18:21:15 +00:00
|
|
|
setImmediate(function(){
|
2014-01-24 22:38:40 +00:00
|
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
|
|
res.end('lol');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-01-24 22:38:40 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-01-24 22:38:40 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(200)
|
|
|
|
.expect('lol')
|
|
|
|
.end(done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-01-24 22:38:40 +00:00
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
describe('when HEAD is used', function(){
|
|
|
|
it('should not respond with the body', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(){
|
|
|
|
this.body = 'Hello';
|
2013-08-17 07:15:57 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.head('/')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
2014-04-15 02:06:32 +00:00
|
|
|
res.should.have.header('Content-Type', 'text/plain; charset=utf-8');
|
2013-08-17 07:15:57 +00:00
|
|
|
res.should.have.header('Content-Length', '5');
|
|
|
|
assert(0 == res.text.length);
|
|
|
|
done();
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-19 19:11:26 +00:00
|
|
|
|
2014-04-15 15:37:48 +00:00
|
|
|
it('should keep json headers', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-15 02:06:32 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.body = { hello: 'world' };
|
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-15 02:06:32 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.head('/')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
2014-06-04 04:44:25 +00:00
|
|
|
res.should.have.header('Content-Type', 'application/json; charset=utf-8');
|
2014-04-15 02:06:32 +00:00
|
|
|
res.should.have.header('Content-Length', '17');
|
|
|
|
assert(0 == res.text.length);
|
|
|
|
done();
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-15 02:06:32 +00:00
|
|
|
|
2014-04-15 15:37:48 +00:00
|
|
|
it('should keep string headers', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-15 15:37:48 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.body = 'hello world';
|
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-15 15:37:48 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.head('/')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
|
|
|
res.should.have.header('Content-Type', 'text/plain; charset=utf-8');
|
|
|
|
res.should.have.header('Content-Length', '11');
|
|
|
|
assert(0 == res.text.length);
|
|
|
|
done();
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-15 15:37:48 +00:00
|
|
|
|
2014-04-15 15:38:32 +00:00
|
|
|
it('should keep buffer headers', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-15 15:38:32 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.body = new Buffer('hello world');
|
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-15 15:38:32 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.head('/')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
|
|
|
res.should.have.header('Content-Type', 'application/octet-stream');
|
|
|
|
res.should.have.header('Content-Length', '11');
|
|
|
|
assert(0 == res.text.length);
|
|
|
|
done();
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-15 15:38:32 +00:00
|
|
|
|
2013-11-19 19:11:26 +00:00
|
|
|
it('should respond with a 404 if no body was set', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-11-19 19:11:26 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
2013-12-30 06:26:19 +00:00
|
|
|
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-19 19:11:26 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-11-19 19:11:26 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.head('/')
|
|
|
|
.expect(404, done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-19 19:11:26 +00:00
|
|
|
|
|
|
|
it('should respond with a 200 if body = ""', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-11-19 19:11:26 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.body = '';
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-19 19:11:26 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-11-19 19:11:26 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.head('/')
|
|
|
|
.expect(200, done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-03-23 11:01:14 +00:00
|
|
|
|
|
|
|
it('should not overwrite the content-type', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-03-23 11:01:14 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 200;
|
|
|
|
this.type = 'application/javascript';
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-03-23 11:01:14 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-03-23 11:01:14 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.head('/')
|
2014-08-04 20:28:26 +00:00
|
|
|
.expect('content-type', /application\/javascript/)
|
2014-03-23 11:01:14 +00:00
|
|
|
.expect(200, done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
describe('when no middleware are present', function(){
|
|
|
|
it('should 404', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(404, done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-12-22 17:26:21 +00:00
|
|
|
describe('when res has already been written to', function(){
|
|
|
|
it('should not cause an app error', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-12-22 17:26:21 +00:00
|
|
|
|
|
|
|
app.use(function *(next){
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = this.res;
|
2014-04-29 04:17:30 +00:00
|
|
|
this.status = 200;
|
2015-10-12 20:36:41 +00:00
|
|
|
res.setHeader('Content-Type', 'text/html');
|
2013-12-22 17:26:21 +00:00
|
|
|
res.write('Hello');
|
2014-03-24 18:21:15 +00:00
|
|
|
setTimeout(function(){
|
2015-10-12 20:36:41 +00:00
|
|
|
res.end('Goodbye');
|
2013-12-22 17:26:21 +00:00
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
2015-10-12 20:36:41 +00:00
|
|
|
let errorCaught = false;
|
2013-12-22 17:26:21 +00:00
|
|
|
|
2014-03-24 18:21:15 +00:00
|
|
|
app.on('error', function(err){
|
2013-12-22 17:26:21 +00:00
|
|
|
errorCaught = err;
|
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-12-22 17:26:21 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
|
|
|
if (errorCaught) return done(errorCaught);
|
|
|
|
done();
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-12-22 17:26:21 +00:00
|
|
|
|
|
|
|
it('should send the right body', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-12-22 17:26:21 +00:00
|
|
|
|
|
|
|
app.use(function *(next){
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = this.res;
|
2014-04-29 04:17:30 +00:00
|
|
|
this.status = 200;
|
2015-10-12 20:36:41 +00:00
|
|
|
res.setHeader('Content-Type', 'text/html');
|
2013-12-22 17:26:21 +00:00
|
|
|
res.write('Hello');
|
2014-03-24 18:21:15 +00:00
|
|
|
setTimeout(function(){
|
2015-10-12 20:36:41 +00:00
|
|
|
res.end('Goodbye');
|
2013-12-22 17:26:21 +00:00
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-12-22 17:26:21 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(200)
|
|
|
|
.expect('HelloGoodbye', done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-12-22 17:26:21 +00:00
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
describe('when .body is missing', function(){
|
2013-11-20 06:20:17 +00:00
|
|
|
describe('with status=400', function(){
|
|
|
|
it('should respond with the associated status message', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-20 06:20:17 +00:00
|
|
|
app.use(function *(){
|
|
|
|
this.status = 400;
|
|
|
|
});
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-20 06:20:17 +00:00
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(400)
|
2014-04-13 02:23:57 +00:00
|
|
|
.expect('Content-Length', 11)
|
2013-11-20 06:20:17 +00:00
|
|
|
.expect('Bad Request', done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-20 06:20:17 +00:00
|
|
|
|
|
|
|
describe('with status=204', function(){
|
|
|
|
it('should respond without a body', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-11-20 06:20:17 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 204;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-20 06:20:17 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-11-20 06:20:17 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(204)
|
|
|
|
.expect('')
|
2014-03-24 18:21:15 +00:00
|
|
|
.end(function(err, res){
|
2013-11-20 06:20:17 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
res.header.should.not.have.property('content-type');
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-20 06:20:17 +00:00
|
|
|
|
|
|
|
describe('with status=205', function(){
|
|
|
|
it('should respond without a body', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-11-20 06:20:17 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 205;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-20 06:20:17 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-11-20 06:20:17 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(205)
|
|
|
|
.expect('')
|
2014-03-24 18:21:15 +00:00
|
|
|
.end(function(err, res){
|
2013-11-20 06:20:17 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
res.header.should.not.have.property('content-type');
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-20 06:20:17 +00:00
|
|
|
|
|
|
|
describe('with status=304', function(){
|
|
|
|
it('should respond without a body', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-11-20 06:20:17 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 304;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-20 06:20:17 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-11-20 06:20:17 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(304)
|
|
|
|
.expect('')
|
2014-03-24 18:21:15 +00:00
|
|
|
.end(function(err, res){
|
2013-11-20 06:20:17 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
res.header.should.not.have.property('content-type');
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-01 12:12:20 +00:00
|
|
|
|
|
|
|
describe('with custom status=700', function(){
|
2015-10-12 20:36:41 +00:00
|
|
|
it('should respond with the associated status message', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-10-01 12:12:20 +00:00
|
|
|
statuses['700'] = 'custom status';
|
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 700;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-10-01 12:12:20 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-10-01 12:12:20 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(700)
|
|
|
|
.expect('custom status')
|
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
|
|
|
res.res.statusMessage.should.equal('custom status');
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-01 12:12:20 +00:00
|
|
|
|
|
|
|
describe('with custom statusMessage=ok', function(){
|
2015-10-12 20:36:41 +00:00
|
|
|
it('should respond with the custom status message', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-10-01 12:12:20 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 200;
|
|
|
|
this.message = 'ok';
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-10-01 12:12:20 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-10-01 12:12:20 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(200)
|
|
|
|
.expect('ok')
|
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
|
|
|
res.res.statusMessage.should.equal('ok');
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-01 12:12:20 +00:00
|
|
|
|
2015-10-12 20:36:41 +00:00
|
|
|
describe('with custom status without message', function(){
|
|
|
|
it('should respond with the status code number', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-10-01 12:12:20 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.res.statusCode = 701;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-10-01 12:12:20 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-10-01 12:12:20 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(701)
|
|
|
|
.expect('701', done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2014-04-09 16:21:15 +00:00
|
|
|
describe('when .body is a null', function(){
|
|
|
|
it('should respond 204 by default', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.body = null;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(204)
|
|
|
|
.expect('')
|
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
res.header.should.not.have.property('content-type');
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
it('should respond 204 with status=200', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 200;
|
|
|
|
this.body = null;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(204)
|
|
|
|
.expect('')
|
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
res.header.should.not.have.property('content-type');
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
it('should respond 205 with status=205', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 205;
|
|
|
|
this.body = null;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(205)
|
|
|
|
.expect('')
|
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
res.header.should.not.have.property('content-type');
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
it('should respond 304 with status=304', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 304;
|
|
|
|
this.body = null;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(304)
|
|
|
|
.expect('')
|
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
res.header.should.not.have.property('content-type');
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
describe('when .body is a string', function(){
|
|
|
|
it('should respond', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(){
|
|
|
|
this.body = 'Hello';
|
2013-08-17 07:15:57 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect('Hello', done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
describe('when .body is a Buffer', function(){
|
|
|
|
it('should respond', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(){
|
|
|
|
this.body = new Buffer('Hello');
|
2013-08-17 07:15:57 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect('Hello', done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
describe('when .body is a Stream', function(){
|
|
|
|
it('should respond', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(){
|
|
|
|
this.body = fs.createReadStream('package.json');
|
2014-06-04 04:44:25 +00:00
|
|
|
this.set('Content-Type', 'application/json; charset=utf-8');
|
2013-08-17 07:15:57 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
2014-06-04 04:44:25 +00:00
|
|
|
.expect('Content-Type', 'application/json; charset=utf-8')
|
2013-08-17 07:15:57 +00:00
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
2015-10-12 04:59:30 +00:00
|
|
|
const pkg = require('../../package');
|
2014-04-29 03:34:26 +00:00
|
|
|
res.should.not.have.header('Content-Length');
|
2013-08-17 07:15:57 +00:00
|
|
|
res.body.should.eql(pkg);
|
|
|
|
done();
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-09-08 19:07:50 +00:00
|
|
|
|
2014-04-29 03:34:26 +00:00
|
|
|
it('should strip content-length when overwriting', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.body = 'hello';
|
|
|
|
this.body = fs.createReadStream('package.json');
|
2014-06-04 04:44:25 +00:00
|
|
|
this.set('Content-Type', 'application/json; charset=utf-8');
|
2014-04-29 03:34:26 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
2014-06-04 04:44:25 +00:00
|
|
|
.expect('Content-Type', 'application/json; charset=utf-8')
|
2014-04-29 03:34:26 +00:00
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
2015-10-12 04:59:30 +00:00
|
|
|
const pkg = require('../../package');
|
2014-04-29 03:34:26 +00:00
|
|
|
res.should.not.have.header('Content-Length');
|
|
|
|
res.body.should.eql(pkg);
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
it('should keep content-length if not overwritten', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.length = fs.readFileSync('package.json').length;
|
|
|
|
this.body = fs.createReadStream('package.json');
|
2014-06-04 04:44:25 +00:00
|
|
|
this.set('Content-Type', 'application/json; charset=utf-8');
|
2014-04-29 03:34:26 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
2014-06-04 04:44:25 +00:00
|
|
|
.expect('Content-Type', 'application/json; charset=utf-8')
|
2014-04-29 03:34:26 +00:00
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
2015-10-12 04:59:30 +00:00
|
|
|
const pkg = require('../../package');
|
2014-04-29 03:34:26 +00:00
|
|
|
res.should.have.header('Content-Length');
|
|
|
|
res.body.should.eql(pkg);
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
it('should keep content-length if overwritten with the same stream', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.length = fs.readFileSync('package.json').length;
|
2015-10-05 18:23:47 +00:00
|
|
|
const stream = fs.createReadStream('package.json');
|
2014-04-29 03:34:26 +00:00
|
|
|
this.body = stream;
|
|
|
|
this.body = stream;
|
2014-06-04 04:44:25 +00:00
|
|
|
this.set('Content-Type', 'application/json; charset=utf-8');
|
2014-04-29 03:34:26 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
2014-06-04 04:44:25 +00:00
|
|
|
.expect('Content-Type', 'application/json; charset=utf-8')
|
2014-04-29 03:34:26 +00:00
|
|
|
.end(function(err, res){
|
|
|
|
if (err) return done(err);
|
2015-10-12 04:59:30 +00:00
|
|
|
const pkg = require('../../package');
|
2014-04-29 03:34:26 +00:00
|
|
|
res.should.have.header('Content-Length');
|
|
|
|
res.body.should.eql(pkg);
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-04-29 03:34:26 +00:00
|
|
|
|
2013-09-08 19:07:50 +00:00
|
|
|
it('should handle errors', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-09-08 19:07:50 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(){
|
2014-06-04 04:44:25 +00:00
|
|
|
this.set('Content-Type', 'application/json; charset=utf-8');
|
2013-11-08 00:15:47 +00:00
|
|
|
this.body = fs.createReadStream('does not exist');
|
2013-09-08 19:07:50 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-09-08 19:07:50 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
2013-09-14 21:48:33 +00:00
|
|
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
2013-09-08 19:11:02 +00:00
|
|
|
.expect(404)
|
2013-09-08 19:07:50 +00:00
|
|
|
.end(done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-09 16:02:13 +00:00
|
|
|
|
|
|
|
it('should handle errors when no content status', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-09 16:02:13 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 204;
|
|
|
|
this.body = fs.createReadStream('does not exist');
|
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-09 16:02:13 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(204, done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-09 16:02:13 +00:00
|
|
|
|
|
|
|
it('should handle all intermediate stream body errors', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-09 16:02:13 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.body = fs.createReadStream('does not exist');
|
|
|
|
this.body = fs.createReadStream('does not exist');
|
|
|
|
this.body = fs.createReadStream('does not exist');
|
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-09 16:02:13 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(404, done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
describe('when .body is an Object', function(){
|
|
|
|
it('should respond with json', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(){
|
|
|
|
this.body = { hello: 'world' };
|
2013-08-17 07:15:57 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
2014-06-04 04:44:25 +00:00
|
|
|
.expect('Content-Type', 'application/json; charset=utf-8')
|
2014-02-01 02:39:47 +00:00
|
|
|
.expect('{"hello":"world"}', done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
describe('when an error occurs', function(){
|
2013-09-08 16:37:19 +00:00
|
|
|
it('should emit "error" on the app', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-09-08 16:37:19 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(){
|
|
|
|
throw new Error('boom');
|
2013-09-08 16:37:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.on('error', function(err){
|
|
|
|
err.message.should.equal('boom');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app.listen())
|
|
|
|
.get('/')
|
|
|
|
.end(function(){});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-09-08 16:37:19 +00:00
|
|
|
|
2013-09-12 15:05:50 +00:00
|
|
|
describe('with an .expose property', function(){
|
|
|
|
it('should expose the message', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-09-12 15:05:50 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(){
|
2015-10-05 18:23:47 +00:00
|
|
|
const err = new Error('sorry!');
|
2013-11-08 00:15:47 +00:00
|
|
|
err.status = 403;
|
|
|
|
err.expose = true;
|
|
|
|
throw err;
|
2013-09-12 15:05:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
request(app.listen())
|
|
|
|
.get('/')
|
|
|
|
.expect(403, 'sorry!')
|
|
|
|
.end(done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-09-12 15:05:50 +00:00
|
|
|
|
2013-08-22 02:47:56 +00:00
|
|
|
describe('with a .status property', function(){
|
|
|
|
it('should respond with .status', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-08-22 02:47:56 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(){
|
2015-10-05 18:23:47 +00:00
|
|
|
const err = new Error('s3 explodes');
|
2013-11-08 00:15:47 +00:00
|
|
|
err.status = 403;
|
|
|
|
throw err;
|
2013-08-22 02:47:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
request(app.listen())
|
|
|
|
.get('/')
|
|
|
|
.expect(403, 'Forbidden')
|
|
|
|
.end(done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-08-22 02:47:56 +00:00
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
it('should respond with 500', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(){
|
|
|
|
throw new Error('boom!');
|
2013-08-17 07:15:57 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(500, 'Internal Server Error')
|
|
|
|
.end(done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
it('should be catchable', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(next){
|
|
|
|
try {
|
|
|
|
yield next;
|
|
|
|
this.body = 'Hello';
|
|
|
|
} catch (err) {
|
|
|
|
this.body = 'Got error';
|
2013-08-17 07:15:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
app.use(function *(next){
|
|
|
|
throw new Error('boom!');
|
2013-08-17 07:15:57 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(200, 'Got error')
|
|
|
|
.end(done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
describe('when status and body property', function(){
|
|
|
|
it('should 200', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 304;
|
|
|
|
this.body = 'hello';
|
|
|
|
this.status = 200;
|
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(200)
|
|
|
|
.expect('hello', done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
2015-10-12 20:36:41 +00:00
|
|
|
it('should 204', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = 200;
|
|
|
|
this.body = 'hello';
|
|
|
|
this.set('content-type', 'text/plain; charset=utf8');
|
|
|
|
this.status = 204;
|
|
|
|
});
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(204)
|
2015-10-12 20:36:41 +00:00
|
|
|
.end(function(err, res){
|
2014-04-09 16:21:15 +00:00
|
|
|
res.should.not.have.header('content-type');
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|