From 9bb0b78da6492b33a96640ea48110492f9f355b8 Mon Sep 17 00:00:00 2001 From: Josh Dover Date: Mon, 30 Dec 2019 12:57:36 -0600 Subject: [PATCH] Fix tests and types --- ...bana-plugin-public.ihttpfetcherror.body.md | 11 ++++++++++ .../kibana-plugin-public.ihttpfetcherror.md | 1 + ...a-plugin-public.savedobjectsclient.find.md | 2 +- ...kibana-plugin-public.savedobjectsclient.md | 2 +- src/core/public/http/types.ts | 1 + src/core/public/public.api.md | 2 ++ .../search/sync_search_strategy.test.ts | 6 ++---- .../public/search/sync_search_strategy.ts | 14 ++++++------- .../indexpattern_plugin/datapanel.test.tsx | 20 +++++++++++-------- .../public/indexpattern_plugin/loader.test.ts | 4 ++-- .../lens/public/indexpattern_plugin/loader.ts | 3 ++- .../lib/__tests__/replace_injected_vars.js | 1 + .../session_timeout_http_interceptor.test.ts | 8 ++++---- 13 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 docs/development/core/public/kibana-plugin-public.ihttpfetcherror.body.md diff --git a/docs/development/core/public/kibana-plugin-public.ihttpfetcherror.body.md b/docs/development/core/public/kibana-plugin-public.ihttpfetcherror.body.md new file mode 100644 index 000000000000000..2a5f3a68635b86f --- /dev/null +++ b/docs/development/core/public/kibana-plugin-public.ihttpfetcherror.body.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-public](./kibana-plugin-public.md) > [IHttpFetchError](./kibana-plugin-public.ihttpfetcherror.md) > [body](./kibana-plugin-public.ihttpfetcherror.body.md) + +## IHttpFetchError.body property + +Signature: + +```typescript +readonly body?: any; +``` diff --git a/docs/development/core/public/kibana-plugin-public.ihttpfetcherror.md b/docs/development/core/public/kibana-plugin-public.ihttpfetcherror.md index d8aa6970cca1ab8..0be3b5817920950 100644 --- a/docs/development/core/public/kibana-plugin-public.ihttpfetcherror.md +++ b/docs/development/core/public/kibana-plugin-public.ihttpfetcherror.md @@ -15,6 +15,7 @@ export interface IHttpFetchError extends Error | Property | Type | Description | | --- | --- | --- | +| [body](./kibana-plugin-public.ihttpfetcherror.body.md) | any | | | [req](./kibana-plugin-public.ihttpfetcherror.req.md) | Request | | | [request](./kibana-plugin-public.ihttpfetcherror.request.md) | Request | | | [res](./kibana-plugin-public.ihttpfetcherror.res.md) | Response | | diff --git a/docs/development/core/public/kibana-plugin-public.savedobjectsclient.find.md b/docs/development/core/public/kibana-plugin-public.savedobjectsclient.find.md index 1ce18834f531968..a4fa3f17d0d94f8 100644 --- a/docs/development/core/public/kibana-plugin-public.savedobjectsclient.find.md +++ b/docs/development/core/public/kibana-plugin-public.savedobjectsclient.find.md @@ -9,5 +9,5 @@ Search for objects Signature: ```typescript -find: (options: Pick) => Promise>; +find: (options: Pick) => Promise>; ``` diff --git a/docs/development/core/public/kibana-plugin-public.savedobjectsclient.md b/docs/development/core/public/kibana-plugin-public.savedobjectsclient.md index 6033c667c1866c7..3c4e33db4af91b5 100644 --- a/docs/development/core/public/kibana-plugin-public.savedobjectsclient.md +++ b/docs/development/core/public/kibana-plugin-public.savedobjectsclient.md @@ -20,7 +20,7 @@ export declare class SavedObjectsClient | [bulkGet](./kibana-plugin-public.savedobjectsclient.bulkget.md) | | (objects?: {
id: string;
type: string;
}[]) => Promise<SavedObjectsBatchResponse<SavedObjectAttributes>> | Returns an array of objects by id | | [create](./kibana-plugin-public.savedobjectsclient.create.md) | | <T extends SavedObjectAttributes>(type: string, attributes: T, options?: SavedObjectsCreateOptions) => Promise<SimpleSavedObject<T>> | Persists an object | | [delete](./kibana-plugin-public.savedobjectsclient.delete.md) | | (type: string, id: string) => Promise<{}> | Deletes an object | -| [find](./kibana-plugin-public.savedobjectsclient.find.md) | | <T extends SavedObjectAttributes>(options: Pick<SavedObjectFindOptionsServer, "search" | "filter" | "type" | "page" | "fields" | "searchFields" | "defaultSearchOperator" | "hasReference" | "sortField" | "perPage">) => Promise<SavedObjectsFindResponsePublic<T>> | Search for objects | +| [find](./kibana-plugin-public.savedobjectsclient.find.md) | | <T extends SavedObjectAttributes>(options: Pick<SavedObjectFindOptionsServer, "search" | "filter" | "type" | "page" | "perPage" | "sortField" | "fields" | "searchFields" | "hasReference" | "defaultSearchOperator">) => Promise<SavedObjectsFindResponsePublic<T>> | Search for objects | | [get](./kibana-plugin-public.savedobjectsclient.get.md) | | <T extends SavedObjectAttributes>(type: string, id: string) => Promise<SimpleSavedObject<T>> | Fetches a single object | ## Methods diff --git a/src/core/public/http/types.ts b/src/core/public/http/types.ts index 22af48cccf44ebb..4794d6f767c12ab 100644 --- a/src/core/public/http/types.ts +++ b/src/core/public/http/types.ts @@ -298,6 +298,7 @@ export interface IHttpFetchError extends Error { * @deprecated Provided for legacy compatibility. Prefer the `response` property instead. */ readonly res?: Response; + readonly body?: any; } /** @public */ diff --git a/src/core/public/public.api.md b/src/core/public/public.api.md index 40bb044b1bf4b5f..b78228721eb7391 100644 --- a/src/core/public/public.api.md +++ b/src/core/public/public.api.md @@ -620,6 +620,8 @@ export type IContextProvider, TContextName // @public (undocumented) export interface IHttpFetchError extends Error { + // (undocumented) + readonly body?: any; // @deprecated (undocumented) readonly req: Request; // (undocumented) diff --git a/src/plugins/data/public/search/sync_search_strategy.test.ts b/src/plugins/data/public/search/sync_search_strategy.test.ts index 2737a4033a015b9..cd19c4d84dce142 100644 --- a/src/plugins/data/public/search/sync_search_strategy.test.ts +++ b/src/plugins/data/public/search/sync_search_strategy.test.ts @@ -44,10 +44,8 @@ describe('Sync search strategy', () => { }, {} ); - expect(mockCoreSetup.http.fetch.mock.calls[0][0]).toBe( - `/internal/search/${SYNC_SEARCH_STRATEGY}` - ); - expect(mockCoreSetup.http.fetch.mock.calls[0][1]).toEqual({ + expect(mockCoreSetup.http.fetch.mock.calls[0][0]).toEqual({ + path: `/internal/search/${SYNC_SEARCH_STRATEGY}`, body: JSON.stringify({ serverStrategy: 'SYNC_SEARCH_STRATEGY', }), diff --git a/src/plugins/data/public/search/sync_search_strategy.ts b/src/plugins/data/public/search/sync_search_strategy.ts index 3885a97a98571a3..65fe10f39aaa04d 100644 --- a/src/plugins/data/public/search/sync_search_strategy.ts +++ b/src/plugins/data/public/search/sync_search_strategy.ts @@ -36,14 +36,12 @@ export const syncSearchStrategyProvider: TSearchStrategyProvider { - const response: Promise = context.core.http.fetch( - `/internal/search/${request.serverStrategy}`, - { - method: 'POST', - body: JSON.stringify(request), - signal: options.signal, - } - ); + const response: Promise = context.core.http.fetch({ + path: `/internal/search/${request.serverStrategy}`, + method: 'POST', + body: JSON.stringify(request), + signal: options.signal, + }); return from(response); }; diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/datapanel.test.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/datapanel.test.tsx index 52f00a7cd4e9df2..35693c06e8ebe8e 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/datapanel.test.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/datapanel.test.tsx @@ -278,8 +278,8 @@ describe('IndexPattern Data Panel', () => { function testProps() { const setState = jest.fn(); - core.http.get.mockImplementation(async (url: string) => { - const parts = url.split('/'); + core.http.get.mockImplementation(async ({ path }) => { + const parts = path.split('/'); const indexPatternTitle = parts[parts.length - 1]; return { indexPatternTitle, @@ -397,7 +397,8 @@ describe('IndexPattern Data Panel', () => { expect(setState).toHaveBeenCalledTimes(2); expect(core.http.get).toHaveBeenCalledTimes(2); - expect(core.http.get).toHaveBeenCalledWith('/api/lens/existing_fields/aaa', { + expect(core.http.get).toHaveBeenCalledWith({ + path: '/api/lens/existing_fields/aaa', query: { fromDate: '2019-01-01', toDate: '2020-01-01', @@ -405,7 +406,8 @@ describe('IndexPattern Data Panel', () => { }, }); - expect(core.http.get).toHaveBeenCalledWith('/api/lens/existing_fields/aaa', { + expect(core.http.get).toHaveBeenCalledWith({ + path: '/api/lens/existing_fields/aaa', query: { fromDate: '2019-01-01', toDate: '2020-01-02', @@ -436,7 +438,8 @@ describe('IndexPattern Data Panel', () => { expect(setState).toHaveBeenCalledTimes(2); - expect(core.http.get).toHaveBeenCalledWith('/api/lens/existing_fields/aaa', { + expect(core.http.get).toHaveBeenCalledWith({ + path: '/api/lens/existing_fields/aaa', query: { fromDate: '2019-01-01', toDate: '2020-01-01', @@ -444,7 +447,8 @@ describe('IndexPattern Data Panel', () => { }, }); - expect(core.http.get).toHaveBeenCalledWith('/api/lens/existing_fields/bbb', { + expect(core.http.get).toHaveBeenCalledWith({ + path: '/api/lens/existing_fields/bbb', query: { fromDate: '2019-01-01', toDate: '2020-01-01', @@ -484,13 +488,13 @@ describe('IndexPattern Data Panel', () => { let overlapCount = 0; const props = testProps(); - core.http.get.mockImplementation((url: string) => { + core.http.get.mockImplementation(({ path }) => { if (queryCount) { ++overlapCount; } ++queryCount; - const parts = url.split('/'); + const parts = path.split('/'); const indexPatternTitle = parts[parts.length - 1]; const result = Promise.resolve({ indexPatternTitle, diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.test.ts b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.test.ts index 2fb678aed5a54b0..f4cea379dd4ce01 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.test.ts +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.test.ts @@ -516,8 +516,8 @@ describe('loader', () => { describe('syncExistingFields', () => { it('should call once for each index pattern', async () => { const setState = jest.fn(); - const fetchJson = jest.fn(async (url: string) => { - const indexPatternTitle = _.last(url.split('/')); + const fetchJson = jest.fn(({ path }: { path: string }) => { + const indexPatternTitle = _.last(path.split('/')); return { indexPatternTitle, existingFieldNames: ['field_1', 'field_2'].map( diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.ts b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.ts index 661c627f3454f69..e3e0fd2067ba693 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.ts +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.ts @@ -243,7 +243,8 @@ export async function syncExistingFields({ query.timeFieldName = pattern.timeFieldName; } - return fetchJson(`${BASE_API_URL}/existing_fields/${pattern.title}`, { + return fetchJson({ + path: `${BASE_API_URL}/existing_fields/${pattern.title}`, query, }) as Promise; }) diff --git a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/replace_injected_vars.js b/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/replace_injected_vars.js index 55da11221bb1015..ae929045cf57093 100644 --- a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/replace_injected_vars.js +++ b/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/replace_injected_vars.js @@ -16,6 +16,7 @@ const buildRequest = (path = '/app/kibana') => { return { path, route: { settings: {} }, + headers: {}, raw: { req: { socket: {}, diff --git a/x-pack/plugins/security/public/session/session_timeout_http_interceptor.test.ts b/x-pack/plugins/security/public/session/session_timeout_http_interceptor.test.ts index ffbd625590b1584..843ca9c8ed835bd 100644 --- a/x-pack/plugins/security/public/session/session_timeout_http_interceptor.test.ts +++ b/x-pack/plugins/security/public/session/session_timeout_http_interceptor.test.ts @@ -58,7 +58,7 @@ describe('response', () => { http.intercept(interceptor); fetchMock.mock('*', 200); - await http.fetch('/foo-api', { headers: { 'kbn-system-api': 'true' } }); + await http.fetch('/foo-api', { asSystemApi: true }); expect(sessionTimeoutMock.extend).not.toHaveBeenCalled(); }); @@ -99,9 +99,9 @@ describe('responseError', () => { http.intercept(interceptor); fetchMock.mock('*', 401); - await expect( - http.fetch('/foo-api', { headers: { 'kbn-system-api': 'true' } }) - ).rejects.toMatchInlineSnapshot(`[Error: Unauthorized]`); + await expect(http.fetch('/foo-api', { asSystemApi: true })).rejects.toMatchInlineSnapshot( + `[Error: Unauthorized]` + ); expect(sessionTimeoutMock.extend).not.toHaveBeenCalled(); });