-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to pass custom http client into node (#880)
- Loading branch information
Showing
28 changed files
with
543 additions
and
276 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,5 @@ | ||
--- | ||
'@segment/analytics-node': minor | ||
--- | ||
|
||
Add `httpClient` setting. This allow users to override default HTTP client with a custom one. |
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 |
---|---|---|
|
@@ -42,7 +42,8 @@ | |
}, | ||
"devDependencies": { | ||
"@internal/config": "0.0.0", | ||
"@types/node": "^16" | ||
"@types/node": "^16", | ||
"axios": "^1.4.0" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
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
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
69 changes: 69 additions & 0 deletions
69
packages/node/src/__tests__/http-client.integration.test.ts
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,69 @@ | ||
import { FetchHTTPClient, HTTPFetchFn } from '..' | ||
import { AbortSignal as AbortSignalShim } from '../lib/abort' | ||
import { httpClientOptionsBodyMatcher } from './test-helpers/assert-shape/segment-http-api' | ||
import { createTestAnalytics } from './test-helpers/create-test-analytics' | ||
import { createSuccess } from './test-helpers/factories' | ||
|
||
const testFetch: jest.MockedFn<HTTPFetchFn> = jest | ||
.fn() | ||
.mockResolvedValue(createSuccess()) | ||
|
||
let analytics: ReturnType<typeof createTestAnalytics> | ||
|
||
const helpers = { | ||
makeTrackCall: () => | ||
new Promise((resolve) => | ||
analytics.track({ event: 'foo', userId: 'bar' }, resolve) | ||
), | ||
assertFetchCallRequest: ( | ||
...[url, options]: typeof testFetch['mock']['lastCall'] | ||
) => { | ||
expect(url).toBe('https://api.segment.io/v1/batch') | ||
expect(options.headers).toEqual({ | ||
Authorization: 'Basic Zm9vOg==', | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'analytics-node-next/latest', | ||
}) | ||
expect(options.method).toBe('POST') | ||
const getLastBatch = (): object[] => { | ||
const [, options] = testFetch.mock.lastCall | ||
const batch = JSON.parse(options.body!).batch | ||
return batch | ||
} | ||
const batch = getLastBatch() | ||
expect(batch.length).toBe(1) | ||
expect(batch[0]).toEqual({ | ||
...httpClientOptionsBodyMatcher, | ||
timestamp: expect.stringMatching( | ||
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/ | ||
), | ||
properties: {}, | ||
event: 'foo', | ||
type: 'track', | ||
userId: 'bar', | ||
}) | ||
// @ts-ignore | ||
expect(options.signal).toBeInstanceOf( | ||
typeof AbortSignal !== 'undefined' ? AbortSignal : AbortSignalShim | ||
) | ||
}, | ||
} | ||
|
||
describe('httpClient option', () => { | ||
it('can be a regular custom HTTP client', async () => { | ||
analytics = createTestAnalytics({ | ||
httpClient: new FetchHTTPClient(testFetch), | ||
}) | ||
expect(testFetch).toHaveBeenCalledTimes(0) | ||
await helpers.makeTrackCall() | ||
expect(testFetch).toHaveBeenCalledTimes(1) | ||
helpers.assertFetchCallRequest(...testFetch.mock.lastCall) | ||
}) | ||
it('can be a simple function that matches the fetch interface', async () => { | ||
analytics = createTestAnalytics({ httpClient: testFetch }) | ||
expect(testFetch).toHaveBeenCalledTimes(0) | ||
await helpers.makeTrackCall() | ||
expect(testFetch).toHaveBeenCalledTimes(1) | ||
helpers.assertFetchCallRequest(...testFetch.mock.lastCall) | ||
}) | ||
}) |
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
Oops, something went wrong.