|
| 1 | +import { parseJsonResponse } from '../src/lib/helper/response-helper'; |
| 2 | +import { logger } from '@openops/server-shared'; |
| 3 | +import { InfrastructureError } from '../src/lib/helper/execution-errors'; |
| 4 | + |
| 5 | +jest.mock('@openops/server-shared', () => ({ |
| 6 | + logger: { |
| 7 | + warn: jest.fn(), |
| 8 | + }, |
| 9 | +})); |
| 10 | + |
| 11 | +describe('parseJsonResponse', () => { |
| 12 | + beforeEach(() => { |
| 13 | + jest.clearAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should return parsed JSON for a valid JSON response', async () => { |
| 17 | + const payload = { id: 1, name: 'test' }; |
| 18 | + const response = { |
| 19 | + status: 200, |
| 20 | + headers: new Headers({ 'content-type': 'application/json' }), |
| 21 | + text: jest.fn().mockResolvedValue(JSON.stringify(payload)), |
| 22 | + } as unknown as Response; |
| 23 | + |
| 24 | + const result = await parseJsonResponse(response); |
| 25 | + expect(result).toEqual(payload); |
| 26 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should attempt to parse JSON when content-type header is missing', async () => { |
| 30 | + const payload = { id: 2 }; |
| 31 | + const response = { |
| 32 | + status: 200, |
| 33 | + headers: new Headers(), |
| 34 | + text: jest.fn().mockResolvedValue(JSON.stringify(payload)), |
| 35 | + } as unknown as Response; |
| 36 | + |
| 37 | + const result = await parseJsonResponse(response); |
| 38 | + expect(result).toEqual(payload); |
| 39 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should include full status code and full body in InfrastructureError for non-json content-type', async () => { |
| 43 | + const body = '<html><body><h1>502 Bad Gateway</h1><p>Nginx Error</p></body></html>'; |
| 44 | + const response = { |
| 45 | + status: 502, |
| 46 | + headers: new Headers({ 'content-type': 'text/html' }), |
| 47 | + text: jest.fn().mockResolvedValue(body), |
| 48 | + } as unknown as Response; |
| 49 | + |
| 50 | + const promise = parseJsonResponse(response); |
| 51 | + await expect(promise).rejects.toThrow(InfrastructureError); |
| 52 | + await expect(promise).rejects.toThrow( |
| 53 | + 'Expected JSON response, but received status 502 and text/html.', |
| 54 | + ); |
| 55 | + expect(logger.warn).toHaveBeenCalledWith( |
| 56 | + 'Expected JSON response but received non-JSON content type', |
| 57 | + { status: 502, contentType: 'text/html', body }, |
| 58 | + |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should include status code in InfrastructureError for invalid json', async () => { |
| 63 | + const body = 'invalid json'; |
| 64 | + const response = { |
| 65 | + status: 200, |
| 66 | + headers: new Headers({ 'content-type': 'application/json' }), |
| 67 | + text: jest.fn().mockResolvedValue(body), |
| 68 | + } as unknown as Response; |
| 69 | + |
| 70 | + const promise = parseJsonResponse(response); |
| 71 | + await expect(promise).rejects.toThrow(InfrastructureError); |
| 72 | + await expect(promise).rejects.toThrow( |
| 73 | + 'Failed to parse JSON response with status 200.', |
| 74 | + ); |
| 75 | + expect(logger.warn).toHaveBeenCalledWith( |
| 76 | + 'Failed to parse JSON response', |
| 77 | + { status: 200, body }, |
| 78 | + ); |
| 79 | + }); |
| 80 | +}); |
0 commit comments