2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-12 04:59:30 +00:00
|
|
|
const response = require('../helpers/context').response;
|
2015-10-05 18:23:47 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('res.length', () => {
|
|
|
|
describe('when Content-Length is defined', () => {
|
|
|
|
it('should return a number', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.header['content-length'] = '120';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, 120);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('res.length', () => {
|
|
|
|
describe('when Content-Length is defined', () => {
|
|
|
|
it('should return a number', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.set('Content-Length', '1024');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, 1024);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when Content-Length is not defined', () => {
|
|
|
|
describe('and a .body is set', () => {
|
|
|
|
it('should return a number', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
res.body = 'foo';
|
2014-04-29 03:34:26 +00:00
|
|
|
res.remove('Content-Length');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, 3);
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2014-04-29 03:34:26 +00:00
|
|
|
res.body = 'foo';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, 3);
|
2014-04-29 03:34:26 +00:00
|
|
|
|
2017-03-20 06:48:37 +00:00
|
|
|
res.body = Buffer.from('foo bar');
|
2014-04-29 03:34:26 +00:00
|
|
|
res.remove('Content-Length');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, 7);
|
2014-04-29 03:34:26 +00:00
|
|
|
|
2017-03-20 06:48:37 +00:00
|
|
|
res.body = Buffer.from('foo bar');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, 7);
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
res.body = { hello: 'world' };
|
|
|
|
res.remove('Content-Length');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, 17);
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
res.body = { hello: 'world' };
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, 17);
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
res.body = fs.createReadStream('package.json');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, undefined);
|
2014-04-29 03:34:26 +00:00
|
|
|
|
|
|
|
res.body = null;
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, undefined);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('and .body is not', () => {
|
|
|
|
it('should return undefined', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, undefined);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|