|
| 1 | +import * as Contentstack from '../../src/stack'; |
| 2 | +import { StackConfig } from '../../src/common/types'; |
| 3 | +import MockAdapter from 'axios-mock-adapter'; |
| 4 | + |
| 5 | +describe('Default network-error retry behavior', () => { |
| 6 | + let mockClient: MockAdapter | undefined; |
| 7 | + |
| 8 | + afterEach(() => { |
| 9 | + mockClient?.restore(); |
| 10 | + mockClient = undefined; |
| 11 | + }); |
| 12 | + |
| 13 | + const dnsError = (code: string) => (config: any) => |
| 14 | + Promise.reject( |
| 15 | + Object.assign(new Error(`getaddrinfo ${code} example.com`), { |
| 16 | + code, |
| 17 | + config, |
| 18 | + isAxiosError: true, |
| 19 | + }) |
| 20 | + ); |
| 21 | + |
| 22 | + it('(a) retries and succeeds after a single transient ENOTFOUND failure with no custom retryCondition', async () => { |
| 23 | + const config: StackConfig = { |
| 24 | + apiKey: 'test-api-key', |
| 25 | + deliveryToken: 'test-delivery-token', |
| 26 | + environment: 'test-environment', |
| 27 | + retryDelay: 10, |
| 28 | + }; |
| 29 | + const stack = Contentstack.stack(config); |
| 30 | + const client = stack.getClient(); |
| 31 | + mockClient = new MockAdapter(client); |
| 32 | + |
| 33 | + mockClient |
| 34 | + .onGet('/content_types/test') |
| 35 | + .replyOnce(dnsError('ENOTFOUND')) |
| 36 | + .onGet('/content_types/test') |
| 37 | + .reply(200, { content_types: [] }); |
| 38 | + |
| 39 | + const res = await client.get('/content_types/test'); |
| 40 | + expect(res.status).toBe(200); |
| 41 | + }); |
| 42 | + |
| 43 | + it('(b) still fails after retryLimit is exhausted on a permanent network failure', async () => { |
| 44 | + const config: StackConfig = { |
| 45 | + apiKey: 'test-api-key', |
| 46 | + deliveryToken: 'test-delivery-token', |
| 47 | + environment: 'test-environment', |
| 48 | + retryLimit: 2, |
| 49 | + retryDelay: 10, |
| 50 | + }; |
| 51 | + const stack = Contentstack.stack(config); |
| 52 | + const client = stack.getClient(); |
| 53 | + mockClient = new MockAdapter(client); |
| 54 | + |
| 55 | + mockClient.onGet('/content_types/test').reply(dnsError('ENOTFOUND')); |
| 56 | + |
| 57 | + await expect(client.get('/content_types/test')).rejects.toBeDefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('(c) composes with a user-supplied retryCondition without replacing it', async () => { |
| 61 | + const userCondition = jest.fn((error: any) => error?.response?.status === 500); |
| 62 | + const config: StackConfig = { |
| 63 | + apiKey: 'test-api-key', |
| 64 | + deliveryToken: 'test-delivery-token', |
| 65 | + environment: 'test-environment', |
| 66 | + retryDelay: 10, |
| 67 | + retryCondition: userCondition, |
| 68 | + }; |
| 69 | + const stack = Contentstack.stack(config); |
| 70 | + const client = stack.getClient(); |
| 71 | + mockClient = new MockAdapter(client); |
| 72 | + |
| 73 | + mockClient |
| 74 | + .onGet('/content_types/test') |
| 75 | + .replyOnce(dnsError('ECONNRESET')) |
| 76 | + .onGet('/content_types/test') |
| 77 | + .reply(200, { content_types: [] }); |
| 78 | + |
| 79 | + const res = await client.get('/content_types/test'); |
| 80 | + expect(res.status).toBe(200); |
| 81 | + // config is never mutated — stack.config.retryCondition stays the exact |
| 82 | + // user-supplied function, matching the identity assertion already made |
| 83 | + // by test/unit/retry-configuration.spec.ts. |
| 84 | + expect(stack.config.retryCondition).toBe(userCondition); |
| 85 | + }); |
| 86 | + |
| 87 | + it('(d) ECONNABORTED/timeout errors are unaffected by the new network-retry path', async () => { |
| 88 | + const config: StackConfig = { |
| 89 | + apiKey: 'test-api-key', |
| 90 | + deliveryToken: 'test-delivery-token', |
| 91 | + environment: 'test-environment', |
| 92 | + retryDelay: 10, |
| 93 | + }; |
| 94 | + const stack = Contentstack.stack(config); |
| 95 | + const client = stack.getClient(); |
| 96 | + mockClient = new MockAdapter(client); |
| 97 | + |
| 98 | + mockClient.onGet('/content_types/test').timeout(); |
| 99 | + |
| 100 | + await expect(client.get('/content_types/test')).rejects.toBeDefined(); |
| 101 | + }); |
| 102 | +}); |
0 commit comments