Skip to content

Commit 71e3db3

Browse files
committed
Addressed Rquested Changes: refactor formatBytes into http_utils, update units and added tests for responseBytes & formatBytes
1 parent b4a124b commit 71e3db3

6 files changed

Lines changed: 109 additions & 33 deletions

File tree

packages/devtools_app/lib/src/screens/network/network_request_inspector_views.dart

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import '../../shared/ui/colors.dart';
1717
import '../../shared/ui/common_widgets.dart';
1818
import 'network_controller.dart';
1919
import 'network_model.dart';
20+
import 'utils/http_utils.dart';
2021

2122
// Approximately double the indent of the expandable tile's title.
2223
const _rowIndentPadding = 30.0;
@@ -663,7 +664,7 @@ class NetworkRequestOverviewView extends StatelessWidget {
663664
_buildRow(
664665
context: context,
665666
title: 'Response Size',
666-
child: _valueText(bytes != null ? _formatBytes(bytes) : '-'),
667+
child: _valueText(bytes != null ? formatBytes(bytes) : '-'),
667668
),
668669
const SizedBox(height: defaultSpacing),
669670

@@ -678,21 +679,6 @@ class NetworkRequestOverviewView extends StatelessWidget {
678679
];
679680
}
680681

681-
// Output Formats:
682-
// - 512 → "512 B"
683-
// - 2000 → "2.0 kB"
684-
// - 1000000 → "1.0 MB"
685-
// Values are rounded to one decimal place for kB and MB.
686-
// Uses decimal (base-10) units to match Chrome DevTools.
687-
String _formatBytes(int? bytes) {
688-
if (bytes == null) return '-';
689-
if (bytes < 1000) return '$bytes B';
690-
if (bytes < 1000 * 1000) {
691-
return '${(bytes / 1000).toStringAsFixed(1)} kB';
692-
}
693-
return '${(bytes / (1000 * 1000)).toStringAsFixed(1)} MB';
694-
}
695-
696682
List<Widget> _buildTimingOverview(BuildContext context) {
697683
return [
698684
_buildRow(

packages/devtools_app/lib/src/screens/network/network_screen.dart

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import '../../shared/ui/utils.dart';
3030
import 'network_controller.dart';
3131
import 'network_model.dart';
3232
import 'network_request_inspector.dart';
33+
import 'utils/http_utils.dart';
3334

3435
class NetworkScreen extends Screen {
3536
NetworkScreen() : super.fromMetaData(ScreenMetaData.network);
@@ -407,21 +408,6 @@ class NetworkRequestsTable extends StatelessWidget {
407408
}
408409
}
409410

410-
// Output Formats:
411-
// - 512 → "512 B"
412-
// - 2000 → "2.0 kB"
413-
// - 1000000 → "1.0 MB"
414-
// Values are rounded to one decimal place for kB and MB.
415-
// Uses decimal (base-10) units to match Chrome DevTools.
416-
String _formatBytes(int? bytes) {
417-
if (bytes == null) return '-';
418-
if (bytes < 1000) return '$bytes B';
419-
if (bytes < 1000 * 1000) {
420-
return '${(bytes / 1000).toStringAsFixed(1)} kB';
421-
}
422-
return '${(bytes / (1000 * 1000)).toStringAsFixed(1)} MB';
423-
}
424-
425411
class ResponseSizeColumn extends ColumnData<NetworkRequest> {
426412
const ResponseSizeColumn()
427413
: super('Size', alignment: ColumnAlignment.right, fixedWidthPx: 90);
@@ -432,7 +418,7 @@ class ResponseSizeColumn extends ColumnData<NetworkRequest> {
432418
@override
433419
String getDisplayValue(NetworkRequest dataObject) {
434420
final bytes = dataObject.responseBytes;
435-
return bytes != null ? _formatBytes(bytes) : '-';
421+
return bytes != null ? formatBytes(bytes) : '-';
436422
}
437423
}
438424

packages/devtools_app/lib/src/screens/network/utils/http_utils.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,18 @@ int calculateHeadersSize(Map<String, Object?>? headers) {
2929
// Calculate the byte length of the headers string
3030
return utf8.encode(headersString).length;
3131
}
32+
33+
// Output Formats:
34+
// - 512 → "512 B"
35+
// - 2000 → "2.0 kB"
36+
// - 1000000 → "1.0 MB"
37+
// Values are rounded to one decimal place for kB and MB.
38+
// Uses decimal (base-10) units to match Chrome DevTools.
39+
String formatBytes(int? bytes) {
40+
if (bytes == null) return '-';
41+
if (bytes < 1000) return '$bytes B';
42+
if (bytes < 1000 * 1000) {
43+
return '${(bytes / 1000).toStringAsFixed(1)} kB';
44+
}
45+
return '${(bytes / (1000 * 1000)).toStringAsFixed(1)} MB';
46+
}

packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ TODO: Remove this section if there are not any updates.
3939

4040
## Network profiler updates
4141

42-
- Added response size ("Res Size") column to the Network tab and displayed response size in the request inspector overview. -
42+
- Added response size column to the Network tab and displayed response size in the request inspector overview. -
4343
[#9744](https://github.com/flutter/devtools/pull/9744)
4444

4545
- Added a filter setting to hide HTTP-profiler socket data. [#9698](https://github.com/flutter/devtools/pull/9698)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import 'package:devtools_app/src/shared/http/http_request_data.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
group('responseBytes', () {
6+
Map<String, dynamic> baseJson(Map<String, Object?> headers) {
7+
return {
8+
'method': 'GET',
9+
'uri': 'https://example.com',
10+
'status': 200,
11+
'responseHeaders': headers,
12+
};
13+
}
14+
15+
// Verifies parsing when content-length is a string value.
16+
test('parses content-length from string', () {
17+
final request = DartIOHttpRequestData.fromJson(
18+
baseJson({'content-length': '1234'}),
19+
null, // requestPostData not used for this test
20+
null, // responseContent not used for this test
21+
);
22+
23+
expect(request.responseBytes, 1234);
24+
});
25+
26+
// Verifies parsing when content-length is a list of strings.
27+
test('parses content-length from list of strings', () {
28+
final request = DartIOHttpRequestData.fromJson(
29+
baseJson({'content-length': '5678'}),
30+
null, // requestPostData not used for this test
31+
null, // responseContent not used for this test
32+
);
33+
34+
expect(request.responseBytes, 5678);
35+
});
36+
37+
// Ensures integer values inside a list are handled correctly.
38+
test('handles integer in list', () {
39+
final request = DartIOHttpRequestData.fromJson(
40+
baseJson({'content-length': '91011'}),
41+
null, // requestPostData not used for this test
42+
null, // responseContent not used for this test
43+
);
44+
45+
expect(request.responseBytes, 91011);
46+
});
47+
48+
// Returns null when header is missing.
49+
test('returns null for missing header', () {
50+
final request = DartIOHttpRequestData.fromJson(
51+
baseJson({}), // No content-length header
52+
null, // requestPostData not used for this test
53+
null, // responseContent not used for this test
54+
);
55+
56+
expect(request.responseBytes, null);
57+
});
58+
59+
// Returns null when parsing fails.
60+
test('returns null for invalid value', () {
61+
final request = DartIOHttpRequestData.fromJson(
62+
baseJson({'content-length': 'invalid'}),
63+
null, // requestPostData not used for this test
64+
null, // responseContent not used for this test
65+
);
66+
67+
expect(request.responseBytes, null);
68+
});
69+
});
70+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:devtools_app/src/screens/network/utils/http_utils.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
group('formatBytes', () {
6+
// Verifies correct formatting across different unit ranges.
7+
test('formats bytes correctly', () {
8+
expect(formatBytes(512), '512 B'); // bytes
9+
expect(formatBytes(2000), '2.0 kB'); // kilobytes (base-10)
10+
expect(formatBytes(1000000), '1.0 MB'); // megabytes (base-10)
11+
});
12+
13+
// Ensures handling of invalid or missing values.
14+
test('handles null and negative values', () {
15+
expect(formatBytes(null), '-');
16+
expect(formatBytes(-1), '-');
17+
});
18+
});
19+
}

0 commit comments

Comments
 (0)