koa-lite/test/response/length.js
2017-05-11 11:30:32 +08:00

68 lines
1.7 KiB
JavaScript

'use strict';
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('when Content-Length is not defined', () => {
describe('and a .body is set', () => {
it('should return a number', () => {
const res = response();
res.body = 'foo';
res.remove('Content-Length');
assert.equal(res.length, 3);
res.body = 'foo';
assert.equal(res.length, 3);
res.body = Buffer.from('foo bar');
res.remove('Content-Length');
assert.equal(res.length, 7);
res.body = Buffer.from('foo bar');
assert.equal(res.length, 7);
res.body = { hello: 'world' };
res.remove('Content-Length');
assert.equal(res.length, 17);
res.body = { hello: 'world' };
assert.equal(res.length, 17);
res.body = fs.createReadStream('package.json');
assert.equal(res.length, undefined);
res.body = null;
assert.equal(res.length, undefined);
});
});
describe('and .body is not', () => {
it('should return undefined', () => {
const res = response();
assert.equal(res.length, undefined);
});
});
});
});