-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not retry POST requests by default (#197)
It doesn't really make sense to retry non-idempotent calls Also moving sanitised error over to a real error rather than a object. Makes it a little bit easier to test these: 'expect(..).reject.throws' etc.. doesn't work if you don't have really errors
- Loading branch information
1 parent
087bc0e
commit ded60e6
Showing
4 changed files
with
142 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import nock from 'nock' | ||
import RestClient from './restClient' | ||
import { AgentConfig } from '../config' | ||
|
||
const restClient = new RestClient( | ||
'name-1', | ||
{ | ||
url: 'http://localhost:8080/api', | ||
timeout: { | ||
response: 1000, | ||
deadline: 1000, | ||
}, | ||
agent: new AgentConfig(1000), | ||
}, | ||
'token-1', | ||
) | ||
|
||
describe('POST', () => { | ||
it('Should return response body', async () => { | ||
nock('http://localhost:8080', { | ||
reqheaders: { authorization: 'Bearer token-1' }, | ||
}) | ||
.post('/api/test') | ||
.reply(200, { success: true }) | ||
|
||
const result = await restClient.post({ | ||
path: '/test', | ||
}) | ||
|
||
expect(nock.isDone()).toBe(true) | ||
|
||
expect(result).toStrictEqual({ | ||
success: true, | ||
}) | ||
}) | ||
|
||
it('Should return raw response body', async () => { | ||
nock('http://localhost:8080', { | ||
reqheaders: { authorization: 'Bearer token-1' }, | ||
}) | ||
.post('/api/test') | ||
.reply(200, { success: true }) | ||
|
||
const result = await restClient.post({ | ||
path: '/test', | ||
headers: { header1: 'headerValue1' }, | ||
raw: true, | ||
}) | ||
|
||
expect(nock.isDone()).toBe(true) | ||
|
||
expect(result).toMatchObject({ | ||
req: { method: 'POST' }, | ||
status: 200, | ||
text: '{"success":true}', | ||
}) | ||
}) | ||
|
||
it('Should not retry by default', async () => { | ||
nock('http://localhost:8080', { | ||
reqheaders: { authorization: 'Bearer token-1' }, | ||
}) | ||
.post('/api/test') | ||
.reply(500) | ||
|
||
await expect( | ||
restClient.post({ | ||
path: '/test', | ||
headers: { header1: 'headerValue1' }, | ||
}), | ||
).rejects.toThrow('Internal Server Error') | ||
|
||
expect(nock.isDone()).toBe(true) | ||
}) | ||
|
||
it('retries if configured to do so', async () => { | ||
nock('http://localhost:8080', { | ||
reqheaders: { authorization: 'Bearer token-1' }, | ||
}) | ||
.post('/api/test') | ||
.reply(500) | ||
.post('/api/test') | ||
.reply(500) | ||
.post('/api/test') | ||
.reply(500) | ||
|
||
await expect( | ||
restClient.post({ | ||
path: '/test', | ||
headers: { header1: 'headerValue1' }, | ||
retry: true, | ||
}), | ||
).rejects.toThrow('Internal Server Error') | ||
|
||
expect(nock.isDone()).toBe(true) | ||
}) | ||
|
||
it('can recover through retries', async () => { | ||
nock('http://localhost:8080', { | ||
reqheaders: { authorization: 'Bearer token-1' }, | ||
}) | ||
.post('/api/test') | ||
.reply(500) | ||
.post('/api/test') | ||
.reply(500) | ||
.post('/api/test') | ||
.reply(200, { success: true }) | ||
|
||
const result = await restClient.post({ | ||
path: '/test', | ||
headers: { header1: 'headerValue1' }, | ||
retry: true, | ||
}) | ||
|
||
expect(result).toStrictEqual({ success: true }) | ||
expect(nock.isDone()).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters