2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const request = require('supertest');
|
|
|
|
const assert = require('assert');
|
2015-10-13 06:19:42 +00:00
|
|
|
const Koa = require('../..');
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('app', () => {
|
|
|
|
it('should handle socket errors', done => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-14 00:45:18 +00:00
|
|
|
// triggers ctx.socket.writable == false
|
|
|
|
ctx.socket.emit('error', new Error('boom'));
|
2015-10-12 04:59:30 +00:00
|
|
|
});
|
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.on('error', err => {
|
2015-10-12 04:59:30 +00:00
|
|
|
err.message.should.equal('boom');
|
|
|
|
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
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should not .writeHead when !socket.writable', done => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-12 04:59:30 +00:00
|
|
|
// set .writable to false
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.socket.writable = false;
|
|
|
|
ctx.status = 204;
|
2015-10-12 04:59:30 +00:00
|
|
|
// throw if .writeHead or .end is called
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.res.writeHead =
|
2015-10-25 07:54:57 +00:00
|
|
|
ctx.res.end = () => {
|
2015-10-12 04:59:30 +00:00
|
|
|
throw new Error('response sent');
|
|
|
|
};
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
// hackish, but the response should occur in a single tick
|
|
|
|
setImmediate(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
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should set development env when NODE_ENV missing', () => {
|
2015-10-12 04:59:30 +00:00
|
|
|
const NODE_ENV = process.env.NODE_ENV;
|
|
|
|
process.env.NODE_ENV = '';
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
process.env.NODE_ENV = NODE_ENV;
|
|
|
|
assert.equal(app.env, 'development');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|