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 setting a custom adapter #67

Merged
merged 3 commits into from
Jan 9, 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
5 changes: 4 additions & 1 deletion lib/create-http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const HOST_REGEX = /^(?!\w+:\/\/)([^\s:]+\.[^\s:]+)(?::(\d+))?(?!:)$/
* @prop {string=} host - Alternate host
* @prop {Object=} httpAgent - HTTP agent for node
* @prop {Object=} httpsAgent - HTTPS agent for node
* @prop {function=} adapter - Axios adapter to handle requests
* @prop {Object=} proxy - Axios proxy config
* @prop {Object=} headers - Additional headers
* @prop {function=} logHandler - A log handler function to process given log messages & errors. Receives the log level (error, warning & info) and the actual log data (Error object or string). (Default can be found here: https://github.com/contentful/contentful-sdk-core/blob/master/lib/create-http-client.js)
Expand All @@ -43,7 +44,8 @@ export default function createHttpClient (axios, options) {
httpsAgent: false,
timeout: 30000,
proxy: false,
basePath: ''
basePath: '',
adapter: false
}
const config = {
...defaultConfig,
Expand Down Expand Up @@ -97,6 +99,7 @@ export default function createHttpClient (axios, options) {
paramsSerializer: qs.stringify,
proxy: config.proxy,
timeout: config.timeout,
adapter: config.adapter,
// Contentful
logHandler: config.logHandler,
retryOnError: config.retryOnError
Expand Down
32 changes: 32 additions & 0 deletions test/unit/create-http-client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import MockAdapter from 'axios-mock-adapter'

const logHandlerStub = sinon.stub()
const mock = new MockAdapter(axios)

function setup () {
createHttpClientRewireApi.__Rewire__('rateLimit', sinon.stub())
sinon.stub(axios, 'create').returns({})
}

function teardown () {
createHttpClientRewireApi.__ResetDependency__('rateLimit')
mock.reset()
Expand Down Expand Up @@ -126,3 +128,33 @@ test('Calls axios based on passed hostname with invalid basePath and fixes the i
teardown()
t.end()
})

test('Can change the adapter axios uses', t => {
const testAdapter = function myAdapter (config) {
return new Promise(function (resolve, reject) {
var response = {
data: 'Adapter was used',
status: 200,
statusText: 'request.statusText',
headers: {},
config: config,
request: undefined
}
resolve(response)
})
}
setup()
const instance = createHttpClient(axios, {
accessToken: 'clientAccessToken',
space: 'clientSpaceId',
defaultHostname: 'defaulthost',
logHandler: logHandlerStub,
adapter: testAdapter
})

t.equals(axios.create.args[0][0].baseURL, 'https://defaulthost:443/spaces/clientSpaceId/')
t.equals(logHandlerStub.callCount, 0, 'does not log anything')
t.equals(instance.httpClientParams.adapter, testAdapter, 'client uses the custom adapter')
teardown()
t.end()
})