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

Commit 65a95fa

Browse files
committed
#5 invoke the API callback with a mocked response upon Error
1 parent c4785c6 commit 65a95fa

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

lib/client.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,14 @@ function Client (globalRequest) {
110110
})
111111

112112
httpRequest.on('error', function (e) {
113-
console.log('Error: ' + e.message)
113+
var response = JSON.parse(JSON.stringify(emptyResponse))
114+
response.statusCode = e.statusCode || 500
115+
response.body = JSON.stringify({
116+
message: e.message,
117+
name: e.name,
118+
stack: e.stack,
119+
})
120+
callback(response)
114121
})
115122

116123
// if thre is a request body, sent it

test/test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,44 @@ describe('Client', function () {
169169
})
170170
done()
171171
})
172+
173+
it('should consider non-HTTP-200 repsonses to be valid', function (done) {
174+
nock(TEST_HOST)
175+
.get('/test')
176+
.delay(100) // it('should wait for the response before invoking the callback')
177+
.reply(503)
178+
var requestGet = client.emptyRequest()
179+
requestGet.method = 'GET'
180+
requestGet.path = '/test'
181+
client.API(requestGet, function (response) {
182+
assert.equal(response.statusCode, '503', 'response.StatusCode equal 503')
183+
assert.equal(response.body, '',
184+
'response.body blank')
185+
assert.equal(JSON.stringify(response.headers),
186+
'{}',
187+
'response.headers blank')
188+
done()
189+
})
190+
})
191+
192+
it('should respond with a mock HTTP 500 response upon Error', function (done) {
193+
nock(TEST_HOST)
194+
.get('/test')
195+
.replyWithError('ERROR')
196+
var requestGet = client.emptyRequest()
197+
requestGet.method = 'GET'
198+
requestGet.path = '/test'
199+
client.API(requestGet, function (response) {
200+
assert.equal(response.statusCode, '500', 'response.StatusCode equal 500')
201+
var body = JSON.parse(response.body)
202+
assert.equal(body.message, 'ERROR', 'response.body.message equal ERROR')
203+
assert.equal(body.name, Error.name, 'response.body.name equal Error.name')
204+
assert.equal(typeof body.stack, 'string', 'response.body.stack is a String')
205+
assert.equal(JSON.stringify(response.headers),
206+
'{}',
207+
'response.headers blank')
208+
done()
209+
})
210+
})
172211
})
173212
})

0 commit comments

Comments
 (0)