-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(httpResponse): pass httpResponse from codemaster
- Loading branch information
1 parent
fd1920e
commit c6a1eb6
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
exports.success = (text, keys, values) => { | ||
const json = { | ||
message: text, | ||
}; | ||
|
||
if (Object.prototype.toString.call(keys) === '[object Array]') { | ||
for (let i = 0; i < keys.length; i++) { | ||
json[keys[i]] = values[i]; | ||
} | ||
} else { | ||
json[keys] = values; | ||
} | ||
return json; | ||
}; | ||
|
||
exports.error = function buildErrorJSON(error, fullMessage, code) { | ||
if (code === 500) error = 'Internal Server Error'; | ||
const json = { error }; | ||
const environment = process.env.NODE_ENV || 'development'; | ||
if (environment !== 'production') { | ||
json.fullError = fullMessage; | ||
} | ||
return json; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// During the test the env variable is set to test | ||
process.env.NODE_ENV = 'test'; | ||
|
||
// Require the dev-dependencies | ||
const chai = require('chai'); | ||
const { httpResponse } = require('../../../'); | ||
|
||
const { assert } = chai; | ||
|
||
|
||
// Our parent block | ||
describe('Http Response: Error', () => { // eslint-disable-line | ||
|
||
|
||
it('code is 500, not include err', (done) => { // eslint-disable-line | ||
const result = httpResponse.error('test', 'full msg', 500); | ||
const expected = { | ||
error: 'Internal Server Error', | ||
fullError: 'full msg', | ||
}; | ||
assert.deepEqual(result, expected); | ||
done(); | ||
}); | ||
|
||
it('code is not 500, include err', (done) => { // eslint-disable-line | ||
const result = httpResponse.error('test', 'full msg'); | ||
const expected = { | ||
error: 'test', | ||
fullError: 'full msg', | ||
}; | ||
assert.deepEqual(result, expected); | ||
done(); | ||
}); | ||
|
||
it('in production do not include full error', (done) => { // eslint-disable-line | ||
process.env.NODE_ENV = 'production'; | ||
const result = httpResponse.error('test', 'full msg'); | ||
const expected = { | ||
error: 'test', | ||
}; | ||
assert.deepEqual(result, expected); | ||
done(); | ||
}); | ||
|
||
it('NODE_ENV not specified', (done) => { // eslint-disable-line | ||
delete process.env.NODE_ENV; | ||
const result = httpResponse.error('test', 'full msg'); | ||
const expected = { | ||
error: 'test', | ||
fullError: 'full msg', | ||
}; | ||
assert.deepEqual(result, expected); | ||
done(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// During the test the env variable is set to test | ||
process.env.NODE_ENV = 'test'; | ||
|
||
// Require the dev-dependencies | ||
const chai = require('chai'); | ||
const { httpResponse } = require('../../..'); | ||
|
||
const { assert } = chai; | ||
|
||
|
||
// Our parent block | ||
describe('Http Response: Success', () => { // eslint-disable-line | ||
|
||
|
||
it('keys is null', (done) => { // eslint-disable-line | ||
const result = httpResponse.success('test', null, '500'); | ||
const expected = { | ||
message: 'test', | ||
null: '500', | ||
}; | ||
assert.deepEqual(result, expected); | ||
done(); | ||
}); | ||
|
||
it('keys is array', (done) => { // eslint-disable-line | ||
const result = httpResponse.success('test', ['k1', 'k2', 'k3'], ['v1', 'v2', 'v3']); | ||
const expected = { | ||
message: 'test', | ||
k1: 'v1', | ||
k2: 'v2', | ||
k3: 'v3', | ||
}; | ||
assert.deepEqual(result, expected); | ||
done(); | ||
}); | ||
|
||
it('keys is not array', (done) => { // eslint-disable-line | ||
process.env.NODE_ENV = 'production'; | ||
const result = httpResponse.success('test', 'k', 'v'); | ||
const expected = { | ||
message: 'test', | ||
k: 'v', | ||
}; | ||
assert.deepEqual(result, expected); | ||
done(); | ||
}); | ||
}); |