Skip to content

Commit

Permalink
Allow http client to accept default headers. (#251)
Browse files Browse the repository at this point in the history
* Allow http client to accept default headers.

* Test default client headers.
  • Loading branch information
matthewcarlreetz authored Oct 28, 2021
1 parent d5c9bd7 commit 1a45294
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/http/src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, {AxiosInstance, AxiosRequestConfig} from 'axios'
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
import * as rax from 'retry-axios'
import qs from 'qs'
import Network from './Network'
Expand Down Expand Up @@ -47,6 +47,7 @@ interface Options {
name?: string
userAgent?: string
retry?: number
headers?: Record<string, string>
}

export default class Client {
Expand All @@ -64,6 +65,7 @@ export default class Client {
this.network = network
this.axios = axios.create({
baseURL: this.network.endpoint,
headers: options?.headers,
})
this.name = options?.name
this.userAgent = options?.userAgent
Expand Down
41 changes: 39 additions & 2 deletions packages/http/src/__tests__/Client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import nock from 'nock'
import Client from '..'
import { Network } from '../'
import Client, { Network } from '..'

test('exposes a client instance with default options', () => {
const prodUrl = 'https://api.helium.io'
Expand Down Expand Up @@ -51,6 +50,26 @@ describe('get', () => {
})
})

it('client passes default headers to GET request', async () => {
const nameHeader = 'name-header-test'
const userAgentHeader = 'user-agent-test'
const network = 'helium'
nock('https://api.helium.io')
.get('/v1/greeting')
.reply(200, {
greeting: 'hello',
})

const opts = { name: nameHeader, userAgent: userAgentHeader, headers: { network } }
const client = new Client(Network.production, opts)
const response = await client.get('/greeting')

expect(response.data.greeting).toBe('hello')
expect(response.config.headers['x-client-name']).toBe(nameHeader)
expect(response.config.headers['User-Agent']).toBe(userAgentHeader)
expect(response.config.headers.network).toBe(network)
})

describe('post', () => {
it('creates a POST request to the full url', async () => {
nock('https://api.helium.io').post('/v1/greeting', { greeting: 'hello' }).reply(200, {
Expand All @@ -77,6 +96,24 @@ describe('post', () => {
expect(response.config.headers['x-client-name']).toBe(nameHeader)
expect(response.config.headers['User-Agent']).toBe(userAgentHeader)
})

it('client passes default headers to POST request', async () => {
const network = 'helium'
const nameHeader = 'name-header-test'
const userAgentHeader = 'user-agent-test'
nock('https://api.helium.io').post('/v1/greeting', { greeting: 'hello' }).reply(200, {
response: 'hey there!',
})
const opts = { name: nameHeader, userAgent: userAgentHeader, headers: { network } }
const client = new Client(Network.production, opts)
const params = { greeting: 'hello' }
const response = await client.post('/greeting', params)

expect(response.data.response).toBe('hey there!')
expect(response.config.headers['x-client-name']).toBe(nameHeader)
expect(response.config.headers['User-Agent']).toBe(userAgentHeader)
expect(response.config.headers.network).toBe(network)
})
})

describe('retry logic', () => {
Expand Down

0 comments on commit 1a45294

Please sign in to comment.