From 759785d5970da1df41643e523bad8f5da80cb7ab Mon Sep 17 00:00:00 2001 From: Geoff Dutton Date: Fri, 27 Jan 2017 09:40:51 -0600 Subject: [PATCH] updated the send method --- .eslintrc | 3 +- package.json | 1 + src/events/response.js | 39 +++++++++++++++---- test/events/response.test.js | 72 ++++++++++++++++++++++++++++++++++-- 4 files changed, 103 insertions(+), 12 deletions(-) diff --git a/.eslintrc b/.eslintrc index 2a9c715..8000c4d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -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"] } } diff --git a/package.json b/package.json index 71d0460..f805667 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/events/response.js b/src/events/response.js index e42834a..e5c586e 100644 --- a/src/events/response.js +++ b/src/events/response.js @@ -82,25 +82,50 @@ class Response { * * Examples: * - * res.send(new Buffer('wahoo')); * res.send({ some: 'json' }); - * res.send('

some html

'); + * res.send('some text'); * * returns and object like: * { * statusCode: 200, - * headers: { 'Content-Type': 'text/html' } - * body: 'some 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) { @@ -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) diff --git a/test/events/response.test.js b/test/events/response.test.js index 91b1abe..b8d53b6 100644 --- a/test/events/response.test.js +++ b/test/events/response.test.js @@ -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', () => {