Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

Commit

Permalink
Merge pull request #6 from cantremember/issue_5_error_response
Browse files Browse the repository at this point in the history
invoke the API callback with a mocked response upon Error
  • Loading branch information
thinkingserious authored Oct 12, 2016
2 parents c4785c6 + 65a95fa commit 2901b0d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@ function Client (globalRequest) {
})

httpRequest.on('error', function (e) {
console.log('Error: ' + e.message)
var response = JSON.parse(JSON.stringify(emptyResponse))
response.statusCode = e.statusCode || 500
response.body = JSON.stringify({
message: e.message,
name: e.name,
stack: e.stack,
})
callback(response)
})

// if thre is a request body, sent it
Expand Down
39 changes: 39 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,44 @@ describe('Client', function () {
})
done()
})

it('should consider non-HTTP-200 repsonses to be valid', function (done) {
nock(TEST_HOST)
.get('/test')
.delay(100) // it('should wait for the response before invoking the callback')
.reply(503)
var requestGet = client.emptyRequest()
requestGet.method = 'GET'
requestGet.path = '/test'
client.API(requestGet, function (response) {
assert.equal(response.statusCode, '503', 'response.StatusCode equal 503')
assert.equal(response.body, '',
'response.body blank')
assert.equal(JSON.stringify(response.headers),
'{}',
'response.headers blank')
done()
})
})

it('should respond with a mock HTTP 500 response upon Error', function (done) {
nock(TEST_HOST)
.get('/test')
.replyWithError('ERROR')
var requestGet = client.emptyRequest()
requestGet.method = 'GET'
requestGet.path = '/test'
client.API(requestGet, function (response) {
assert.equal(response.statusCode, '500', 'response.StatusCode equal 500')
var body = JSON.parse(response.body)
assert.equal(body.message, 'ERROR', 'response.body.message equal ERROR')
assert.equal(body.name, Error.name, 'response.body.name equal Error.name')
assert.equal(typeof body.stack, 'string', 'response.body.stack is a String')
assert.equal(JSON.stringify(response.headers),
'{}',
'response.headers blank')
done()
})
})
})
})

0 comments on commit 2901b0d

Please sign in to comment.