-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[IM] Remove axios dependency in tests
#128171
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
Changes from all commits
809c581
ec80ba5
dd405c1
ae9c3c9
8afc145
ba71517
eee9c7c
500a75e
e8a291c
455a328
4bdcb26
ea7e7c5
feac4c0
73d3108
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,10 +5,11 @@ | |
| * 2.0. | ||
| */ | ||
|
|
||
| import sinon, { SinonFakeServer } from 'sinon'; | ||
| import { httpServiceMock } from '../../../../../../src/core/public/mocks'; | ||
| import { API_BASE_PATH } from '../../../common/constants'; | ||
|
|
||
| type HttpResponse = Record<string, any> | any[]; | ||
| type HttpMethod = 'GET' | 'PUT' | 'DELETE' | 'POST'; | ||
|
|
||
| export interface ResponseError { | ||
| statusCode: number; | ||
|
|
@@ -17,139 +18,105 @@ export interface ResponseError { | |
| } | ||
|
|
||
| // Register helpers to mock HTTP Requests | ||
| const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { | ||
| const setLoadTemplatesResponse = (response: HttpResponse = []) => { | ||
| server.respondWith('GET', `${API_BASE_PATH}/index_templates`, [ | ||
| 200, | ||
| { 'Content-Type': 'application/json' }, | ||
| JSON.stringify(response), | ||
| ]); | ||
| }; | ||
|
|
||
| const setLoadIndicesResponse = (response: HttpResponse = []) => { | ||
| server.respondWith('GET', `${API_BASE_PATH}/indices`, [ | ||
| 200, | ||
| { 'Content-Type': 'application/json' }, | ||
| JSON.stringify(response), | ||
| ]); | ||
| }; | ||
|
|
||
| const setReloadIndicesResponse = (response: HttpResponse = []) => { | ||
| server.respondWith('POST', `${API_BASE_PATH}/indices/reload`, [ | ||
| 200, | ||
| { 'Content-Type': 'application/json' }, | ||
| JSON.stringify(response), | ||
| ]); | ||
| }; | ||
|
|
||
| const setLoadDataStreamsResponse = (response: HttpResponse = []) => { | ||
| server.respondWith('GET', `${API_BASE_PATH}/data_streams`, [ | ||
| 200, | ||
| { 'Content-Type': 'application/json' }, | ||
| JSON.stringify(response), | ||
| ]); | ||
| }; | ||
|
|
||
| const setLoadDataStreamResponse = (response: HttpResponse = []) => { | ||
| server.respondWith('GET', `${API_BASE_PATH}/data_streams/:id`, [ | ||
| 200, | ||
| { 'Content-Type': 'application/json' }, | ||
| JSON.stringify(response), | ||
| ]); | ||
| }; | ||
|
|
||
| const setDeleteDataStreamResponse = (response: HttpResponse = []) => { | ||
| server.respondWith('POST', `${API_BASE_PATH}/delete_data_streams`, [ | ||
| 200, | ||
| { 'Content-Type': 'application/json' }, | ||
| JSON.stringify(response), | ||
| ]); | ||
| }; | ||
|
|
||
| const setDeleteTemplateResponse = (response: HttpResponse = []) => { | ||
| server.respondWith('POST', `${API_BASE_PATH}/delete_index_templates`, [ | ||
| 200, | ||
| { 'Content-Type': 'application/json' }, | ||
| JSON.stringify(response), | ||
| ]); | ||
| }; | ||
|
|
||
| const setLoadTemplateResponse = (response?: HttpResponse, error?: any) => { | ||
| const status = error ? error.status || 400 : 200; | ||
| const body = error ? error.body : response; | ||
|
|
||
| server.respondWith('GET', `${API_BASE_PATH}/index_templates/:id`, [ | ||
| status, | ||
| { 'Content-Type': 'application/json' }, | ||
| JSON.stringify(body), | ||
| ]); | ||
| }; | ||
|
|
||
| const setCreateTemplateResponse = (response?: HttpResponse, error?: any) => { | ||
| const status = error ? error.body.status || 400 : 200; | ||
| const body = error ? JSON.stringify(error.body) : JSON.stringify(response); | ||
|
|
||
| server.respondWith('POST', `${API_BASE_PATH}/index_templates`, [ | ||
| status, | ||
| { 'Content-Type': 'application/json' }, | ||
| body, | ||
| ]); | ||
| }; | ||
|
|
||
| const setUpdateTemplateResponse = (response?: HttpResponse, error?: any) => { | ||
| const status = error ? error.status || 400 : 200; | ||
| const body = error ? JSON.stringify(error.body) : JSON.stringify(response); | ||
|
|
||
| server.respondWith('PUT', `${API_BASE_PATH}/index_templates/:name`, [ | ||
| status, | ||
| { 'Content-Type': 'application/json' }, | ||
| body, | ||
| ]); | ||
| }; | ||
|
|
||
| const setUpdateIndexSettingsResponse = (response?: HttpResponse, error?: ResponseError) => { | ||
| const status = error ? error.statusCode || 400 : 200; | ||
| const body = error ?? response; | ||
|
|
||
| server.respondWith('PUT', `${API_BASE_PATH}/settings/:name`, [ | ||
| status, | ||
| { 'Content-Type': 'application/json' }, | ||
| JSON.stringify(body), | ||
| ]); | ||
| }; | ||
|
|
||
| const setSimulateTemplateResponse = (response?: HttpResponse, error?: any) => { | ||
| const status = error ? error.status || 400 : 200; | ||
| const body = error ? JSON.stringify(error.body) : JSON.stringify(response); | ||
|
|
||
| server.respondWith('POST', `${API_BASE_PATH}/index_templates/simulate`, [ | ||
| status, | ||
| { 'Content-Type': 'application/json' }, | ||
| body, | ||
| ]); | ||
| }; | ||
|
|
||
| const setLoadComponentTemplatesResponse = (response?: HttpResponse, error?: any) => { | ||
| const status = error ? error.status || 400 : 200; | ||
| const body = error ? error.body : response; | ||
|
|
||
| server.respondWith('GET', `${API_BASE_PATH}/component_templates`, [ | ||
| status, | ||
| { 'Content-Type': 'application/json' }, | ||
| JSON.stringify(body), | ||
| ]); | ||
| }; | ||
|
|
||
| const setLoadNodesPluginsResponse = (response?: HttpResponse, error?: any) => { | ||
| const status = error ? error.status || 400 : 200; | ||
| const body = error ? error.body : response; | ||
|
|
||
| server.respondWith('GET', `${API_BASE_PATH}/nodes/plugins`, [ | ||
| status, | ||
| { 'Content-Type': 'application/json' }, | ||
| JSON.stringify(body), | ||
| ]); | ||
| }; | ||
| const registerHttpRequestMockHelpers = ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Starting here to L56 seems to be the same code across the test refactoring PRs. Does it make sense to try to centralize it somewhere so it can be reused?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you agree, I think this can be done as a separate PR
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've suggested also to put this somewhere in |
||
| httpSetup: ReturnType<typeof httpServiceMock.createStartContract> | ||
| ) => { | ||
| const mockResponses = new Map<HttpMethod, Map<string, Promise<unknown>>>( | ||
| ['GET', 'PUT', 'DELETE', 'POST'].map( | ||
| (method) => [method, new Map()] as [HttpMethod, Map<string, Promise<unknown>>] | ||
| ) | ||
| ); | ||
|
|
||
| const mockMethodImplementation = (method: HttpMethod, path: string) => { | ||
| return mockResponses.get(method)?.get(path) ?? Promise.resolve({}); | ||
| }; | ||
|
|
||
| httpSetup.get.mockImplementation((path) => | ||
| mockMethodImplementation('GET', path as unknown as string) | ||
| ); | ||
| httpSetup.delete.mockImplementation((path) => | ||
| mockMethodImplementation('DELETE', path as unknown as string) | ||
| ); | ||
| httpSetup.post.mockImplementation((path) => | ||
| mockMethodImplementation('POST', path as unknown as string) | ||
| ); | ||
| httpSetup.put.mockImplementation((path) => | ||
| mockMethodImplementation('PUT', path as unknown as string) | ||
| ); | ||
|
|
||
| const mockResponse = (method: HttpMethod, path: string, response?: unknown, error?: unknown) => { | ||
| const defuse = (promise: Promise<unknown>) => { | ||
| promise.catch(() => {}); | ||
| return promise; | ||
| }; | ||
|
|
||
| return mockResponses | ||
| .get(method)! | ||
| .set(path, error ? defuse(Promise.reject({ body: error })) : Promise.resolve(response)); | ||
| }; | ||
|
|
||
| const setLoadTemplatesResponse = (response?: HttpResponse, error?: ResponseError) => | ||
| mockResponse('GET', `${API_BASE_PATH}/index_templates`, response, error); | ||
|
|
||
| const setLoadIndicesResponse = (response?: HttpResponse, error?: ResponseError) => | ||
| mockResponse('GET', `${API_BASE_PATH}/indices`, response, error); | ||
|
|
||
| const setReloadIndicesResponse = (response?: HttpResponse, error?: ResponseError) => | ||
| mockResponse('POST', `${API_BASE_PATH}/indices/reload`, response, error); | ||
|
|
||
| const setLoadDataStreamsResponse = (response?: HttpResponse, error?: ResponseError) => | ||
| mockResponse('GET', `${API_BASE_PATH}/data_streams`, response, error); | ||
|
|
||
| const setLoadDataStreamResponse = ( | ||
| dataStreamId: string, | ||
| response?: HttpResponse, | ||
| error?: ResponseError | ||
| ) => | ||
| mockResponse( | ||
| 'GET', | ||
| `${API_BASE_PATH}/data_streams/${encodeURIComponent(dataStreamId)}`, | ||
| response, | ||
| error | ||
| ); | ||
|
|
||
| const setDeleteDataStreamResponse = (response?: HttpResponse, error?: ResponseError) => | ||
| mockResponse('POST', `${API_BASE_PATH}/delete_data_streams`, response, error); | ||
|
|
||
| const setDeleteTemplateResponse = (response?: HttpResponse, error?: ResponseError) => | ||
| mockResponse('POST', `${API_BASE_PATH}/delete_index_templates`, response, error); | ||
|
|
||
| const setLoadTemplateResponse = ( | ||
| templateId: string, | ||
| response?: HttpResponse, | ||
| error?: ResponseError | ||
| ) => mockResponse('GET', `${API_BASE_PATH}/index_templates/${templateId}`, response, error); | ||
|
|
||
| const setCreateTemplateResponse = (response?: HttpResponse, error?: ResponseError) => | ||
| mockResponse('POST', `${API_BASE_PATH}/index_templates`, response, error); | ||
|
|
||
| const setUpdateTemplateResponse = ( | ||
| templateId: string, | ||
| response?: HttpResponse, | ||
| error?: ResponseError | ||
| ) => mockResponse('PUT', `${API_BASE_PATH}/index_templates/${templateId}`, response, error); | ||
|
|
||
| const setUpdateIndexSettingsResponse = ( | ||
| indexName: string, | ||
| response?: HttpResponse, | ||
| error?: ResponseError | ||
| ) => mockResponse('PUT', `${API_BASE_PATH}/settings/${indexName}`, response, error); | ||
|
|
||
| const setSimulateTemplateResponse = (response?: HttpResponse, error?: ResponseError) => | ||
| mockResponse('POST', `${API_BASE_PATH}/index_templates/simulate`, response, error); | ||
|
|
||
| const setLoadComponentTemplatesResponse = (response?: HttpResponse, error?: ResponseError) => | ||
| mockResponse('GET', `${API_BASE_PATH}/component_templates`, response, error); | ||
|
|
||
| const setLoadNodesPluginsResponse = (response?: HttpResponse, error?: ResponseError) => | ||
| mockResponse('GET', `${API_BASE_PATH}/nodes/plugins`, response, error); | ||
|
|
||
| const setLoadTelemetryResponse = (response?: HttpResponse, error?: ResponseError) => | ||
| mockResponse('GET', '/api/ui_counters/_report', response, error); | ||
|
|
||
| return { | ||
| setLoadTemplatesResponse, | ||
|
|
@@ -166,22 +133,16 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { | |
| setSimulateTemplateResponse, | ||
| setLoadComponentTemplatesResponse, | ||
| setLoadNodesPluginsResponse, | ||
| setLoadTelemetryResponse, | ||
| }; | ||
| }; | ||
|
|
||
| export const init = () => { | ||
| const server = sinon.fakeServer.create(); | ||
| server.respondImmediately = true; | ||
|
|
||
| // Define default response for unhandled requests. | ||
| // We make requests to APIs which don't impact the component under test, e.g. UI metric telemetry, | ||
| // and we can mock them all with a 200 instead of mocking each one individually. | ||
| server.respondWith([200, {}, 'DefaultSinonMockServerResponse']); | ||
|
|
||
| const httpRequestsMockHelpers = registerHttpRequestMockHelpers(server); | ||
| const httpSetup = httpServiceMock.createSetupContract(); | ||
| const httpRequestsMockHelpers = registerHttpRequestMockHelpers(httpSetup); | ||
|
|
||
| return { | ||
| server, | ||
| httpSetup, | ||
| httpRequestsMockHelpers, | ||
| }; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With sinon fake server we could use wildcards for url params, that is no longer allowed with HttpSetup mock. So we need to directly specify the full url when mocking api endpoints. For this particular case would look something like: