Skip to content

Commit d73e7d5

Browse files
committed
chore: add default value of 10KB as a fallback, add network body size specific tests
- Introduced a default network body max size of 10KB to improve logging consistency. - Updated error handling to set the cached size to the default when native API retrieval fails. - Added unit tests for `didRequestBodyExceedSizeLimit` and `didResponseBodyExceedSizeLimit` to ensure proper size limit checks for request and response bodies.
1 parent efd961d commit d73e7d5

File tree

3 files changed

+234
-2
lines changed

3 files changed

+234
-2
lines changed

lib/src/utils/network_manager.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class NetworkManager {
1515
ObfuscateLogCallback? _obfuscateLogCallback;
1616
OmitLogCallback? _omitLogCallback;
1717
int? _cachedNetworkBodyMaxSize;
18+
int _defaultNetworkBodyMaxSize = 10240; // in bytes
1819
final _host = InstabugHostApi();
1920

2021
// ignore: use_setters_to_change_properties
@@ -124,10 +125,13 @@ class NetworkManager {
124125
return limit;
125126
} catch (error) {
126127
InstabugLogger.I.e(
127-
'Failed to get network body max size from native API: $error',
128+
'Failed to get network body max size from native API: $error'
129+
'\n'
130+
'Setting it to the default value of $_defaultNetworkBodyMaxSize bytes = ${_defaultNetworkBodyMaxSize / 1024} KB',
128131
tag: InstabugConstants.networkManagerTag,
129132
);
130-
return null;
133+
_cachedNetworkBodyMaxSize = _defaultNetworkBodyMaxSize;
134+
return _defaultNetworkBodyMaxSize;
131135
}
132136
}
133137
}

test/network_logger_test.dart

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ void main() {
6060
"isW3cCaughtHeaderEnabled": true,
6161
}),
6262
);
63+
when(mManager.didRequestBodyExceedSizeLimit(any))
64+
.thenAnswer((_) async => false);
65+
when(mManager.didResponseBodyExceedSizeLimit(any))
66+
.thenAnswer((_) async => false);
6367
});
6468

6569
test('[networkLog] should call 1 host method on iOS', () async {
@@ -239,4 +243,152 @@ void main() {
239243
mInstabugHost.setNetworkLogBodyEnabled(enabled),
240244
).called(1);
241245
});
246+
247+
group('[networkLogInternal] body size limit tests', () {
248+
test('should replace request body when it exceeds size limit', () async {
249+
final largeRequestData = data.copyWith(
250+
requestBodySize: 15000, // 15KB > 10KB default
251+
responseBodySize: 5000, // 5KB < 10KB default
252+
);
253+
254+
when(mBuildInfo.isAndroid).thenReturn(true);
255+
when(mManager.obfuscateLog(any)).thenAnswer((invocation) async {
256+
final inputData = invocation.positionalArguments[0] as NetworkData;
257+
return inputData;
258+
});
259+
when(mManager.omitLog(largeRequestData)).thenReturn(false);
260+
when(mManager.didRequestBodyExceedSizeLimit(largeRequestData))
261+
.thenAnswer((_) async => true);
262+
when(mManager.didResponseBodyExceedSizeLimit(largeRequestData))
263+
.thenAnswer((_) async => false);
264+
265+
await logger.networkLogInternal(largeRequestData);
266+
267+
// Verify that obfuscateLog was called with modified data
268+
verify(mManager.obfuscateLog(argThat(
269+
predicate<NetworkData>((processedData) =>
270+
processedData.requestBody ==
271+
'[REQUEST_BODY_REPLACED] - Size: 15000 exceeds limit' &&
272+
processedData.responseBody == largeRequestData.responseBody),
273+
))).called(1);
274+
275+
// Verify that networkLog was called
276+
verify(mInstabugHost.networkLog(any)).called(1);
277+
verify(mApmHost.networkLogAndroid(any)).called(1);
278+
});
279+
280+
test('should replace response body when it exceeds size limit', () async {
281+
final largeResponseData = data.copyWith(
282+
requestBodySize: 5000, // 5KB < 10KB default
283+
responseBodySize: 15000, // 15KB > 10KB default
284+
);
285+
286+
when(mBuildInfo.isAndroid).thenReturn(true);
287+
when(mManager.obfuscateLog(any)).thenAnswer((invocation) async {
288+
final inputData = invocation.positionalArguments[0] as NetworkData;
289+
return inputData;
290+
});
291+
when(mManager.omitLog(largeResponseData)).thenReturn(false);
292+
when(mManager.didRequestBodyExceedSizeLimit(largeResponseData))
293+
.thenAnswer((_) async => false);
294+
when(mManager.didResponseBodyExceedSizeLimit(largeResponseData))
295+
.thenAnswer((_) async => true);
296+
297+
await logger.networkLogInternal(largeResponseData);
298+
299+
// Verify that obfuscateLog was called with modified data
300+
verify(mManager.obfuscateLog(argThat(
301+
predicate<NetworkData>((processedData) =>
302+
processedData.requestBody == largeResponseData.requestBody &&
303+
processedData.responseBody ==
304+
'[RESPONSE_BODY_REPLACED] - Size: 15000 exceeds limit'),
305+
))).called(1);
306+
307+
// Verify that networkLog was called
308+
verify(mInstabugHost.networkLog(any)).called(1);
309+
verify(mApmHost.networkLogAndroid(any)).called(1);
310+
});
311+
312+
test('should replace both bodies when both exceed size limit', () async {
313+
final largeBothData = data.copyWith(
314+
requestBodySize: 15000, // 15KB > 10KB default
315+
responseBodySize: 15000, // 15KB > 10KB default
316+
);
317+
318+
when(mBuildInfo.isAndroid).thenReturn(true);
319+
when(mManager.obfuscateLog(any)).thenAnswer((invocation) async {
320+
final inputData = invocation.positionalArguments[0] as NetworkData;
321+
return inputData;
322+
});
323+
when(mManager.omitLog(largeBothData)).thenReturn(false);
324+
when(mManager.didRequestBodyExceedSizeLimit(largeBothData))
325+
.thenAnswer((_) async => true);
326+
when(mManager.didResponseBodyExceedSizeLimit(largeBothData))
327+
.thenAnswer((_) async => true);
328+
329+
await logger.networkLogInternal(largeBothData);
330+
331+
// Verify that obfuscateLog was called with modified data
332+
verify(mManager.obfuscateLog(argThat(
333+
predicate<NetworkData>((processedData) =>
334+
processedData.requestBody ==
335+
'[REQUEST_BODY_REPLACED] - Size: 15000 exceeds limit' &&
336+
processedData.responseBody ==
337+
'[RESPONSE_BODY_REPLACED] - Size: 15000 exceeds limit'),
338+
))).called(1);
339+
340+
// Verify that networkLog was called
341+
verify(mInstabugHost.networkLog(any)).called(1);
342+
verify(mApmHost.networkLogAndroid(any)).called(1);
343+
});
344+
345+
test('should not replace bodies when both are within size limit', () async {
346+
final smallData = data.copyWith(
347+
requestBodySize: 5000, // 5KB < 10KB default
348+
responseBodySize: 5000, // 5KB < 10KB default
349+
);
350+
351+
when(mBuildInfo.isAndroid).thenReturn(true);
352+
when(mManager.obfuscateLog(any)).thenAnswer((invocation) async {
353+
final inputData = invocation.positionalArguments[0] as NetworkData;
354+
return inputData;
355+
});
356+
when(mManager.omitLog(smallData)).thenReturn(false);
357+
when(mManager.didRequestBodyExceedSizeLimit(smallData))
358+
.thenAnswer((_) async => false);
359+
when(mManager.didResponseBodyExceedSizeLimit(smallData))
360+
.thenAnswer((_) async => false);
361+
362+
await logger.networkLogInternal(smallData);
363+
364+
// Verify that obfuscateLog was called with original data
365+
verify(mManager.obfuscateLog(smallData)).called(1);
366+
367+
// Verify that networkLog was called
368+
verify(mInstabugHost.networkLog(any)).called(1);
369+
verify(mApmHost.networkLogAndroid(any)).called(1);
370+
});
371+
372+
test('should not log when data should be omitted', () async {
373+
final largeData = data.copyWith(
374+
requestBodySize: 15000, // 15KB > 10KB default
375+
);
376+
377+
when(mBuildInfo.isAndroid).thenReturn(true);
378+
when(mManager.omitLog(largeData)).thenReturn(true);
379+
380+
await logger.networkLogInternal(largeData);
381+
382+
// Verify that omitLog was called
383+
verify(mManager.omitLog(largeData)).called(1);
384+
385+
// Verify that size limit checks were not called
386+
verifyNever(mManager.didRequestBodyExceedSizeLimit(any));
387+
verifyNever(mManager.didResponseBodyExceedSizeLimit(any));
388+
389+
// Verify that networkLog was not called
390+
verifyNever(mInstabugHost.networkLog(any));
391+
verifyNever(mApmHost.networkLogAndroid(any));
392+
});
393+
});
242394
}

test/network_manager_test.dart

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ void main() {
1313
url: "https://httpbin.org/get",
1414
method: "GET",
1515
startTime: DateTime.now(),
16+
requestBodySize: 1000,
17+
responseBodySize: 2000,
1618
);
1719
late NetworkManager manager;
1820

@@ -76,4 +78,78 @@ void main() {
7678

7779
expect(result, equals(omit));
7880
});
81+
82+
group('[didRequestBodyExceedSizeLimit]', () {
83+
test('should return false when request body size is within default limit',
84+
() async {
85+
final smallData =
86+
data.copyWith(requestBodySize: 5000); // 5KB < 10KB default
87+
88+
final result = await manager.didRequestBodyExceedSizeLimit(smallData);
89+
90+
expect(result, isFalse);
91+
});
92+
93+
test('should return true when request body size exceeds default limit',
94+
() async {
95+
final largeData =
96+
data.copyWith(requestBodySize: 15000); // 15KB > 10KB default
97+
98+
final result = await manager.didRequestBodyExceedSizeLimit(largeData);
99+
100+
expect(result, isTrue);
101+
});
102+
103+
test('should return false when request body size equals default limit',
104+
() async {
105+
final exactData = data.copyWith(requestBodySize: 10240); // Exactly 10KB
106+
107+
final result = await manager.didRequestBodyExceedSizeLimit(exactData);
108+
109+
expect(result, isFalse);
110+
});
111+
112+
test('should handle errors gracefully and return false', () async {
113+
final result = await manager.didRequestBodyExceedSizeLimit(data);
114+
115+
expect(result, isA<bool>());
116+
});
117+
});
118+
119+
group('[didResponseBodyExceedSizeLimit]', () {
120+
test('should return false when response body size is within default limit',
121+
() async {
122+
final smallData =
123+
data.copyWith(responseBodySize: 5000); // 5KB < 10KB default
124+
125+
final result = await manager.didResponseBodyExceedSizeLimit(smallData);
126+
127+
expect(result, isFalse);
128+
});
129+
130+
test('should return true when response body size exceeds default limit',
131+
() async {
132+
final largeData =
133+
data.copyWith(responseBodySize: 15000); // 15KB > 10KB default
134+
135+
final result = await manager.didResponseBodyExceedSizeLimit(largeData);
136+
137+
expect(result, isTrue);
138+
});
139+
140+
test('should return false when response body size equals default limit',
141+
() async {
142+
final exactData = data.copyWith(responseBodySize: 10240); // Exactly 10KB
143+
144+
final result = await manager.didResponseBodyExceedSizeLimit(exactData);
145+
146+
expect(result, isFalse);
147+
});
148+
149+
test('should handle errors gracefully and return false', () async {
150+
final result = await manager.didResponseBodyExceedSizeLimit(data);
151+
152+
expect(result, isA<bool>());
153+
});
154+
});
79155
}

0 commit comments

Comments
 (0)