mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
* added kibana version to user-agent * Update x-pack/plugins/fleet/server/services/epm/registry/requests.ts Co-authored-by: Nicolas Chaulet <n.chaulet@gmail.com> Co-authored-by: Nicolas Chaulet <n.chaulet@gmail.com> Co-authored-by: Julia Bardi <90178898+juliaElastic@users.noreply.github.com> Co-authored-by: Nicolas Chaulet <n.chaulet@gmail.com>
This commit is contained in:
parent
6ec6fd2edf
commit
0cd4bcab3b
2 changed files with 52 additions and 5 deletions
|
@ -6,10 +6,24 @@
|
|||
*/
|
||||
|
||||
import { RegistryError, RegistryConnectionError, RegistryResponseError } from '../../../errors';
|
||||
import { appContextService } from '../../app_context';
|
||||
|
||||
import { fetchUrl } from './requests';
|
||||
import { fetchUrl, getResponse } from './requests';
|
||||
jest.mock('node-fetch');
|
||||
|
||||
let mockRegistryProxyUrl: string | undefined;
|
||||
jest.mock('./proxy', () => ({
|
||||
getProxyAgent: jest.fn().mockReturnValue('proxy agent'),
|
||||
getRegistryProxyUrl: () => mockRegistryProxyUrl,
|
||||
}));
|
||||
|
||||
jest.mock('../../app_context', () => ({
|
||||
appContextService: {
|
||||
getKibanaVersion: jest.fn(),
|
||||
getLogger: jest.fn().mockReturnValue({ debug: jest.fn() }),
|
||||
},
|
||||
}));
|
||||
|
||||
const { Response, FetchError } = jest.requireActual('node-fetch');
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const fetchMock = require('node-fetch') as jest.Mock;
|
||||
|
@ -22,6 +36,35 @@ describe('Registry request', () => {
|
|||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('fetch options', () => {
|
||||
beforeEach(() => {
|
||||
fetchMock.mockImplementationOnce(() => Promise.resolve(new Response('')));
|
||||
(appContextService.getKibanaVersion as jest.Mock).mockReturnValue('8.0.0');
|
||||
});
|
||||
it('should set User-Agent header including kibana version', async () => {
|
||||
getResponse('');
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith('', {
|
||||
headers: {
|
||||
'User-Agent': 'Kibana/8.0.0 node-fetch',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should set User-Agent header including kibana version with agent', async () => {
|
||||
mockRegistryProxyUrl = 'url';
|
||||
|
||||
getResponse('');
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith('', {
|
||||
agent: 'proxy agent',
|
||||
headers: {
|
||||
'User-Agent': 'Kibana/8.0.0 node-fetch',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('fetchUrl / getResponse errors', () => {
|
||||
it('regular Errors do not retry. Becomes RegistryError', async () => {
|
||||
fetchMock.mockImplementationOnce(() => {
|
||||
|
|
|
@ -88,15 +88,19 @@ function isSystemError(error: FailedAttemptErrors): boolean {
|
|||
}
|
||||
|
||||
export function getFetchOptions(targetUrl: string): RequestInit | undefined {
|
||||
const options: RequestInit = {
|
||||
headers: {
|
||||
'User-Agent': `Kibana/${appContextService.getKibanaVersion()} node-fetch`,
|
||||
},
|
||||
};
|
||||
const proxyUrl = getRegistryProxyUrl();
|
||||
if (!proxyUrl) {
|
||||
return undefined;
|
||||
return options;
|
||||
}
|
||||
|
||||
const logger = appContextService.getLogger();
|
||||
logger.debug(`Using ${proxyUrl} as proxy for ${targetUrl}`);
|
||||
|
||||
return {
|
||||
agent: getProxyAgent({ proxyUrl, targetUrl }),
|
||||
};
|
||||
options.agent = getProxyAgent({ proxyUrl, targetUrl });
|
||||
return options;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue