Skip to content

Commit

Permalink
#113: Extending req/res mock to include send(), updating mocha for se…
Browse files Browse the repository at this point in the history
…curity vulnerability
  • Loading branch information
TigerC10 committed May 15, 2018
1 parent 3c71912 commit 35588c1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 25 additions & 4 deletions lib/framework/koa.js
Expand Up @@ -108,17 +108,36 @@ function authenticate(passport, name, options, callback) {
// mock the `req` object
const req = createReqMock(ctx, options.assignProperty || passport._userProperty || 'user')

/*
* res.end & res.send should basically behave the same
* res.end only takes strings (but should be okay for the mock to take more)
* res.send can send a buffer, object, array, or string
*/
function setBodyAndResolve(content) {
switch (typeof content) {
case 'Buffer':
ctx.body = content.toString()
break
case 'object':
ctx.body = JSON.stringify(content)
break
case 'string':
default:
ctx.body = content
break
}
resolve(false)
}

// mock the `res` object
const res = {
redirect: function(url) {
ctx.redirect(url)
resolve(false)
},
setHeader: ctx.set.bind(ctx),
end: function(content) {
if (content) ctx.body = content
resolve(false)
},
end: setBodyAndResolve,
send: setBodyAndResolve,
set statusCode(status) {
ctx.status = status
},
Expand All @@ -127,6 +146,8 @@ function authenticate(passport, name, options, callback) {
}
}

req.res = res

// update the custom callback above
if (callback) {
callback.resolve = resolve
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -24,7 +24,7 @@
"koa": "^2.3.0",
"koa-bodyparser": "^4.2.0",
"koa-route": "^3.2",
"mocha": "^3.5.0",
"mocha": "^5.1.1",
"passport-local": "^1.0",
"supertest": "^3.0"
},
Expand Down

0 comments on commit 35588c1

Please sign in to comment.