|
| 1 | +import { httpClient, AxiosInstance } from '@contentstack/core'; |
| 2 | +import * as Contentstack from '../../src/lib/contentstack'; |
| 3 | +import { Stack } from '../../src/lib/stack'; |
| 4 | +import { Policy, StackConfig } from '../../src/lib/types'; |
| 5 | +import MockAdapter from 'axios-mock-adapter'; |
| 6 | + |
| 7 | +describe('Contentstack Debug Logging Integration', () => { |
| 8 | + let mockLogHandler: jest.Mock; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + mockLogHandler = jest.fn(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should execute debug logging for request interceptor', async () => { |
| 15 | + const config: StackConfig = { |
| 16 | + apiKey: "apiKey", |
| 17 | + deliveryToken: "delivery", |
| 18 | + environment: "env", |
| 19 | + debug: true, |
| 20 | + logHandler: mockLogHandler, |
| 21 | + }; |
| 22 | + |
| 23 | + const stack = Contentstack.stack(config); |
| 24 | + const client = stack.getClient(); |
| 25 | + const mockClient = new MockAdapter(client); |
| 26 | + |
| 27 | + mockClient.onGet('/content_types/test').reply(200, { |
| 28 | + content_types: [] |
| 29 | + }); |
| 30 | + |
| 31 | + // Make an actual request to trigger interceptors |
| 32 | + try { |
| 33 | + await client.get('/content_types/test'); |
| 34 | + } catch (e) { |
| 35 | + // Ignore errors |
| 36 | + } |
| 37 | + |
| 38 | + // Verify request logging was called |
| 39 | + const requestLogs = mockLogHandler.mock.calls.filter((call: any) => |
| 40 | + call[1]?.type === 'request' |
| 41 | + ); |
| 42 | + expect(requestLogs.length).toBeGreaterThan(0); |
| 43 | + |
| 44 | + mockClient.restore(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should execute debug logging for response interceptor with 2xx status', async () => { |
| 48 | + const config: StackConfig = { |
| 49 | + apiKey: "apiKey", |
| 50 | + deliveryToken: "delivery", |
| 51 | + environment: "env", |
| 52 | + debug: true, |
| 53 | + logHandler: mockLogHandler, |
| 54 | + }; |
| 55 | + |
| 56 | + const stack = Contentstack.stack(config); |
| 57 | + const client = stack.getClient(); |
| 58 | + const mockClient = new MockAdapter(client); |
| 59 | + |
| 60 | + mockClient.onGet('/content_types/test').reply(200, { |
| 61 | + content_types: [] |
| 62 | + }); |
| 63 | + |
| 64 | + await client.get('/content_types/test'); |
| 65 | + |
| 66 | + // Verify response logging was called with info level |
| 67 | + const responseLogs = mockLogHandler.mock.calls.filter((call: any) => |
| 68 | + call[1]?.type === 'response' && call[0] === 'info' |
| 69 | + ); |
| 70 | + expect(responseLogs.length).toBeGreaterThan(0); |
| 71 | + |
| 72 | + mockClient.restore(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should execute debug logging for response interceptor with 3xx status', async () => { |
| 76 | + const config: StackConfig = { |
| 77 | + apiKey: "apiKey", |
| 78 | + deliveryToken: "delivery", |
| 79 | + environment: "env", |
| 80 | + debug: true, |
| 81 | + logHandler: mockLogHandler, |
| 82 | + }; |
| 83 | + |
| 84 | + const stack = Contentstack.stack(config); |
| 85 | + const client = stack.getClient(); |
| 86 | + const mockClient = new MockAdapter(client); |
| 87 | + |
| 88 | + // 3xx responses are treated as errors by axios-mock-adapter |
| 89 | + mockClient.onGet('/content_types/test').reply(304, {}); |
| 90 | + |
| 91 | + try { |
| 92 | + await client.get('/content_types/test'); |
| 93 | + } catch (e) { |
| 94 | + // Expected - 3xx responses trigger error handler in mock adapter |
| 95 | + } |
| 96 | + |
| 97 | + // Verify error response logging was called - 3xx goes through error interceptor |
| 98 | + const errorLogs = mockLogHandler.mock.calls.filter((call: any) => |
| 99 | + call[1]?.type === 'response_error' && call[1]?.status === 304 |
| 100 | + ); |
| 101 | + expect(errorLogs.length).toBeGreaterThan(0); |
| 102 | + |
| 103 | + mockClient.restore(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should execute debug logging for error response interceptor with 4xx status', async () => { |
| 107 | + const config: StackConfig = { |
| 108 | + apiKey: "apiKey", |
| 109 | + deliveryToken: "delivery", |
| 110 | + environment: "env", |
| 111 | + debug: true, |
| 112 | + logHandler: mockLogHandler, |
| 113 | + }; |
| 114 | + |
| 115 | + const stack = Contentstack.stack(config); |
| 116 | + const client = stack.getClient(); |
| 117 | + const mockClient = new MockAdapter(client); |
| 118 | + |
| 119 | + mockClient.onGet('/content_types/test').reply(404, { |
| 120 | + error: 'Not found' |
| 121 | + }); |
| 122 | + |
| 123 | + try { |
| 124 | + await client.get('/content_types/test'); |
| 125 | + } catch (e) { |
| 126 | + // Expected error |
| 127 | + } |
| 128 | + |
| 129 | + // Verify error logging was called |
| 130 | + const errorLogs = mockLogHandler.mock.calls.filter((call: any) => |
| 131 | + call[1]?.type === 'response_error' && call[0] === 'error' |
| 132 | + ); |
| 133 | + expect(errorLogs.length).toBeGreaterThan(0); |
| 134 | + |
| 135 | + mockClient.restore(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should execute debug logging for error response without status', async () => { |
| 139 | + const config: StackConfig = { |
| 140 | + apiKey: "apiKey", |
| 141 | + deliveryToken: "delivery", |
| 142 | + environment: "env", |
| 143 | + debug: true, |
| 144 | + logHandler: mockLogHandler, |
| 145 | + }; |
| 146 | + |
| 147 | + const stack = Contentstack.stack(config); |
| 148 | + const client = stack.getClient(); |
| 149 | + const mockClient = new MockAdapter(client); |
| 150 | + |
| 151 | + mockClient.onGet('/content_types/test').networkError(); |
| 152 | + |
| 153 | + try { |
| 154 | + await client.get('/content_types/test'); |
| 155 | + } catch (e) { |
| 156 | + // Expected network error |
| 157 | + } |
| 158 | + |
| 159 | + // Verify error logging was called with debug level for no status |
| 160 | + const errorLogs = mockLogHandler.mock.calls.filter((call: any) => |
| 161 | + call[1]?.type === 'response_error' && call[0] === 'debug' |
| 162 | + ); |
| 163 | + expect(errorLogs.length).toBeGreaterThan(0); |
| 164 | + |
| 165 | + mockClient.restore(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should execute cache adapter when cacheOptions is provided', async () => { |
| 169 | + const config: StackConfig = { |
| 170 | + apiKey: "apiKey", |
| 171 | + deliveryToken: "delivery", |
| 172 | + environment: "env", |
| 173 | + cacheOptions: { |
| 174 | + policy: Policy.CACHE_THEN_NETWORK, |
| 175 | + maxAge: 3600, |
| 176 | + }, |
| 177 | + }; |
| 178 | + |
| 179 | + const stack = Contentstack.stack(config); |
| 180 | + const client = stack.getClient(); |
| 181 | + const mockClient = new MockAdapter(client); |
| 182 | + |
| 183 | + mockClient.onGet('/content_types/test').reply(200, { |
| 184 | + content_types: [] |
| 185 | + }); |
| 186 | + |
| 187 | + // Make request to trigger cache adapter |
| 188 | + await client.get('/content_types/test', { contentTypeUid: 'test' }); |
| 189 | + |
| 190 | + expect(client.defaults.adapter).toBeDefined(); |
| 191 | + |
| 192 | + mockClient.restore(); |
| 193 | + }); |
| 194 | +}); |
| 195 | + |
0 commit comments