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

Allow retry limit to be changed #83

Merged
merged 1 commit into from
Sep 18, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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