Skip to content

Commit d726f8a

Browse files
committed
test: add unit tests for content type and max size limits
1 parent feda3c8 commit d726f8a

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

test/mocks/mockInstabugUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ jest.mock('../../src/utils/InstabugUtils', () => {
1111
getStackTrace: jest.fn().mockReturnValue('javascriptStackTrace'),
1212
getFullRoute: jest.fn().mockImplementation(() => 'ScreenName'),
1313
reportNetworkLog: jest.fn(),
14+
isContentTypeNotAllowed: jest.fn(),
1415
};
1516
});

test/modules/NetworkLogger.spec.ts

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import waitForExpect from 'wait-for-expect';
55

66
import * as NetworkLogger from '../../src/modules/NetworkLogger';
77
import Interceptor from '../../src/utils/XhrNetworkInterceptor';
8-
import { reportNetworkLog } from '../../src/utils/InstabugUtils';
8+
import { isContentTypeNotAllowed, reportNetworkLog } from '../../src/utils/InstabugUtils';
9+
import InstabugConstants from '../../src/utils/InstabugConstants';
910

1011
const clone = <T>(obj: T): T => {
1112
return JSON.parse(JSON.stringify(obj));
@@ -33,6 +34,7 @@ describe('NetworkLogger Module', () => {
3334

3435
beforeEach(() => {
3536
NetworkLogger.setNetworkDataObfuscationHandler(null);
37+
NetworkLogger.setRequestFilterExpression('false');
3638
});
3739

3840
it('should set onProgressCallback with callback', () => {
@@ -146,4 +148,132 @@ describe('NetworkLogger Module', () => {
146148

147149
consoleSpy.mockRestore();
148150
});
151+
152+
it('should omit request body if its content type is not allowed', () => {
153+
const consoleWarn = jest.spyOn(console, 'warn').mockImplementation();
154+
jest.mocked(isContentTypeNotAllowed).mockReturnValueOnce(true);
155+
156+
const networkData = {
157+
...network,
158+
requestBody: 'some request body',
159+
};
160+
161+
Interceptor.setOnDoneCallback = jest
162+
.fn()
163+
.mockImplementation((callback) => callback(networkData));
164+
165+
NetworkLogger.setEnabled(true);
166+
167+
expect(reportNetworkLog).toHaveBeenCalledWith({
168+
...networkData,
169+
requestBody: expect.stringContaining('omitted'),
170+
});
171+
172+
expect(consoleWarn).toBeCalledTimes(1);
173+
174+
consoleWarn.mockRestore();
175+
});
176+
177+
it('should omit response body if its content type is not allowed', () => {
178+
const consoleWarn = jest.spyOn(console, 'warn').mockImplementation();
179+
jest.mocked(isContentTypeNotAllowed).mockReturnValueOnce(true);
180+
181+
const networkData = {
182+
...network,
183+
responseBody: 'some response body',
184+
};
185+
186+
Interceptor.setOnDoneCallback = jest
187+
.fn()
188+
.mockImplementation((callback) => callback(networkData));
189+
190+
NetworkLogger.setEnabled(true);
191+
192+
expect(reportNetworkLog).toHaveBeenCalledWith({
193+
...networkData,
194+
responseBody: expect.stringContaining('omitted'),
195+
});
196+
197+
expect(consoleWarn).toBeCalledTimes(1);
198+
199+
consoleWarn.mockRestore();
200+
});
201+
202+
it('should omit request body if its size exceeds the maximum allowed size', () => {
203+
const consoleWarn = jest.spyOn(console, 'warn').mockImplementation();
204+
205+
const networkData = {
206+
...network,
207+
requestBodySize: InstabugConstants.MAX_NETWORK_BODY_SIZE_IN_BYTES + 1,
208+
};
209+
210+
Interceptor.setOnDoneCallback = jest
211+
.fn()
212+
.mockImplementation((callback) => callback(networkData));
213+
214+
NetworkLogger.setEnabled(true);
215+
216+
expect(reportNetworkLog).toHaveBeenCalledWith({
217+
...networkData,
218+
requestBody: InstabugConstants.MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE,
219+
});
220+
221+
expect(consoleWarn).toBeCalledTimes(1);
222+
223+
consoleWarn.mockRestore();
224+
});
225+
226+
it('should not omit request body if its size does not exceed the maximum allowed size', () => {
227+
const networkData = {
228+
...network,
229+
requestBodySize: InstabugConstants.MAX_NETWORK_BODY_SIZE_IN_BYTES,
230+
};
231+
232+
Interceptor.setOnDoneCallback = jest
233+
.fn()
234+
.mockImplementation((callback) => callback(networkData));
235+
236+
NetworkLogger.setEnabled(true);
237+
238+
expect(reportNetworkLog).toHaveBeenCalledWith(networkData);
239+
});
240+
241+
it('should omit response body if its size exceeds the maximum allowed size', () => {
242+
const consoleWarn = jest.spyOn(console, 'warn').mockImplementation();
243+
244+
const networkData = {
245+
...network,
246+
responseBodySize: InstabugConstants.MAX_NETWORK_BODY_SIZE_IN_BYTES + 1,
247+
};
248+
249+
Interceptor.setOnDoneCallback = jest
250+
.fn()
251+
.mockImplementation((callback) => callback(networkData));
252+
253+
NetworkLogger.setEnabled(true);
254+
255+
expect(reportNetworkLog).toHaveBeenCalledWith({
256+
...networkData,
257+
responseBody: InstabugConstants.MAX_RESPONSE_BODY_SIZE_EXCEEDED_MESSAGE,
258+
});
259+
260+
expect(consoleWarn).toBeCalledTimes(1);
261+
262+
consoleWarn.mockRestore();
263+
});
264+
265+
it('should not omit response body if its size does not exceed the maximum allowed size', () => {
266+
const networkData = {
267+
...network,
268+
responseBodySize: InstabugConstants.MAX_NETWORK_BODY_SIZE_IN_BYTES,
269+
};
270+
271+
Interceptor.setOnDoneCallback = jest
272+
.fn()
273+
.mockImplementation((callback) => callback(networkData));
274+
275+
NetworkLogger.setEnabled(true);
276+
277+
expect(reportNetworkLog).toHaveBeenCalledWith(networkData);
278+
});
149279
});

0 commit comments

Comments
 (0)