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