Skip to content

Commit

Permalink
feat(timeout/retry): allow retry limit to be changed (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-contentful committed Sep 18, 2019
1 parent dfd8528 commit 6e70a49
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/create-http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ export default function createHttpClient (axios, options) {
...newParams
})
}
rateLimit(instance, axiosOptions)
rateLimit(instance, axiosOptions, config.retryLimit)
return instance
}
36 changes: 32 additions & 4 deletions test/unit/rate-limit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const logHandlerStub = sinon.stub()

const mock = new MockAdapter(axios)

function setup () {
const client = axios.create({
function setup (options = {}) {
const client = axios.create(Object.assign({
logHandler: logHandlerStub,
retryOnError: true
})
}, options))
rateLimit(client, {
logHandler: logHandlerStub,
retryOnError: true
})
}, options.retryLimit)
return { client }
}

Expand Down Expand Up @@ -92,6 +92,34 @@ test('Retry on 5** - multiple errors', (t) => {
})
})

test('Retry on 5** - multiple errors - reach/exceed limit', (t) => {
const { client } = setup({ retryLimit: 7 })
mock.onGet('/rate-limit-me').replyOnce(500, 'Server Error', { 'x-contentful-request-id': 12345 })
mock.onGet('/rate-limit-me').replyOnce(500, 'Server Error', { 'x-contentful-request-id': 12345 })
mock.onGet('/rate-limit-me').replyOnce(503, 'Another Server Error', { 'x-contentful-request-id': 12345 })
mock.onGet('/rate-limit-me').replyOnce(500, 'Server Error', { 'x-contentful-request-id': 12345 })
mock.onGet('/rate-limit-me').replyOnce(503, 'Another Server Error', { 'x-contentful-request-id': 12345 })
mock.onGet('/rate-limit-me').replyOnce(500, 'Server Error', { 'x-contentful-request-id': 12345 })
mock.onGet('/rate-limit-me').replyOnce(503, 'Another Server Error', { 'x-contentful-request-id': 12345 })
mock.onGet('/rate-limit-me').replyOnce(200, 'works')
mock.onGet('/rate-limit-me').replyOnce(500, 'Server Error', { 'x-contentful-request-id': 12345 })

t.plan(3)

return client.get('/rate-limit-me').then((response) => {
t.ok(response.data)
t.equals(response.data, 'works')
}).then(() => {
return client.get('/rate-limit-me').then(() => {
t.fail('Promise should reject not resolve')
teardown()
}).catch((error) => {
t.equals(error.message, 'Request failed with status code 500')
teardown()
})
})
})

test('Retry on network error', (t) => {
const { client } = setupWithOneRetry()
mock.onGet('/rate-limit-me').networkError()
Expand Down

0 comments on commit 6e70a49

Please sign in to comment.