|
| 1 | +// Import node-fetch module to spy on it |
| 2 | +import * as nodeFetch from 'node-fetch'; |
| 3 | +import { ApiClient } from './ApiClient'; |
| 4 | +import { HttpMethod } from './models'; |
| 5 | + |
| 6 | +/* eslint-disable max-lines-per-function */ |
| 7 | +import { StatusCodes } from 'http-status-codes'; |
| 8 | +// Import after mock |
| 9 | +import { Response } from 'node-fetch'; |
| 10 | + |
| 11 | +jest.mock('./hyperproof-api/Logger'); |
| 12 | + |
| 13 | +// Mock only the default export (fetch function) |
| 14 | +const mockFetch = jest.spyOn(nodeFetch, 'default') as jest.MockedFunction< |
| 15 | + typeof nodeFetch.default |
| 16 | +>; |
| 17 | + |
| 18 | +class TestApiClient extends ApiClient { |
| 19 | + // Expose protected methods for testing |
| 20 | + public async testParseResponseBodyJson(response: Response, url: string) { |
| 21 | + return this.parseResponseBodyJson(response, url); |
| 22 | + } |
| 23 | + |
| 24 | + public async testGetStatusCodeFromErrorMessage(error?: any) { |
| 25 | + return this.getStatusCodeFromErrorMessage(error); |
| 26 | + } |
| 27 | + |
| 28 | + public async testHandleNetworkError(err: any) { |
| 29 | + return this.handleNetworkError(err); |
| 30 | + } |
| 31 | + |
| 32 | + public async testHandleFailedResponse(response: Response, apiUrl: string) { |
| 33 | + return this.handleFailedResponse(response, apiUrl); |
| 34 | + } |
| 35 | + |
| 36 | + public async testBuildApiUrlAndFetch(params: any) { |
| 37 | + return (this as any).buildApiUrlAndFetch(params); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +describe('ApiClient', () => { |
| 42 | + let client: TestApiClient; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + client = new TestApiClient({}); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('parseResponseBodyJson', () => { |
| 49 | + it('should return JSON when response has valid JSON content', async () => { |
| 50 | + const response = new Response(JSON.stringify({ key: 'value' }), { |
| 51 | + status: StatusCodes.OK |
| 52 | + }); |
| 53 | + |
| 54 | + const result = await client.testParseResponseBodyJson( |
| 55 | + response, |
| 56 | + 'http://example.com' |
| 57 | + ); |
| 58 | + expect(result).toEqual({ key: 'value' }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should return undefined when response is NO_CONTENT', async () => { |
| 62 | + const response = new Response(undefined, { |
| 63 | + status: StatusCodes.NO_CONTENT |
| 64 | + }); |
| 65 | + |
| 66 | + const result = await client.testParseResponseBodyJson( |
| 67 | + response, |
| 68 | + 'http://example.com' |
| 69 | + ); |
| 70 | + expect(result).toBeUndefined(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return undefined when response body is empty', async () => { |
| 74 | + const response = new Response('', { |
| 75 | + status: StatusCodes.OK |
| 76 | + }); |
| 77 | + |
| 78 | + const result = await client.testParseResponseBodyJson( |
| 79 | + response, |
| 80 | + 'http://example.com' |
| 81 | + ); |
| 82 | + expect(result).toBeUndefined(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should throw error when response content is not valid JSON', async () => { |
| 86 | + const response = new Response('Not JSON content', { |
| 87 | + status: StatusCodes.OK |
| 88 | + }); |
| 89 | + |
| 90 | + await expect( |
| 91 | + client.testParseResponseBodyJson(response, 'http://example.com') |
| 92 | + ).rejects.toMatchObject({ |
| 93 | + status: StatusCodes.INTERNAL_SERVER_ERROR, |
| 94 | + message: 'Failed to convert response body to JSON' |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('getStatusCodeFromErrorMessage', () => { |
| 100 | + it('should return INTERNAL_SERVER_ERROR when error is undefined', async () => { |
| 101 | + const result = await client.testGetStatusCodeFromErrorMessage(undefined); |
| 102 | + expect(result).toBe(StatusCodes.INTERNAL_SERVER_ERROR); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should return INTERNAL_SERVER_ERROR when error has no code or message', async () => { |
| 106 | + const result = await client.testGetStatusCodeFromErrorMessage({}); |
| 107 | + expect(result).toBe(StatusCodes.INTERNAL_SERVER_ERROR); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should map ENOTFOUND error to BAD_GATEWAY', async () => { |
| 111 | + const error = { |
| 112 | + code: 'ENOTFOUND', |
| 113 | + message: 'request to https://example.com failed, getaddrinfo ENOTFOUND' |
| 114 | + }; |
| 115 | + |
| 116 | + const result = await client.testGetStatusCodeFromErrorMessage(error); |
| 117 | + expect(result).toBe(StatusCodes.BAD_GATEWAY); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should return INTERNAL_SERVER_ERROR when no pattern matches', async () => { |
| 121 | + const error = { |
| 122 | + code: 'UNKNOWN_ERROR', |
| 123 | + message: 'Some unknown error occurred' |
| 124 | + }; |
| 125 | + |
| 126 | + const result = await client.testGetStatusCodeFromErrorMessage(error); |
| 127 | + expect(result).toBe(StatusCodes.INTERNAL_SERVER_ERROR); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('handleNetworkError', () => { |
| 132 | + it('should preserve status code when error has valid status', async () => { |
| 133 | + const error = { |
| 134 | + status: StatusCodes.SERVICE_UNAVAILABLE, |
| 135 | + message: 'Service unavailable' |
| 136 | + }; |
| 137 | + |
| 138 | + const result = await client.testHandleNetworkError(error); |
| 139 | + expect(result.status).toBe(StatusCodes.SERVICE_UNAVAILABLE); |
| 140 | + expect(result.message).toBe('Service unavailable'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should map ENOTFOUND to BAD_GATEWAY', async () => { |
| 144 | + const error = { |
| 145 | + code: 'ENOTFOUND', |
| 146 | + message: |
| 147 | + 'request to https://api.example.com failed, getaddrinfo ENOTFOUND' |
| 148 | + }; |
| 149 | + |
| 150 | + const result = await client.testHandleNetworkError(error); |
| 151 | + expect(result.status).toBe(StatusCodes.BAD_GATEWAY); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + describe('handleFailedResponse', () => { |
| 156 | + it('should throw HttpError with response details', async () => { |
| 157 | + const response = new Response('Bad request error', { |
| 158 | + status: StatusCodes.BAD_REQUEST |
| 159 | + }); |
| 160 | + |
| 161 | + await expect( |
| 162 | + client.testHandleFailedResponse(response, 'http://example.com/api') |
| 163 | + ).rejects.toMatchObject({ |
| 164 | + status: StatusCodes.BAD_REQUEST |
| 165 | + }); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + describe('setBaseUrl', () => { |
| 170 | + it('should update base URL', () => { |
| 171 | + client.setBaseUrl('https://api.newdomain.com'); |
| 172 | + // No error should be thrown |
| 173 | + expect(client).toBeDefined(); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + describe('buildApiUrlAndFetch', () => { |
| 178 | + beforeEach(() => { |
| 179 | + jest.clearAllMocks(); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should call handleNetworkError when fetch throws an error', async () => { |
| 183 | + const networkError = new Error('Network failure'); |
| 184 | + mockFetch.mockRejectedValue(networkError); |
| 185 | + |
| 186 | + const handleNetworkErrorSpy = jest.spyOn( |
| 187 | + client as any, |
| 188 | + 'handleNetworkError' |
| 189 | + ); |
| 190 | + |
| 191 | + await expect( |
| 192 | + client.testBuildApiUrlAndFetch({ |
| 193 | + url: 'http://example.com/api', |
| 194 | + method: HttpMethod.GET |
| 195 | + }) |
| 196 | + ).rejects.toMatchObject({ |
| 197 | + status: StatusCodes.INTERNAL_SERVER_ERROR |
| 198 | + }); |
| 199 | + |
| 200 | + expect(handleNetworkErrorSpy).toHaveBeenCalledWith(networkError); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should call handleFailedResponse when response is not ok', async () => { |
| 204 | + const failedResponse = new Response('Bad request', { |
| 205 | + status: StatusCodes.BAD_REQUEST |
| 206 | + }); |
| 207 | + mockFetch.mockResolvedValue(failedResponse); |
| 208 | + |
| 209 | + const handleFailedResponseSpy = jest.spyOn( |
| 210 | + client as any, |
| 211 | + 'handleFailedResponse' |
| 212 | + ); |
| 213 | + |
| 214 | + await expect( |
| 215 | + client.testBuildApiUrlAndFetch({ |
| 216 | + url: 'http://example.com/api', |
| 217 | + method: HttpMethod.GET |
| 218 | + }) |
| 219 | + ).rejects.toMatchObject({ |
| 220 | + status: StatusCodes.BAD_REQUEST |
| 221 | + }); |
| 222 | + |
| 223 | + expect(handleFailedResponseSpy).toHaveBeenCalledWith( |
| 224 | + failedResponse, |
| 225 | + 'http://example.com/api' |
| 226 | + ); |
| 227 | + }); |
| 228 | + }); |
| 229 | +}); |
0 commit comments