-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from kafkajs/set-user-agent-header
Send user-agent header with API requests
- Loading branch information
Showing
4 changed files
with
100 additions
and
2 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,23 @@ | ||
import API from '.' | ||
import { mockClient, install, uninstall } from 'mappersmith/test' | ||
|
||
const client = API({ clientId: 'test-client', host: 'http://example.com' }) | ||
const mock = mockClient<typeof client>(client) | ||
.resource('Schema') | ||
.method('find') | ||
.with({ id: 'abc' }) | ||
.response({}) | ||
.assertObject() | ||
|
||
describe('API Client', () => { | ||
beforeEach(() => install()) | ||
|
||
afterEach(() => uninstall()) | ||
|
||
it('should include a user agent header', async () => { | ||
const response = await client.Schema.find({ id: 'abc' }) | ||
|
||
expect(mock.callsCount()).toBe(1) | ||
expect(response.request().header('User-Agent')).not.toBeUndefined() | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Request } from 'mappersmith' | ||
|
||
import UserAgentMiddleware from './userAgent' | ||
|
||
const middlewareParams = (clientId?: string) => ({ | ||
resourceName: 'resourceNameMock', | ||
resourceMethod: 'resourceMethodMock', | ||
context: { context: 'contextMock' }, | ||
clientId, | ||
}) | ||
|
||
describe('UserAgentMiddleware', () => { | ||
let next, request | ||
|
||
beforeEach(() => { | ||
request = ({ | ||
enhance: jest.fn(), | ||
} as unknown) as jest.Mocked<Request> | ||
next = jest.fn().mockResolvedValue(request) | ||
}) | ||
|
||
describe('When the user has provided a clientId', () => { | ||
const params = middlewareParams('some-client-id') | ||
|
||
it('should add the client id as a user agent comment', async () => { | ||
const middleware = UserAgentMiddleware(params) | ||
|
||
await middleware.prepareRequest(next, jest.fn()) | ||
|
||
expect(request.enhance).toHaveBeenCalledWith({ | ||
headers: { | ||
'User-Agent': `@kafkajs/confluent-schema-registry (${params.clientId})`, | ||
}, | ||
}) | ||
}) | ||
}) | ||
|
||
describe('When the user has not provided a clientId', () => { | ||
const params = middlewareParams() | ||
|
||
it('should not include a comment in the user agent', async () => { | ||
const middleware = UserAgentMiddleware(params) | ||
|
||
await middleware.prepareRequest(next, jest.fn()) | ||
|
||
expect(request.enhance).toHaveBeenCalledWith({ | ||
headers: { | ||
'User-Agent': `@kafkajs/confluent-schema-registry`, | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) |
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,19 @@ | ||
import { Middleware } from 'mappersmith' | ||
import { DEFAULT_API_CLIENT_ID } from '../../constants' | ||
|
||
const product = '@kafkajs/confluent-schema-registry' | ||
|
||
const userAgentMiddleware: Middleware = ({ clientId }) => { | ||
const comment = clientId !== DEFAULT_API_CLIENT_ID ? clientId : undefined | ||
const userAgent = comment ? `${product} (${comment})` : product | ||
const headers = { | ||
'User-Agent': userAgent, | ||
} | ||
return { | ||
prepareRequest: next => { | ||
return next().then(req => req.enhance({ headers })) | ||
}, | ||
} | ||
} | ||
|
||
export default userAgentMiddleware |