Skip to content

Commit 20e58cf

Browse files
committed
test: imporve coverage to 100%
1 parent 4a40d63 commit 20e58cf

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

lib/application.js

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ module.exports = class Application extends Emitter {
5959
this.context = Object.create(context);
6060
this.request = Object.create(request);
6161
this.response = Object.create(response);
62+
// util.inspect.custom support for node 6+
63+
/* istanbul ignore else */
6264
if (util.inspect.custom) {
6365
this[util.inspect.custom] = this.inspect;
6466
}

lib/response.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,12 @@ module.exports = {
572572
};
573573

574574
/**
575-
* Custom inspection implementation for newer Node.js versions.
575+
* Custom inspection implementation for node 6+.
576576
*
577577
* @return {Object}
578578
* @api public
579579
*/
580+
/* istanbul ignore else */
580581
if (util.inspect.custom) {
581582
module.exports[util.inspect.custom] = module.exports.inspect;
582583
}

test/application/respond.js

+22
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,28 @@ describe('app.respond', () => {
683683
.expect('Content-Type', 'application/json; charset=utf-8')
684684
.expect('{"hello":"world"}');
685685
});
686+
describe('and headers sent', () => {
687+
it('should respond with json body and headers', () => {
688+
const app = new Koa();
689+
690+
app.use(ctx => {
691+
ctx.length = 17;
692+
ctx.type = 'json';
693+
ctx.set('foo', 'bar');
694+
ctx.res.flushHeaders();
695+
ctx.body = { hello: 'world' };
696+
});
697+
698+
const server = app.listen();
699+
700+
return request(server)
701+
.get('/')
702+
.expect('Content-Type', 'application/json; charset=utf-8')
703+
.expect('Content-Length', '17')
704+
.expect('foo', 'bar')
705+
.expect('{"hello":"world"}');
706+
});
707+
});
686708
});
687709

688710
describe('when an error occurs', () => {

test/response/length.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ const response = require('../helpers/context').response;
55
const assert = require('assert');
66
const fs = require('fs');
77

8-
describe('res.length', () => {
9-
describe('when Content-Length is defined', () => {
10-
it('should return a number', () => {
11-
const res = response();
12-
res.header['content-length'] = '120';
13-
assert.equal(res.length, 120);
14-
});
15-
});
16-
});
17-
188
describe('res.length', () => {
199
describe('when Content-Length is defined', () => {
2010
it('should return a number', () => {
2111
const res = response();
2212
res.set('Content-Length', '1024');
2313
assert.equal(res.length, 1024);
2414
});
15+
16+
describe('but not number', () => {
17+
it('should return 0', () => {
18+
const res = response();
19+
res.set('Content-Length', 'hey');
20+
assert.equal(res.length, 0);
21+
});
22+
});
2523
});
2624

2725
describe('when Content-Length is not defined', () => {

test/response/set.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ describe('ctx.set(name, val)', () => {
2525

2626
it('should set a field value of array', () => {
2727
const ctx = context();
28-
ctx.set('x-foo', ['foo', 'bar']);
29-
assert.deepEqual(ctx.response.header['x-foo'], [ 'foo', 'bar' ]);
28+
ctx.set('x-foo', ['foo', 'bar', 123 ]);
29+
assert.deepEqual(ctx.response.header['x-foo'], [ 'foo', 'bar', '123' ]);
3030
});
3131
});
3232

0 commit comments

Comments
 (0)