Throw error if serve response status code is higher or equal than 400

This commit is contained in:
Juan Cruz Viotti 2014-10-31 13:25:02 -04:00
parent 62fc40cd4b
commit b52d1ccb6d
2 changed files with 14 additions and 2 deletions

View File

@ -14,6 +14,9 @@ exports.request = (method = 'GET', uri, json, callback) ->
try
response.body = JSON.parse(response.body)
if response?.statusCode >= 400
error = new Error(response.body)
return callback?.call(null, error, response, body)
exports.get = (uri, callback) ->

View File

@ -8,12 +8,14 @@ TEST_URI = config.baseUrl
URI =
ok: '/ok'
nojson: '/nojson'
error: '/error'
RESPONSE =
nojson: 'NO JSON RESPONSE'
STATUS =
ok: 'ok'
error: 'error'
METHODS = [
'GET'
@ -27,11 +29,12 @@ METHODS = [
describe 'Server', ->
beforeEach ->
nock(TEST_URI).get('/nojson').reply(200, RESPONSE.nojson)
nock(TEST_URI).get(URI.nojson).reply(200, RESPONSE.nojson)
nock(TEST_URI).get(URI.error).reply(400, status: STATUS.error)
for method in METHODS
lowercaseMethod = method.toLowerCase()
nock(TEST_URI)[lowercaseMethod]('/ok').reply(200, status: STATUS.ok)
nock(TEST_URI)[lowercaseMethod](URI.ok).reply(200, status: STATUS.ok)
describe '#request()', ->
@ -78,6 +81,12 @@ describe 'Server', ->
expect(error).to.be.an.instanceof(Error)
done()
it 'should throw an error if the status code is >= 400', (done) ->
server.request 'GET', URI.error, null, (error, response) ->
expect(error).to.exist
expect(error).to.be.an.instanceof(Error)
done()
checkRequestTypeWithoutBody = (type) ->
return (done) ->
lowercaseType = type.toLowerCase()