From 02df98e461aa4558250c7f1001c025b3cdbf0535 Mon Sep 17 00:00:00 2001 From: Wojciech Duda <69160975+wojd0@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:57:12 +0100 Subject: [PATCH] AAE-30882 Code formatting and unit tests --- lib/js-api/src/superagentHttpClient.ts | 2 +- lib/js-api/test/superagentHttpClient.spec.ts | 68 ++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/lib/js-api/src/superagentHttpClient.ts b/lib/js-api/src/superagentHttpClient.ts index f1670e1627..fdbf4e8e80 100644 --- a/lib/js-api/src/superagentHttpClient.ts +++ b/lib/js-api/src/superagentHttpClient.ts @@ -283,7 +283,7 @@ export class SuperagentHttpClient implements HttpClient { const newParams: { [key: string]: any } = {}; for (const key in params) { - if (Object.prototype.hasOwnProperty.call(params, key) && params[key] !== undefined && params[key] !== null) { + if (Object.prototype.hasOwnProperty.call(params, key) && params[key] !== null) { const value = params[key]; if (SuperagentHttpClient.isFileParam(value) || Array.isArray(value)) { newParams[key] = value; diff --git a/lib/js-api/test/superagentHttpClient.spec.ts b/lib/js-api/test/superagentHttpClient.spec.ts index a9dac14ebe..45c7154527 100644 --- a/lib/js-api/test/superagentHttpClient.spec.ts +++ b/lib/js-api/test/superagentHttpClient.spec.ts @@ -55,7 +55,9 @@ describe('SuperagentHttpClient', () => { json: () => Promise.resolve({ data: 'test' }) } as unknown as FetchResponse; (ofetch as unknown as jest.Mock).mockResolvedValue(fakeResponse); + const result = await client.request(url, options, securityOptions, emitters); + expect(result).toEqual({ data: 'test' }); expect(emitters.eventEmitter.emit).toHaveBeenCalledWith('success', { data: 'test' }); }); @@ -68,6 +70,7 @@ describe('SuperagentHttpClient', () => { text: () => Promise.resolve('Bad Request') } as unknown as FetchResponse; (ofetch as unknown as jest.Mock).mockResolvedValue(fakeResponse); + await expect(client.request(url, options, securityOptions, emitters)).rejects.toMatchObject({ status: 400 }); expect(emitters.apiClientEmitter.emit).toHaveBeenCalledWith('error', fakeResponse); expect(emitters.eventEmitter.emit).toHaveBeenCalledWith('error', fakeResponse); @@ -81,10 +84,75 @@ describe('SuperagentHttpClient', () => { text: () => Promise.resolve('Unauthorized') } as unknown as FetchResponse; (ofetch as unknown as jest.Mock).mockResolvedValue(fakeResponse); + await expect(client.request(url, options, securityOptions, emitters)).rejects.toMatchObject({ status: 401 }); + expect(emitters.apiClientEmitter.emit).toHaveBeenCalledWith('unauthorized'); expect(emitters.eventEmitter.emit).toHaveBeenCalledWith('unauthorized'); }); + + it('should reject with network error when fetch fails', async () => { + const networkError = new Error('Network failure'); + (ofetch as unknown as jest.Mock).mockRejectedValue(networkError); + + await expect(client.request(url, options, defaultSecurityOptions, emitters)).rejects.toEqual(networkError); + expect(emitters.apiClientEmitter.emit).toHaveBeenCalledWith('error', networkError); + expect(emitters.eventEmitter.emit).toHaveBeenCalledWith('error', networkError); + }); + + it('should parse text/html response correctly', async () => { + const htmlResponse = { + ok: true, + headers: new Map([['content-type', 'text/html']]), + json: () => Promise.reject(new Error('Not JSON')), + text: () => Promise.resolve('HTML Content') + } as unknown as FetchResponse; + (ofetch as unknown as jest.Mock).mockResolvedValue(htmlResponse); + + const result = await client.request('http://fake-api/test', defaultRequestOptions, defaultSecurityOptions, emitters); + + expect(result).toEqual('HTML Content'); + expect(emitters.eventEmitter.emit).toHaveBeenCalledWith('success', 'HTML Content'); + }); + + it('should include custom headers from headerParams', async () => { + const customOptions: RequestOptions = { + ...defaultRequestOptions, + headerParams: { 'X-Custom-Header': 'customValue' } + }; + // Return a successful JSON response + const fakeResponse = { + ok: true, + headers: new Map([['content-type', 'application/json']]), + json: () => Promise.resolve({ data: 'with-custom-header' }) + } as unknown as FetchResponse; + (ofetch as unknown as jest.Mock).mockResolvedValue(fakeResponse); + + await client.request(url, customOptions, securityOptions, emitters); + + const fetchCallArgs = (ofetch as unknown as jest.Mock).mock.calls[0]; + const fetchOptions = fetchCallArgs[1]; + expect(fetchOptions.headers['x-custom-header']).toEqual('customValue'); + }); + + it('should include headers from securityOptions.defaultHeaders', async () => { + const securityOptionsWithDefault = { + ...defaultSecurityOptions, + defaultHeaders: { 'X-Default-Header': 'defaultValue' } + }; + const fakeResponse = { + ok: true, + headers: new Map([['content-type', 'application/json']]), + json: () => Promise.resolve({ data: 'with-default-header' }) + } as unknown as FetchResponse; + (ofetch as unknown as jest.Mock).mockResolvedValue(fakeResponse); + + await client.request(url, defaultRequestOptions, securityOptionsWithDefault, emitters); + + const fetchCallArgs = (ofetch as unknown as jest.Mock).mock.calls[0]; + const fetchOptions = fetchCallArgs[1]; + expect(fetchOptions.headers['x-default-header']).toEqual('defaultValue'); + }); }); describe('buildRequest', () => {