Skip to content

Commit

Permalink
test: imporve coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Jun 21, 2020
1 parent 4a40d63 commit 20e58cf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ module.exports = class Application extends Emitter {
this.context = Object.create(context);
this.request = Object.create(request);
this.response = Object.create(response);
// util.inspect.custom support for node 6+
/* istanbul ignore else */
if (util.inspect.custom) {
this[util.inspect.custom] = this.inspect;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,12 @@ module.exports = {
};

/**
* Custom inspection implementation for newer Node.js versions.
* Custom inspection implementation for node 6+.
*
* @return {Object}
* @api public
*/
/* istanbul ignore else */
if (util.inspect.custom) {
module.exports[util.inspect.custom] = module.exports.inspect;
}
22 changes: 22 additions & 0 deletions test/application/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,28 @@ describe('app.respond', () => {
.expect('Content-Type', 'application/json; charset=utf-8')
.expect('{"hello":"world"}');
});
describe('and headers sent', () => {
it('should respond with json body and headers', () => {
const app = new Koa();

app.use(ctx => {
ctx.length = 17;
ctx.type = 'json';
ctx.set('foo', 'bar');
ctx.res.flushHeaders();
ctx.body = { hello: 'world' };
});

const server = app.listen();

return request(server)
.get('/')
.expect('Content-Type', 'application/json; charset=utf-8')
.expect('Content-Length', '17')
.expect('foo', 'bar')
.expect('{"hello":"world"}');
});
});
});

describe('when an error occurs', () => {
Expand Down
18 changes: 8 additions & 10 deletions test/response/length.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@ const response = require('../helpers/context').response;
const assert = require('assert');
const fs = require('fs');

describe('res.length', () => {
describe('when Content-Length is defined', () => {
it('should return a number', () => {
const res = response();
res.header['content-length'] = '120';
assert.equal(res.length, 120);
});
});
});

describe('res.length', () => {
describe('when Content-Length is defined', () => {
it('should return a number', () => {
const res = response();
res.set('Content-Length', '1024');
assert.equal(res.length, 1024);
});

describe('but not number', () => {
it('should return 0', () => {
const res = response();
res.set('Content-Length', 'hey');
assert.equal(res.length, 0);
});
});
});

describe('when Content-Length is not defined', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/response/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe('ctx.set(name, val)', () => {

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

Expand Down

0 comments on commit 20e58cf

Please sign in to comment.