Skip to content

Commit

Permalink
fix: If route action returns null or undefined, respond with empty ob…
Browse files Browse the repository at this point in the history
…ject
  • Loading branch information
andreidmt committed Oct 22, 2019
1 parent 150afb2 commit 1335df3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
16 changes: 7 additions & 9 deletions src/middleware/res-goodbye.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
const debug = require("debug")("Blocks:GoodbyeMiddleware")

import { forEach } from "@mutantlove/m"
import { forEach, is } from "@mutantlove/m"

module.exports = () => (req, res) => {
debug(`${req.method}:${req.url} responding with ${res.ctx.status}`)

const payloadType = typeof res.ctx.payload
const payload = is(res.ctx.payload) ? res.ctx.payload : {}
const isObject = typeof payload === "object"
const endAt = process.hrtime(req.ctx.startAt)

const body =
payloadType === "object" ? JSON.stringify(res.ctx.payload) : res.ctx.payload
const contentType =
payloadType === "object"
? "application/json; charset=utf-8"
: "text/plain; charset=utf-8"
const body = isObject ? JSON.stringify(payload) : payload
const contentType = isObject
? "application/json; charset=utf-8"
: "text/plain; charset=utf-8"

forEach(([name, value]) => res.setHeader(name, value))([
// from the first middleware to now
["X-Response-Time", `${endAt[0]}s ${endAt[1] / 1000000}ms`],

["Content-Type", contentType],
["Content-Length", Buffer.byteLength(body, "utf8")],
])
Expand Down
12 changes: 10 additions & 2 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("blocks :: init with defaults", async assert => {
require("./routes/no-schema.route"),
require("./routes/with-schema.route"),
require("./routes/no-allow.route"),
require("./routes/return-undefined.route"),
],
})

Expand All @@ -29,10 +30,10 @@ describe("blocks :: init with defaults", async assert => {
})

assert({
given: "3 custom routes",
given: "4 custom routes",
should: "load default /ping and custom",
actual: Plugins.Router.count(),
expected: 4,
expected: 5,
})

assert({
Expand Down Expand Up @@ -112,5 +113,12 @@ describe("blocks :: init with defaults", async assert => {
},
})

assert({
given: "route that returns undefined",
should: "return empty JSON object",
actual: await request(`${API_URL}/return-undefined`),
expected: {},
})

server.close()
})
26 changes: 26 additions & 0 deletions tests/routes/return-undefined.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
method: "GET",
path: "/return-undefined",

/**
* Permission checking, if allowed:
* -> continue to action
* -> otherwise return 403
*
* @param {Object} plugins Plugins
* @param {Object} req Node request
*
* @return {boolean}
*/
isAllowed: (/* plugins */) => async () => true,

/**
* After schema validation and permission checking, do route logic
*
* @param {Object} plugins Plugins
* @param {Object} req Node request
*
* @return {mixed}
*/
action: () => () => {},
}

0 comments on commit 1335df3

Please sign in to comment.