Skip to content

Back merge #85

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
merged 7 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
64 changes: 64 additions & 0 deletions test/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
30 changes: 30 additions & 0 deletions test/retryPolicy/delivery-sdk-handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Loading