mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-18 21:27:51 +00:00
Throw error if serve response status code is higher or equal than 400
This commit is contained in:
parent
62fc40cd4b
commit
b52d1ccb6d
@ -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) ->
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user