koa-lite/test/application/index.js

81 lines
2 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
const request = require('supertest');
const assert = require('assert');
2015-10-13 06:19:42 +00:00
const Koa = require('../..');
describe('app', () => {
it('should handle socket errors', done => {
2015-10-13 06:19:42 +00:00
const app = new Koa();
app.use((ctx, next) => {
// triggers ctx.socket.writable == false
ctx.socket.emit('error', new Error('boom'));
});
app.on('error', err => {
2017-05-11 03:30:32 +00:00
assert.equal(err.message, 'boom');
done();
});
request(app.callback())
.get('/')
.end(() => {});
2015-10-12 20:36:41 +00:00
});
it('should not .writeHead when !socket.writable', done => {
2015-10-13 06:19:42 +00:00
const app = new Koa();
app.use((ctx, next) => {
// set .writable to false
ctx.socket.writable = false;
ctx.status = 204;
// throw if .writeHead or .end is called
ctx.res.writeHead =
ctx.res.end = () => {
throw new Error('response sent');
};
2015-10-12 20:36:41 +00:00
});
// hackish, but the response should occur in a single tick
setImmediate(done);
request(app.callback())
.get('/')
.end(() => {});
2015-10-12 20:36:41 +00:00
});
it('should set development env when NODE_ENV missing', () => {
const NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = '';
2015-10-13 06:19:42 +00:00
const app = new Koa();
process.env.NODE_ENV = NODE_ENV;
assert.equal(app.env, 'development');
2015-10-12 20:36:41 +00:00
});
it('should set env from the constructor', () => {
const env = 'custom';
const app = new Koa({ env });
assert.strictEqual(app.env, env);
});
it('should set proxy flag from the constructor', () => {
const proxy = true;
const app = new Koa({ proxy });
assert.strictEqual(app.proxy, proxy);
});
it('should set signed cookie keys from the constructor', () => {
const keys = ['customkey'];
const app = new Koa({ keys });
assert.strictEqual(app.keys, keys);
});
it('should set subdomainOffset from the constructor', () => {
const subdomainOffset = 3;
const app = new Koa({ subdomainOffset });
assert.strictEqual(app.subdomainOffset, subdomainOffset);
});
2015-10-12 20:36:41 +00:00
});