Skip to content

Commit

Permalink
feat: Allow to disable payload and query string extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasdao committed Dec 27, 2017
1 parent 62891a4 commit 2b19f56
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
18 changes: 11 additions & 7 deletions src/webfunc.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,11 @@ const serveHttp = (arg1, arg2, arg3) => {
parameters = r.parameters
}

const extractParams = _appconfig.extractParams == undefined || _appconfig.extractParams
const getParams = extractParams ? getRequestParameters(req) : Promise.resolve({})
return handleHttpRequest(req, res, _appconfig)
.then(() => !res.headersSent
? setResponseHeaders(res, _appconfig).then(res => getRequestParameters(req).then(paramts => httpNextRequest(req, res, Object.assign(parameters, paramts))))
? setResponseHeaders(res, _appconfig).then(res => getParams.then(paramts => httpNextRequest(req, res, extractParams ? Object.assign(parameters, paramts) : {})))
: res)
.then(() => ({ req, res, ctx }))
}
Expand Down Expand Up @@ -461,7 +463,7 @@ const serveHttpEndpoints = (endpoints, appconfig) => {
req.__transactionId = shortid.generate().replace(/-/g, 'r').replace(/_/g, '9')
// 1. Pre process request
return preProcess(req, res)
// 2. Capture pre processing errors
// 2. Capture pre processing errors
.catch(err => {
try {
return setResponseHeaders(res, _appconfig).then(res => {
Expand All @@ -473,7 +475,7 @@ const serveHttpEndpoints = (endpoints, appconfig) => {
return { req, res, __err: err }
}
})
// 3. Process request
// 3. Process request
.then((ctx={}) => {
if (ctx && ctx.__err)
return { req, res, __err: ctx.__err }
Expand Down Expand Up @@ -503,12 +505,14 @@ const serveHttpEndpoints = (endpoints, appconfig) => {
return res.status(500).send(`Wrong argument exception. Endpoint '${httpEndpoint}' for method ${httpMethod} defines a 'next' argument that is not a function similar to '(req, res, params) => ...'.`)

const paramts = Object.assign({}, endpoint.winningRoute.parameters)
return getRequestParameters(req).then(parameters => next(req, res, Object.assign(parameters, paramts)))
const extractParams = _appconfig.extractParams == undefined || _appconfig.extractParams
const getParams = extractParams ? getRequestParameters(req) : Promise.resolve({})
return getParams.then(parameters => next(req, res, extractParams ? Object.assign(parameters, paramts) : {}))
})
: res)
.then(() => ({ req, res, ctx }))
})
// 4. Capture processing errors
// 4. Capture processing errors
.catch(err => {
try {
return setResponseHeaders(res, _appconfig).then(res => {
Expand All @@ -520,13 +524,13 @@ const serveHttpEndpoints = (endpoints, appconfig) => {
return { req, res, __err: err }
}
})
// 5. Post processing request
// 5. Post processing request
.then(({ req, res, ctx }) => {
if (!ctx)
ctx = {}
ctx.__ellapsedMillis = Date.now() - start
return postProcess(req, res, ctx)
// 6. Capture post processing errors
// 6. Capture post processing errors
.catch(err => {
try {
return setResponseHeaders(res, _appconfig).then(res => {
Expand Down
48 changes: 45 additions & 3 deletions test/webfunc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,9 @@ describe('webfunc', () =>
body: {
username: 'nic',
password: '1234'
},
_parsedUrl: {
pathname: '/users/create'
}
})
const res = httpMocks.createResponse()
Expand All @@ -1438,15 +1441,54 @@ describe('webfunc', () =>
'Access-Control-Max-Age': '1296000'
}
}
const fn = serveHttp((req, res, params) => {
res.status(200).send(`The secret password of ${params.username} is ${params.password}`)
const fn = serveHttp('users/:action', (req, res, params) => {
res.status(200).send(`Action ${params.action}. The secret password of ${params.username} is ${params.password}`)
return res
}, appconfig)
return fn(req, res).then(() => {
assert.isOk(req)
assert.equal(res.statusCode, 200)
assert.equal(res._getData(), 'The secret password of nic is 1234')
assert.equal(res._getData(), 'Action create. The secret password of nic is 1234')
})
})))

/*eslint-disable */
describe('webfunc', () =>
describe('#serveHttp: 23', () =>
it(`Should not extract any parameters from the payload or the query string when the 'extractParams' property is set to false.`, () => {
/*eslint-enable */
const req = httpMocks.createRequest({
method: 'POST',
headers: {
origin: 'http://localhost:8080',
referer: 'http://localhost:8080'
},
body: {
username: 'nic',
password: '1234'
},
_parsedUrl: {
pathname: '/users/create'
}
})
const res = httpMocks.createResponse()
const appconfig = {
headers: {
'Access-Control-Allow-Methods': 'GET, HEAD, OPTIONS, POST',
'Access-Control-Allow-Headers': 'Authorization, Content-Type, Origin',
'Access-Control-Allow-Origin': 'http://boris.com, http://localhost:8080',
'Access-Control-Max-Age': '1296000'
},
extractParams: false
}
const fn = serveHttp('users/:action', (req, res, params) => {
res.status(200).send(`Action ${params.action}. The secret password of ${params.username} is ${params.password}`)
return res
}, appconfig)
return fn(req, res).then(() => {
assert.isOk(req)
assert.equal(res.statusCode, 200)
assert.equal(res._getData(), 'Action undefined. The secret password of undefined is undefined')
})
})))

0 comments on commit 2b19f56

Please sign in to comment.