Skip to content

Commit

Permalink
updated the send method
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffdutton committed Jan 27, 2017
1 parent be25336 commit 759785d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"max-len": ["error", 120],
"mocha/no-exclusive-tests": "error",
"mocha/no-skipped-tests": "error",
"mocha/no-pending-tests": "error"
"mocha/no-pending-tests": "error",
"prefer-const": ["error"]
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"main": "./src/index.js",
"scripts": {
"coveralls": "cat ./coverage/lcov.info | coveralls",
"coverage": "rm -rf ./coverage && istanbul cover _mocha -- --colors --reporter spec --recursive test/",
"lint": "eslint .",
"test": "istanbul cover _mocha --report lcovonly -- --colors --reporter spec --recursive test/",
"publish-please": "publish-please",
Expand Down
39 changes: 32 additions & 7 deletions src/events/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,50 @@ class Response {
*
* Examples:
*
* res.send(new Buffer('wahoo'));
* res.send({ some: 'json' });
* res.send('<p>some html</p>');
* res.send('some text');
*
* returns and object like:
* {
* statusCode: 200,
* headers: { 'Content-Type': 'text/html' }
* body: '<html>some html</html>
* headers: {
* 'Content-Type': 'text/plain' ,
* 'Content-Length': '9'
* },
* body: 'some text'
* }
*
* @param {string|number|boolean|object|Buffer} [body]
* @param {string|number|boolean|object} [body]
* @returns {object}
*/
send (body) {
body = typeof body === 'undefined' ? this.body : body

switch (typeof body) {
// string defaulting to plain text
case 'string':
if (!this.get('Content-Type')) {
this.contentType('text')
}
break
case 'boolean':
case 'number':
case 'object':
if (body === null) {
body = ''
if (!this.get('Content-Type')) {
this.contentType('text')
}
} else {
return this.json(body)
}
break
}

const res = {
statusCode: this.statusCode,
headers: this.headers,
body: body || this.body,
body: body,
}

if (this.isBase64Encoded) {
Expand Down Expand Up @@ -149,7 +174,7 @@ class Response {
cookie (name, value, options) {
const opts = options || {}

let val = typeof value === 'object'
const val = typeof value === 'object'
? `j:${JSON.stringify(value)}`
: _toString(value)

Expand Down
72 changes: 68 additions & 4 deletions test/events/response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,86 @@ describe('events', () => {
const res = new Response()
expect(res.send('blah')).to.eql({
statusCode: 200,
headers: {},
headers: { 'Content-Type': 'text/plain' },
body: 'blah',
})
})

it('should not overwrite content type if set', () => {
const res = new Response()
res.contentType('html')
expect(res.send('blah')).to.eql({
statusCode: 200,
headers: { 'Content-Type': 'text/html' },
body: 'blah',
})
})

it('should add isBase64Encoded if true', () => {
const base64EmptyGif = 'R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='
const res = new Response({
headers: {
'Content-Type': 'image/gif',
},
isBase64Encoded: true,
})
expect(res.send('blah')).to.eql({
expect(res.send(base64EmptyGif)).to.eql({
statusCode: 200,
headers: {},
body: 'blah',
headers: {
'Content-Type': 'image/gif',
},
body: base64EmptyGif,
isBase64Encoded: true,
})
})

context('passing an object', () => {
it('should return empty string if undefined', () => {
const res = new Response()
expect(res.send()).to.eql({
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: '',
})
})

// @TODO: Is this necessary??
it('should return empty string if null', () => {
const res = new Response()
expect(res.send(null)).to.eql({
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: '',
})
})

it('should stringify if boolean', () => {
const res = new Response()
expect(res.send(false)).to.eql({
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(false),
})
})

it('should stringify if number', () => {
const res = new Response()
expect(res.send(6000)).to.eql({
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(6000),
})
})

it('should stringify and set content type', () => {
const res = new Response()
expect(res.send({ some: 'object' })).to.eql({
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ some: 'object' }),
})
})
})
})

describe('#json', () => {
Expand Down

0 comments on commit 759785d

Please sign in to comment.