Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed calls to POST /v2/deliveries/{delivery_id}/cancel #15

Merged
merged 6 commits into from Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions __tests__/index.test.js
Expand Up @@ -111,6 +111,26 @@ describe('index', () => {
)
})
})
})

describe('HttpClient#performPost', () => {
beforeEach(() => {
httpClient = new HttpClient(authenticator)

nock(httpClient.authenticator.environment.baseUrl)
.post('/some-url')
.reply(200, {
some: 'response'
})

nock(httpClient.authenticator.environment.baseUrl)
.post('/some-url-that-returns-null-body')
.reply(204)

nock(httpClient.authenticator.environment.baseUrl)
.post('/make-tea')
.reply(418)
})

it('sends a post http request with correct parameters', () => {
const spy = jest.spyOn(Request, 'post')
Expand All @@ -123,5 +143,37 @@ describe('index', () => {
)
})
})

it('handles null response bodies', () => {
const spy = jest.spyOn(Request, 'post')
return httpClient.performPost('/some-url-that-returns-null-body').then(() => {
expect(spy).toHaveBeenCalledWith(
{'headers': {'Authorization': 'Bearer new-token', 'Content-Type': 'application/json', 'User-Agent': 'stuart-client-js/' + PackageJson.version},
'url': 'https://sandbox-api.stuart.com/some-url-that-returns-null-body'},
expect.anything()
)
})
})

it('assumes 200 responses are successful', () => {
const spy = jest.spyOn(Request, 'post')
return httpClient.performPost('/some-url').then((response) => {
expect(response.success()).toBe(true)
})
})

it('assumes 204 responses are successful', () => {
const spy = jest.spyOn(Request, 'post')
return httpClient.performPost('/some-url-that-returns-null-body').then((response) => {
expect(response.success()).toBe(true)
})
})

it('assumes non 2xx responses are failures', () => {
const spy = jest.spyOn(Request, 'post')
return httpClient.performPost('/make-tea').then((response) => {
expect(response.success()).toBe(false)
})
})
})
})
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -47,7 +47,7 @@ class ApiResponse {
}

success () {
return this.statusCode === 200 || this.statusCode === 201
return this.statusCode >= 200 && this.statusCode < 300
}
}

Expand Down Expand Up @@ -81,7 +81,7 @@ class HttpClient {
}

Request.post(options, (err, res) => resolve(
new ApiResponse(res.statusCode, JSON.parse(res.body))))
new ApiResponse(res.statusCode, JSON.parse(res.body || '{}'))))
}).catch(error => { reject(error) })
})
};
Expand Down