Skip to content

Commit

Permalink
feat-add test to check response code attached is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushagwl007 committed Sep 9, 2019
1 parent 3de90d0 commit f790e2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
17 changes: 17 additions & 0 deletions __tests__/servers/web/web.js
Expand Up @@ -710,6 +710,11 @@ describe('Server: Web', () => {
const validRandomKeys = ['key1', 'key2', 'key3']
const validRandomKey = validRandomKeys.indexOf(data.params.randomKey) > -1
if (!validRandomKey) {
if (data.params.randomKey === 'expired-key') {
const expiredError = new Error(`999: Key '${data.params.randomKey}' is expired`)
expiredError.code = 999
throw expiredError
}
const suspiciousError = new Error(`402: Suspicious Activity detected with key ${data.params.randomKey}`)
suspiciousError.code = 402
throw suspiciousError
Expand Down Expand Up @@ -803,6 +808,18 @@ describe('Server: Web', () => {
const receivedBody = await toJson(responseWithKeyAndQuery.body)
expect(receivedBody.good).toEqual(true)
})

test('should not work for 999 status code set using custom error and default error code, 400 is thrown', async () => {
try {
await request.post(url + '/api/statusTestAction', { form: { key: 'value', randomKey: 'expired-key' } })
throw new Error('should not get here')
} catch (error) {
expect(error.statusCode).not.toEqual(999)
expect(error.statusCode).toEqual(400)
const body = await toJson(error.response.body)
expect(body.error).toEqual('999: Key \'expired-key\' is expired')
}
})
})
})

Expand Down
6 changes: 4 additions & 2 deletions tutorials/web-server.md
Expand Up @@ -151,8 +151,9 @@ exports['default'] = {
},
// When true, returnErrorCodes will modify the response header for http(s) clients if connection.error is not null.
// You can also set connection.rawConnection.responseHttpCode to specify a code per request.
/**
* To create custom Error objects with custom response code you need to add the your response status code in the error object eg:
/**
* To create custom Error objects with custom response code you need to add the your response status code in the error object.
* // Note: Error code in error object will overwrite the response's status code.
* @example
*
* // Your action
Expand All @@ -173,6 +174,7 @@ exports['default'] = {
* }
* ...
* }
* // Only the values between 100 and 599 are accepted for status code, otherwise, it will be ignored.
* */
returnErrorCodes: true,
// should this node server attempt to gzip responses if the client can accept them?
Expand Down

0 comments on commit f790e2e

Please sign in to comment.