From 20e58cf3e4f20fc5d5886df1d0ac6dd8c33bd202 Mon Sep 17 00:00:00 2001 From: dead-horse Date: Sun, 21 Jun 2020 23:56:31 +0800 Subject: [PATCH] test: imporve coverage to 100% --- lib/application.js | 2 ++ lib/response.js | 3 ++- test/application/respond.js | 22 ++++++++++++++++++++++ test/response/length.js | 18 ++++++++---------- test/response/set.js | 4 ++-- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/lib/application.js b/lib/application.js index ded8fef41..2761af204 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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; } diff --git a/lib/response.js b/lib/response.js index d14e86a11..2f7e7a5f3 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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; } diff --git a/test/application/respond.js b/test/application/respond.js index b7080c9ef..b0fd5964b 100644 --- a/test/application/respond.js +++ b/test/application/respond.js @@ -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', () => { diff --git a/test/response/length.js b/test/response/length.js index 9249ec4bd..d64cfb329 100644 --- a/test/response/length.js +++ b/test/response/length.js @@ -5,16 +5,6 @@ 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', () => { @@ -22,6 +12,14 @@ describe('res.length', () => { 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', () => { diff --git a/test/response/set.js b/test/response/set.js index 710b4586d..22e9a3dd8 100644 --- a/test/response/set.js +++ b/test/response/set.js @@ -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' ]); }); });