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.body=', () => {
|
|
|
|
describe('when Content-Type is set', () => {
|
|
|
|
it('should not override', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.type = 'png';
|
2017-03-20 06:48:37 +00:00
|
|
|
res.body = Buffer.from('something');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('image/png', res.header['content-type']);
|
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 body is an object', () => {
|
|
|
|
it('should override as json', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
res.body = '<em>hey</em>';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('text/html; charset=utf-8', res.header['content-type']);
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
res.body = { foo: 'bar' };
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('application/json; charset=utf-8', res.header['content-type']);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should override length', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.type = 'html';
|
|
|
|
res.body = 'something';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.length, 9);
|
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 a string is given', () => {
|
|
|
|
it('should default to text', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.body = 'Tobi';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('text/plain; charset=utf-8', res.header['content-type']);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should set length', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.body = 'Tobi';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('4', res.header['content-length']);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-02-14 02:35:25 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('and contains a non-leading <', () => {
|
|
|
|
it('should default to text', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2014-02-14 02:35:25 +00:00
|
|
|
res.body = 'aklsdjf < klajsdlfjasd';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('text/plain; charset=utf-8', res.header['content-type']);
|
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 an html string is given', () => {
|
|
|
|
it('should default to html', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.body = '<h1>Tobi</h1>';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('text/html; charset=utf-8', res.header['content-type']);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should set length', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const string = '<h1>Tobi</h1>';
|
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.body = string;
|
|
|
|
assert.equal(res.length, Buffer.byteLength(string));
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should set length when body is overridden', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const string = '<h1>Tobi</h1>';
|
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.body = string;
|
|
|
|
res.body = string + string;
|
|
|
|
assert.equal(res.length, 2 * Buffer.byteLength(string));
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-02-14 02:35:25 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when it contains leading whitespace', () => {
|
|
|
|
it('should default to html', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2014-02-14 02:35:25 +00:00
|
|
|
res.body = ' <h1>Tobi</h1>';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('text/html; charset=utf-8', res.header['content-type']);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-02-14 02:35:25 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when an xml string is given', () => {
|
|
|
|
it('should default to html', () => {
|
2014-02-14 02:35:25 +00:00
|
|
|
/**
|
2015-10-14 00:45:18 +00:00
|
|
|
* ctx test is to show that we're not going
|
2014-02-14 02:35:25 +00:00
|
|
|
* to be stricter with the html sniff
|
|
|
|
* or that we will sniff other string types.
|
2015-10-14 00:45:18 +00:00
|
|
|
* You should `.type=` if ctx simple test fails.
|
2014-02-14 02:35:25 +00:00
|
|
|
*/
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2014-02-14 02:35:25 +00:00
|
|
|
res.body = '<?xml version="1.0" encoding="UTF-8"?>\n<俄语>данные</俄语>';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('text/html; charset=utf-8', res.header['content-type']);
|
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 a stream is given', () => {
|
|
|
|
it('should default to an octet stream', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.body = fs.createReadStream('LICENSE');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('application/octet-stream', res.header['content-type']);
|
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 a buffer is given', () => {
|
|
|
|
it('should default to an octet stream', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2017-03-20 06:48:37 +00:00
|
|
|
res.body = Buffer.from('hey');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('application/octet-stream', res.header['content-type']);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should set length', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2017-03-20 06:48:37 +00:00
|
|
|
res.body = Buffer.from('Tobi');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('4', res.header['content-length']);
|
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 an object is given', () => {
|
|
|
|
it('should default to json', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.body = { foo: 'bar' };
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal('application/json; charset=utf-8', res.header['content-type']);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|