From 23b62ec15e2d98a1adc89d4c834c257bb69109e6 Mon Sep 17 00:00:00 2001 From: raj pandey Date: Thu, 16 Jan 2025 17:25:39 +0530 Subject: [PATCH 1/2] fix license --- LICENSE.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE.txt b/LICENSE.txt index eebdf5a..17d428a 100755 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,7 +1,7 @@ The MIT License (MIT) -Copyright (c) 2016-2024 Contentstack +Copyright (c) 2016-2025 Contentstack Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 04448c08828f8a9fe556bbbc584cf6328c1f5495 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Fri, 17 Jan 2025 13:36:50 +0530 Subject: [PATCH 2/2] improved code coverage by adding test case --- test/request.spec.ts | 64 +++++++++++++++++++ .../retryPolicy/delivery-sdk-handlers.spec.ts | 30 +++++++++ 2 files changed, 94 insertions(+) diff --git a/test/request.spec.ts b/test/request.spec.ts index 66a3f28..d0836d0 100644 --- a/test/request.spec.ts +++ b/test/request.spec.ts @@ -37,4 +37,68 @@ describe('Request tests', () => { }; await expect(getData(client, url, {})).rejects.toThrowError('Host is required for live preview'); }); + + it('should handle live_preview with enable=true and live_preview=init', async () => { + const client = httpClient({}); + const mock = new MockAdapter(client as any); + const url = '/your-api-endpoint'; + const mockResponse = { data: 'mocked' }; + + client.stackConfig = { + live_preview: { + enable: true, + preview_token: 'someToken', + live_preview: 'init', + }, + }; + + mock.onGet(url).reply(200, mockResponse); + + const result = await getData(client, url, {}); + expect(result).toEqual(mockResponse); + }); + + it('should set baseURL correctly when host is provided without https://', async () => { + const client = httpClient({}); + const mock = new MockAdapter(client as any); + const url = '/your-api-endpoint'; + const mockResponse = { data: 'mocked' }; + + client.stackConfig = { + live_preview: { + enable: true, + preview_token: 'someToken', + live_preview: 'someHash', + host: 'example.com', + }, + }; + + mock.onGet(url).reply(200, mockResponse); + + const result = await getData(client, url, {}); + expect(client.defaults.baseURL).toBe('https://example.com'); + expect(result).toEqual(mockResponse); + }); + + it('should not modify baseURL when host is already prefixed with https://', async () => { + const client = httpClient({}); + const mock = new MockAdapter(client as any); + const url = '/your-api-endpoint'; + const mockResponse = { data: 'mocked' }; + + client.stackConfig = { + live_preview: { + enable: true, + preview_token: 'someToken', + live_preview: 'someHash', + host: 'https://example.com', + }, + }; + + mock.onGet(url).reply(200, mockResponse); + + const result = await getData(client, url, {}); + expect(client.stackConfig.live_preview.host).toBe('https://example.com'); + expect(result).toEqual(mockResponse); + }); }); diff --git a/test/retryPolicy/delivery-sdk-handlers.spec.ts b/test/retryPolicy/delivery-sdk-handlers.spec.ts index 7aecc5c..ce146a7 100644 --- a/test/retryPolicy/delivery-sdk-handlers.spec.ts +++ b/test/retryPolicy/delivery-sdk-handlers.spec.ts @@ -183,4 +183,34 @@ describe('retryResponseErrorHandler', () => { await expect(retryResponseErrorHandler(error, config, client)).rejects.toBe(error); }); + + it('should retry when response status is 429 and retryCount is less than retryLimit', async () => { + const error = { + config: { retryOnError: true, retryCount: 1 }, + response: { status: 429, statusText: 'Rate limit exceeded' }, + }; + const config = { retryLimit: 3 }; + const client = axios.create(); + + mock.onAny().reply(200); + + const response: any = await retryResponseErrorHandler(error, config, client); + expect(response.status).toBe(200); + }); + + it('should retry when retryCondition is true', async () => { + const error = { + config: { retryOnError: true, retryCount: 1 }, + response: { status: 500, statusText: 'Internal Server Error' }, + }; + const retryCondition = jest.fn().mockReturnValue(true); + const config = { retryLimit: 3, retryCondition, retryDelay: 100 }; + const client = axios.create(); + + mock.onAny().reply(200); + + const response: any = await retryResponseErrorHandler(error, config, client); + expect(response.status).toBe(200); + expect(retryCondition).toHaveBeenCalledWith(error); + }); });