From 1b8ccb2dc71922d3d1feb1170f343c2afd8a97eb Mon Sep 17 00:00:00 2001 From: Derek Xu Date: Tue, 26 Mar 2024 11:00:18 -0400 Subject: [PATCH] [package:http_profile] Make connectionInfo a top-level field of HttpClientRequestProfile --- .../lib/src/http_client_request_profile.dart | 44 +++++++++++++ .../lib/src/http_profile_request_data.dart | 42 ------------- .../lib/src/http_profile_response_data.dart | 35 ----------- .../http_client_request_profile_test.dart | 62 +++++++++++++++++++ .../test/http_profile_request_data_test.dart | 50 --------------- .../test/http_profile_response_data_test.dart | 50 --------------- 6 files changed, 106 insertions(+), 177 deletions(-) diff --git a/pkgs/http_profile/lib/src/http_client_request_profile.dart b/pkgs/http_profile/lib/src/http_client_request_profile.dart index 84db62f1a2..3d2372ba01 100644 --- a/pkgs/http_profile/lib/src/http_client_request_profile.dart +++ b/pkgs/http_profile/lib/src/http_client_request_profile.dart @@ -77,6 +77,50 @@ final class HttpClientRequestProfile { HttpProfileRequestEvent._fromJson, )); + /// Information about the networking connection used. + /// + /// This information is meant to be used for debugging. + /// + /// It can contain any arbitrary data as long as the values are of type + /// [String] or [int]. + /// + /// This field can only be modified by assigning a Map to it. That is: + /// ```dart + /// // Valid + /// profile?.connectionInfo = { + /// 'localPort': 1285, + /// 'remotePort': 443, + /// 'connectionPoolId': '21x23', + /// }; + /// + /// // Invalid + /// profile?.connectionInfo?['localPort'] = 1285; + /// ``` + set connectionInfo(Map? value) { + _updated(); + if (value == null) { + requestData._requestData.remove('connectionInfo'); + responseData._responseData.remove('connectionInfo'); + } else { + for (final v in value.values) { + if (!(v is String || v is int)) { + throw ArgumentError( + 'The values in connectionInfo must be of type String or int.', + ); + } + } + requestData._requestData['connectionInfo'] = {...value}; + responseData._responseData['connectionInfo'] = {...value}; + } + } + + Map? get connectionInfo => + requestData._data['connectionInfo'] == null + ? null + : UnmodifiableMapView( + requestData._data['connectionInfo'] as Map, + ); + /// Details about the request. late final HttpProfileRequestData requestData; diff --git a/pkgs/http_profile/lib/src/http_profile_request_data.dart b/pkgs/http_profile/lib/src/http_profile_request_data.dart index 1463994c12..537643cc2c 100644 --- a/pkgs/http_profile/lib/src/http_profile_request_data.dart +++ b/pkgs/http_profile/lib/src/http_profile_request_data.dart @@ -61,48 +61,6 @@ final class HttpProfileRequestData { List get bodyBytes => UnmodifiableListView(_data['requestBodyBytes'] as List); - /// Information about the networking connection used in the HTTP request. - /// - /// This information is meant to be used for debugging. - /// - /// It can contain any arbitrary data as long as the values are of type - /// [String] or [int]. - /// - /// This field can only be modified by assigning a Map to it. That is: - /// ```dart - /// // Valid - /// profile?.requestData.connectionInfo = { - /// 'localPort': 1285, - /// 'remotePort': 443, - /// 'connectionPoolId': '21x23', - /// }; - /// - /// // Invalid - /// profile?.requestData.connectionInfo?['localPort'] = 1285; - /// ``` - set connectionInfo(Map? value) { - _checkAndUpdate(); - if (value == null) { - _requestData.remove('connectionInfo'); - } else { - for (final v in value.values) { - if (!(v is String || v is int)) { - throw ArgumentError( - 'The values in connectionInfo must be of type String or int.', - ); - } - } - _requestData['connectionInfo'] = {...value}; - } - } - - Map? get connectionInfo => - _requestData['connectionInfo'] == null - ? null - : UnmodifiableMapView( - _requestData['connectionInfo'] as Map, - ); - /// The content length of the request, in bytes. set contentLength(int? value) { _checkAndUpdate(); diff --git a/pkgs/http_profile/lib/src/http_profile_response_data.dart b/pkgs/http_profile/lib/src/http_profile_response_data.dart index 87c769c5e2..5eb452adcb 100644 --- a/pkgs/http_profile/lib/src/http_profile_response_data.dart +++ b/pkgs/http_profile/lib/src/http_profile_response_data.dart @@ -68,41 +68,6 @@ final class HttpProfileResponseData { List get bodyBytes => UnmodifiableListView(_data['responseBodyBytes'] as List); - /// Information about the networking connection used in the HTTP response. - /// - /// This information is meant to be used for debugging. - /// - /// It can contain any arbitrary data as long as the values are of type - /// [String] or [int]. - /// - /// This field can only be modified by assigning a Map to it. That is: - /// ```dart - /// // Valid - /// profile?.responseData.connectionInfo = { - /// 'localPort': 1285, - /// 'remotePort': 443, - /// 'connectionPoolId': '21x23', - /// }; - /// - /// // Invalid - /// profile?.responseData.connectionInfo?['localPort'] = 1285; - /// ``` - set connectionInfo(Map? value) { - _checkAndUpdate(); - if (value == null) { - _responseData.remove('connectionInfo'); - } else { - for (final v in value.values) { - if (!(v is String || v is int)) { - throw ArgumentError( - 'The values in connectionInfo must be of type String or int.', - ); - } - } - _responseData['connectionInfo'] = {...value}; - } - } - Map? get connectionInfo => _responseData['connectionInfo'] as Map?; diff --git a/pkgs/http_profile/test/http_client_request_profile_test.dart b/pkgs/http_profile/test/http_client_request_profile_test.dart index 6c6a9e4884..fdba3e3a6b 100644 --- a/pkgs/http_profile/test/http_client_request_profile_test.dart +++ b/pkgs/http_profile/test/http_client_request_profile_test.dart @@ -77,4 +77,66 @@ void main() { expect(eventFromGetter.timestamp, DateTime.parse('2024-03-22')); expect(eventFromGetter.name, 'an event'); }); + + test('populating HttpClientRequestProfile.connectionInfo', () async { + final requestData = backingMap['requestData'] as Map; + final responseData = backingMap['responseData'] as Map; + expect(requestData['connectionInfo'], isNull); + expect(responseData['connectionInfo'], isNull); + expect(profile.responseData.connectionInfo, isNull); + + profile.connectionInfo = { + 'localPort': 1285, + 'remotePort': 443, + 'connectionPoolId': '21x23' + }; + + final connectionInfoFromRequestData = + requestData['connectionInfo'] as Map; + final connectionInfoFromResponseData = + responseData['connectionInfo'] as Map; + expect(connectionInfoFromRequestData['localPort'], 1285); + expect(connectionInfoFromResponseData['localPort'], 1285); + expect(connectionInfoFromRequestData['remotePort'], 443); + expect(connectionInfoFromResponseData['remotePort'], 443); + expect(connectionInfoFromRequestData['connectionPoolId'], '21x23'); + expect(connectionInfoFromResponseData['connectionPoolId'], '21x23'); + + final connectionInfoFromGetter = profile.responseData.connectionInfo!; + expect(connectionInfoFromGetter['localPort'], 1285); + expect(connectionInfoFromGetter['remotePort'], 443); + expect(connectionInfoFromGetter['connectionPoolId'], '21x23'); + }); + + test('HttpClientRequestProfile.connectionInfo = null', () async { + profile.connectionInfo = { + 'localPort': 1285, + 'remotePort': 443, + 'connectionPoolId': '21x23' + }; + + final requestData = backingMap['requestData'] as Map; + final connectionInfoFromRequestData = + requestData['connectionInfo'] as Map; + final responseData = backingMap['responseData'] as Map; + final connectionInfoFromResponseData = + responseData['connectionInfo'] as Map; + expect(connectionInfoFromRequestData['localPort'], 1285); + expect(connectionInfoFromResponseData['localPort'], 1285); + expect(connectionInfoFromRequestData['remotePort'], 443); + expect(connectionInfoFromResponseData['remotePort'], 443); + expect(connectionInfoFromRequestData['connectionPoolId'], '21x23'); + expect(connectionInfoFromResponseData['connectionPoolId'], '21x23'); + + final connectionInfoFromGetter = profile.responseData.connectionInfo!; + expect(connectionInfoFromGetter['localPort'], 1285); + expect(connectionInfoFromGetter['remotePort'], 443); + expect(connectionInfoFromGetter['connectionPoolId'], '21x23'); + + profile.connectionInfo = null; + + expect(requestData['connectionInfo'], isNull); + expect(responseData['connectionInfo'], isNull); + expect(profile.responseData.connectionInfo, isNull); + }); } diff --git a/pkgs/http_profile/test/http_profile_request_data_test.dart b/pkgs/http_profile/test/http_profile_request_data_test.dart index 6723e81331..36c79a766f 100644 --- a/pkgs/http_profile/test/http_profile_request_data_test.dart +++ b/pkgs/http_profile/test/http_profile_request_data_test.dart @@ -56,56 +56,6 @@ void main() { expect(profile.requestData.endTime, DateTime.parse('2024-03-23')); }); - test('populating HttpClientRequestProfile.requestData.connectionInfo', - () async { - final requestData = backingMap['requestData'] as Map; - expect(requestData['connectionInfo'], isNull); - expect(profile.requestData.connectionInfo, isNull); - - profile.requestData.connectionInfo = { - 'localPort': 1285, - 'remotePort': 443, - 'connectionPoolId': '21x23' - }; - - final connectionInfoFromBackingMap = - requestData['connectionInfo'] as Map; - expect(connectionInfoFromBackingMap['localPort'], 1285); - expect(connectionInfoFromBackingMap['remotePort'], 443); - expect(connectionInfoFromBackingMap['connectionPoolId'], '21x23'); - - final connectionInfoFromGetter = profile.requestData.connectionInfo!; - expect(connectionInfoFromGetter['localPort'], 1285); - expect(connectionInfoFromGetter['remotePort'], 443); - expect(connectionInfoFromGetter['connectionPoolId'], '21x23'); - }); - - test('HttpClientRequestProfile.requestData.connectionInfo = null', () async { - final requestData = backingMap['requestData'] as Map; - - profile.requestData.connectionInfo = { - 'localPort': 1285, - 'remotePort': 443, - 'connectionPoolId': '21x23' - }; - - final connectionInfoFromBackingMap = - requestData['connectionInfo'] as Map; - expect(connectionInfoFromBackingMap['localPort'], 1285); - expect(connectionInfoFromBackingMap['remotePort'], 443); - expect(connectionInfoFromBackingMap['connectionPoolId'], '21x23'); - - final connectionInfoFromGetter = profile.requestData.connectionInfo!; - expect(connectionInfoFromGetter['localPort'], 1285); - expect(connectionInfoFromGetter['remotePort'], 443); - expect(connectionInfoFromGetter['connectionPoolId'], '21x23'); - - profile.requestData.connectionInfo = null; - - expect(requestData['connectionInfo'], isNull); - expect(profile.requestData.connectionInfo, isNull); - }); - test('populating HttpClientRequestProfile.requestData.contentLength', () async { final requestData = backingMap['requestData'] as Map; diff --git a/pkgs/http_profile/test/http_profile_response_data_test.dart b/pkgs/http_profile/test/http_profile_response_data_test.dart index f256971463..9b73cb7b38 100644 --- a/pkgs/http_profile/test/http_profile_response_data_test.dart +++ b/pkgs/http_profile/test/http_profile_response_data_test.dart @@ -52,56 +52,6 @@ void main() { expect(redirectFromGetter.location, 'https://images.example.com/1'); }); - test('populating HttpClientRequestProfile.responseData.connectionInfo', - () async { - final responseData = backingMap['responseData'] as Map; - expect(responseData['connectionInfo'], isNull); - expect(profile.responseData.connectionInfo, isNull); - - profile.responseData.connectionInfo = { - 'localPort': 1285, - 'remotePort': 443, - 'connectionPoolId': '21x23' - }; - - final connectionInfoFromBackingMap = - responseData['connectionInfo'] as Map; - expect(connectionInfoFromBackingMap['localPort'], 1285); - expect(connectionInfoFromBackingMap['remotePort'], 443); - expect(connectionInfoFromBackingMap['connectionPoolId'], '21x23'); - - final connectionInfoFromGetter = profile.responseData.connectionInfo!; - expect(connectionInfoFromGetter['localPort'], 1285); - expect(connectionInfoFromGetter['remotePort'], 443); - expect(connectionInfoFromGetter['connectionPoolId'], '21x23'); - }); - - test('HttpClientRequestProfile.responseData.connectionInfo = null', () async { - final responseData = backingMap['responseData'] as Map; - - profile.responseData.connectionInfo = { - 'localPort': 1285, - 'remotePort': 443, - 'connectionPoolId': '21x23' - }; - - final connectionInfoFromBackingMap = - responseData['connectionInfo'] as Map; - expect(connectionInfoFromBackingMap['localPort'], 1285); - expect(connectionInfoFromBackingMap['remotePort'], 443); - expect(connectionInfoFromBackingMap['connectionPoolId'], '21x23'); - - final connectionInfoFromGetter = profile.responseData.connectionInfo!; - expect(connectionInfoFromGetter['localPort'], 1285); - expect(connectionInfoFromGetter['remotePort'], 443); - expect(connectionInfoFromGetter['connectionPoolId'], '21x23'); - - profile.responseData.connectionInfo = null; - - expect(responseData['connectionInfo'], isNull); - expect(profile.responseData.connectionInfo, isNull); - }); - test('populating HttpClientRequestProfile.responseData.headersListValues', () async { final responseData = backingMap['responseData'] as Map;