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