Skip to content

Commit

Permalink
Ignore errors added to bodySinks (#1164)
Browse files Browse the repository at this point in the history
* Ignore errors added to `bodySink`s

* Add functionality
  • Loading branch information
brianquinlan authored Mar 28, 2024
1 parent 280d361 commit 70cf298
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
9 changes: 5 additions & 4 deletions pkgs/http_profile/lib/src/http_client_request_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ final class HttpClientRequestProfile {
responseData = HttpProfileResponseData._(_data, _updated);
_data['requestBodyBytes'] = <int>[];
requestData._body.stream.listen(
(final bytes) => (_data['requestBodyBytes'] as List<int>).addAll(bytes),
);
(final bytes) => (_data['requestBodyBytes'] as List<int>).addAll(bytes),
onError: (e) {});
_data['responseBodyBytes'] = <int>[];
responseData._body.stream.listen(
(final bytes) => (_data['responseBodyBytes'] as List<int>).addAll(bytes),
);
(final bytes) =>
(_data['responseBodyBytes'] as List<int>).addAll(bytes),
onError: (e) {});
// This entry is needed to support the updatedSince parameter of
// ext.dart.io.getHttpProfile.
_updated();
Expand Down
3 changes: 3 additions & 0 deletions pkgs/http_profile/lib/src/http_profile_request_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ final class HttpProfileRequestData {
_data['requestData'] as Map<String, dynamic>;

/// A sink that can be used to record the body of the request.
///
/// Errors added to [bodySink] (for example with [StreamSink.addError]) are
/// ignored.
StreamSink<List<int>> get bodySink => _body.sink;

/// The body of the request represented as an unmodifiable list of bytes.
Expand Down
3 changes: 3 additions & 0 deletions pkgs/http_profile/lib/src/http_profile_response_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ final class HttpProfileResponseData {
.map(HttpProfileRedirectData._fromJson));

/// A sink that can be used to record the body of the response.
///
/// Errors added to [bodySink] (for example with [StreamSink.addError]) are
/// ignored.
StreamSink<List<int>> get bodySink => _body.sink;

/// The body of the response represented as an unmodifiable list of bytes.
Expand Down
6 changes: 4 additions & 2 deletions pkgs/http_profile/test/http_profile_request_data_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,11 @@ void main() {
expect(profile.requestData.bodyBytes, isEmpty);

profile.requestData.bodySink.add([1, 2, 3]);
profile.requestData.bodySink.addError('this is an error');
profile.requestData.bodySink.add([4, 5]);
await profile.requestData.close();

expect(requestBodyBytes, [1, 2, 3]);
expect(profile.requestData.bodyBytes, [1, 2, 3]);
expect(requestBodyBytes, [1, 2, 3, 4, 5]);
expect(profile.requestData.bodyBytes, [1, 2, 3, 4, 5]);
});
}
6 changes: 4 additions & 2 deletions pkgs/http_profile/test/http_profile_response_data_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,11 @@ void main() {
expect(profile.responseData.bodyBytes, isEmpty);

profile.responseData.bodySink.add([1, 2, 3]);
profile.responseData.bodySink.addError('this is an error');
profile.responseData.bodySink.add([4, 5]);
await profile.responseData.close();

expect(responseBodyBytes, [1, 2, 3]);
expect(profile.responseData.bodyBytes, [1, 2, 3]);
expect(responseBodyBytes, [1, 2, 3, 4, 5]);
expect(profile.responseData.bodyBytes, [1, 2, 3, 4, 5]);
});
}

0 comments on commit 70cf298

Please sign in to comment.