koa-lite/test/application/respond.js

820 lines
18 KiB
JavaScript
Raw Normal View History

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