-
Notifications
You must be signed in to change notification settings - Fork 702
OCPBUGS-81512: Use ETag conditional requests for OpenAPI v2 fetching #16457
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
Merged
+180
−10
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,164 @@ | ||
| import type { SwaggerAPISpec, SwaggerDefinitions } from '../swagger'; | ||
|
|
||
| const mockCoFetch = jest.fn(); | ||
| jest.mock('@console/shared/src/utils/console-fetch', () => ({ | ||
| coFetch: mockCoFetch, | ||
| })); | ||
|
|
||
| const mockDefinitions: SwaggerDefinitions = { | ||
| 'io.k8s.api.core.v1.Pod': { | ||
| description: 'Pod is a collection of containers.', | ||
| type: 'object', | ||
| properties: { | ||
| metadata: { $ref: '#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta' }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const createMockResponse = (definitions: SwaggerDefinitions, etag?: string): Response => | ||
| (({ | ||
| status: 200, | ||
| ok: true, | ||
| headers: new Headers(etag ? { ETag: etag } : {}), | ||
| json: jest.fn().mockResolvedValue({ | ||
| swagger: '2.0', | ||
| info: { title: 'Kubernetes', version: 'v1' }, | ||
| paths: {}, | ||
| definitions, | ||
| } as SwaggerAPISpec), | ||
| } as unknown) as Response); | ||
|
|
||
| const create304Response = (): Response => | ||
| (({ | ||
| status: 304, | ||
| ok: true, | ||
| headers: new Headers(), | ||
| json: jest.fn(), | ||
| } as unknown) as Response); | ||
|
|
||
| describe('fetchSwagger', () => { | ||
| let fetchSwagger: () => Promise<SwaggerDefinitions>; | ||
| let getSwaggerDefinitions: () => SwaggerDefinitions; | ||
|
|
||
| beforeEach(() => { | ||
| jest.isolateModules(() => { | ||
| // Fresh module to reset cachedETag and swaggerDefinitions | ||
| const swagger = require('../swagger'); | ||
| fetchSwagger = swagger.fetchSwagger; | ||
| getSwaggerDefinitions = swagger.getSwaggerDefinitions; | ||
| }); | ||
| mockCoFetch.mockReset(); | ||
| jest.spyOn(window, 'dispatchEvent').mockImplementation(() => true); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('should fetch and return swagger definitions on first call', async () => { | ||
| mockCoFetch.mockResolvedValue(createMockResponse(mockDefinitions, '"abc123"')); | ||
|
|
||
| const result = await fetchSwagger(); | ||
|
|
||
| expect(result).toEqual(mockDefinitions); | ||
| expect(mockCoFetch).toHaveBeenCalledWith('api/kubernetes/openapi/v2', { | ||
| headers: { Accept: 'application/json' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should send If-None-Match header on subsequent requests', async () => { | ||
| mockCoFetch.mockResolvedValue(createMockResponse(mockDefinitions, '"abc123"')); | ||
| await fetchSwagger(); | ||
|
|
||
| mockCoFetch.mockResolvedValue(create304Response()); | ||
| await fetchSwagger(); | ||
|
|
||
| expect(mockCoFetch).toHaveBeenCalledTimes(2); | ||
| expect(mockCoFetch.mock.calls[1]).toEqual([ | ||
| 'api/kubernetes/openapi/v2', | ||
| { headers: { Accept: 'application/json', 'If-None-Match': '"abc123"' } }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should return cached definitions on 304', async () => { | ||
| mockCoFetch.mockResolvedValue(createMockResponse(mockDefinitions, '"abc123"')); | ||
| await fetchSwagger(); | ||
|
|
||
| const notModifiedResponse = create304Response(); | ||
| mockCoFetch.mockResolvedValue(notModifiedResponse); | ||
| const result = await fetchSwagger(); | ||
|
|
||
| expect(result).toEqual(mockDefinitions); | ||
| expect(notModifiedResponse.json).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should update definitions when server returns new data', async () => { | ||
| mockCoFetch.mockResolvedValue(createMockResponse(mockDefinitions, '"abc123"')); | ||
| await fetchSwagger(); | ||
|
|
||
| const updatedDefinitions: SwaggerDefinitions = { | ||
| ...mockDefinitions, | ||
| 'io.k8s.api.apps.v1.Deployment': { description: 'A Deployment.', type: 'object' }, | ||
| }; | ||
| mockCoFetch.mockResolvedValue(createMockResponse(updatedDefinitions, '"def456"')); | ||
| const result = await fetchSwagger(); | ||
|
|
||
| expect(result).toEqual(updatedDefinitions); | ||
| expect(getSwaggerDefinitions()).toEqual(updatedDefinitions); | ||
| }); | ||
|
|
||
| it('should dispatch console_swagger_refresh on successful fetch', async () => { | ||
| mockCoFetch.mockResolvedValue(createMockResponse(mockDefinitions, '"abc123"')); | ||
| await fetchSwagger(); | ||
|
|
||
| expect(window.dispatchEvent).toHaveBeenCalledWith(new Event('console_swagger_refresh')); | ||
| }); | ||
|
|
||
| it('should not dispatch console_swagger_refresh on 304', async () => { | ||
| mockCoFetch.mockResolvedValue(createMockResponse(mockDefinitions, '"abc123"')); | ||
| await fetchSwagger(); | ||
| (window.dispatchEvent as jest.Mock).mockClear(); | ||
|
|
||
| mockCoFetch.mockResolvedValue(create304Response()); | ||
| await fetchSwagger(); | ||
|
|
||
| expect(window.dispatchEvent).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return null when definitions are missing from response', async () => { | ||
| const response = ({ | ||
| status: 200, | ||
| ok: true, | ||
| headers: new Headers(), | ||
| json: jest.fn().mockResolvedValue({ swagger: '2.0', paths: {} }), | ||
| } as unknown) as Response; | ||
| mockCoFetch.mockResolvedValue(response); | ||
|
|
||
| const result = await fetchSwagger(); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return null on fetch error', async () => { | ||
| mockCoFetch.mockRejectedValue(new Error('Network error')); | ||
|
|
||
| const result = await fetchSwagger(); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should work without ETag header from server', async () => { | ||
| mockCoFetch.mockResolvedValue(createMockResponse(mockDefinitions)); | ||
| const result = await fetchSwagger(); | ||
|
|
||
| expect(result).toEqual(mockDefinitions); | ||
|
|
||
| mockCoFetch.mockResolvedValue(createMockResponse(mockDefinitions)); | ||
| await fetchSwagger(); | ||
|
|
||
| expect(mockCoFetch.mock.calls[1]).toEqual([ | ||
| 'api/kubernetes/openapi/v2', | ||
| { headers: { Accept: 'application/json' } }, | ||
| ]); | ||
| }); | ||
| }); | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.