Skip to content

Commit fa59ae2

Browse files
committed
feat: add cookies to SentryResponse
1 parent 6cc09c5 commit fa59ae2

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

dart/lib/src/protocol/sentry_response.dart

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:meta/meta.dart';
2+
import 'package:collection/collection.dart';
23
import 'contexts.dart';
34

45
/// The response interface contains information on a HTTP request related to the event.
@@ -24,16 +25,26 @@ class SentryResponse {
2425

2526
final Map<String, String>? _headers;
2627

27-
SentryResponse({
28-
this.bodySize,
29-
this.statusCode,
30-
Map<String, String>? headers,
31-
}) : _headers = headers != null ? Map.from(headers) : null;
28+
/// Cookie key-value pairs as string.
29+
final String? cookies;
30+
31+
SentryResponse(
32+
{this.bodySize,
33+
this.statusCode,
34+
Map<String, String>? headers,
35+
String? cookies})
36+
: _headers = headers != null ? Map.from(headers) : null,
37+
// Look for a 'Set-Cookie' header (case insensitive) if not given.
38+
cookies = cookies ??
39+
headers?.entries
40+
.firstWhereOrNull((e) => e.key.toLowerCase() == 'set-cookie')
41+
?.value;
3242

3343
/// Deserializes a [SentryResponse] from JSON [Map].
3444
factory SentryResponse.fromJson(Map<String, dynamic> json) {
3545
return SentryResponse(
3646
headers: json.containsKey('headers') ? Map.from(json['headers']) : null,
47+
cookies: json['cookies'],
3748
bodySize: json['body_size'],
3849
statusCode: json['status_code']);
3950
}
@@ -42,6 +53,7 @@ class SentryResponse {
4253
Map<String, dynamic> toJson() {
4354
return <String, dynamic>{
4455
if (headers.isNotEmpty) 'headers': headers,
56+
if (cookies != null) 'cookies': cookies,
4557
if (bodySize != null) 'body_size': bodySize,
4658
if (statusCode != null) 'status_code': statusCode,
4759
};
@@ -51,16 +63,19 @@ class SentryResponse {
5163
int? statusCode,
5264
int? bodySize,
5365
Map<String, String>? headers,
66+
String? cookies,
5467
}) =>
5568
SentryResponse(
5669
headers: headers ?? _headers,
70+
cookies: cookies ?? this.cookies,
5771
bodySize: bodySize ?? this.bodySize,
5872
statusCode: statusCode ?? this.statusCode,
5973
);
6074

6175
SentryResponse clone() => SentryResponse(
6276
bodySize: bodySize,
6377
headers: headers,
78+
cookies: cookies,
6479
statusCode: statusCode,
6580
);
6681
}

dart/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ dependencies:
1616
stack_trace: ^1.10.0
1717
uuid: ^3.0.0
1818
intl: ^0.17.0
19+
collection: ^1.16.0
1920

2021
dev_dependencies:
2122
mockito: ^5.1.0
2223
lints: ^2.0.0
2324
test: ^1.21.1
2425
yaml: ^3.1.0 # needed for version match (code and pubspec)
25-
collection: ^1.16.0
2626
coverage: ^1.3.0

dart/test/http_client/failed_request_client_test.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ void main() {
8383
test('event reported if bad status code occurs', () async {
8484
final sut = fixture.getSut(
8585
client: fixture.getClient(
86-
statusCode: 404, body: 'foo', headers: {'lorem': 'ipsum'}),
86+
statusCode: 404,
87+
body: 'foo',
88+
headers: {'lorem': 'ipsum', 'set-cookie': 'foo=bar'}),
8789
badStatusCodes: [SentryStatusCode(404)],
8890
);
8991

@@ -122,7 +124,9 @@ void main() {
122124
final response = eventCall.contexts.response!;
123125
expect(response.bodySize, 3);
124126
expect(response.statusCode, 404);
125-
expect(response.headers, equals({'lorem': 'ipsum'}));
127+
expect(response.headers,
128+
equals({'lorem': 'ipsum', 'set-cookie': 'foo=bar'}));
129+
expect(response.cookies, equals('foo=bar'));
126130
});
127131

128132
test(

dart/test/protocol/sentry_response_test.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ void main() {
77
bodySize: 42,
88
statusCode: 200,
99
headers: {'header_key': 'header_value'},
10+
cookies: 'foo=bar, another=cookie',
1011
);
1112

1213
final sentryResponseJson = <String, dynamic>{
1314
'body_size': 42,
1415
'status_code': 200,
1516
'headers': {'header_key': 'header_value'},
17+
'cookies': 'foo=bar, another=cookie',
1618
};
1719

1820
group('json', () {
@@ -41,10 +43,7 @@ void main() {
4143

4244
final copy = data.copyWith();
4345

44-
expect(
45-
DeepCollectionEquality().equals(data.toJson(), copy.toJson()),
46-
true,
47-
);
46+
expect(data.toJson(), copy.toJson());
4847
});
4948

5049
test('copyWith takes new values', () {

dio/test/dio_event_processor_test.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ void main() {
102102
response: Response<dynamic>(
103103
data: 'foobar',
104104
headers: Headers.fromMap(<String, List<String>>{
105-
'foo': ['bar']
105+
'foo': ['bar'],
106+
'set-cookie': ['foo=bar']
106107
}),
107108
requestOptions: request,
108109
isRedirect: true,
@@ -117,7 +118,11 @@ void main() {
117118
expect(processedEvent.contexts.response, isNotNull);
118119
expect(processedEvent.contexts.response?.bodySize, 6);
119120
expect(processedEvent.contexts.response?.statusCode, 200);
120-
expect(processedEvent.contexts.response?.headers, {'foo': 'bar'});
121+
expect(processedEvent.contexts.response?.headers, {
122+
'foo': 'bar',
123+
'set-cookie': 'foo=bar',
124+
});
125+
expect(processedEvent.contexts.response?.cookies, 'foo=bar');
121126
});
122127

123128
test('$DioEventProcessor adds response without PII', () {

0 commit comments

Comments
 (0)