-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
tracing_client_adapter_test.dart
193 lines (157 loc) · 5.02 KB
/
tracing_client_adapter_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// ignore_for_file: invalid_use_of_internal_member
// The lint above is okay, because we're using another Sentry package
import 'package:dio/dio.dart';
import 'package:http/http.dart';
import 'package:mockito/mockito.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry/src/sentry_tracer.dart';
import 'package:sentry_dio/src/tracing_client_adapter.dart';
import 'package:test/test.dart';
import 'mocks.dart';
import 'mocks/mock_http_client_adapter.dart';
import 'mocks/mock_transport.dart';
final requestUri = Uri.parse('https://example.com?foo=bar');
final requestOptions = '?foo=bar';
void main() {
group(TracingClientAdapter, () {
late Fixture fixture;
setUp(() {
fixture = Fixture();
});
test('captured span if successful request', () async {
final sut = fixture.getSut(
client: fixture.getClient(statusCode: 200, reason: 'OK'),
);
final tr = fixture._hub.startTransaction(
'name',
'op',
bindToScope: true,
);
await sut.get<dynamic>(requestOptions);
await tr.finish();
final tracer = (tr as SentryTracer);
final span = tracer.children.first;
expect(span.status, SpanStatus.ok());
expect(span.context.operation, 'http.client');
expect(span.context.description, 'GET https://example.com?foo=bar');
});
test('finish span if errored request', () async {
final sut = fixture.getSut(
client: createThrowingClient(),
);
final tr = fixture._hub.startTransaction(
'name',
'op',
bindToScope: true,
);
try {
await sut.get<dynamic>(requestOptions);
} catch (_) {
// ignore
}
await tr.finish();
final tracer = (tr as SentryTracer);
final span = tracer.children.first;
expect(span.finished, isTrue);
});
test('associate exception to span if errored request', () async {
final sut = fixture.getSut(
client: createThrowingClient(),
);
final tr = fixture._hub.startTransaction(
'name',
'op',
bindToScope: true,
);
dynamic exception;
try {
await sut.get<dynamic>(requestOptions);
} catch (error) {
exception = error;
}
await tr.finish();
final tracer = (tr as SentryTracer);
final span = tracer.children.first;
expect(span.status, SpanStatus.internalError());
expect(span.throwable, isA<TestException>());
expect(exception, isA<DioError>());
expect((exception as DioError).error, isA<TestException>());
});
test('captured span adds sentry-trace header to the request', () async {
final sut = fixture.getSut(
client: fixture.getClient(statusCode: 200, reason: 'OK'),
);
final tr = fixture._hub.startTransaction(
'name',
'op',
bindToScope: true,
);
final response = await sut.get<dynamic>(requestOptions);
await tr.finish();
final tracer = (tr as SentryTracer);
final span = tracer.children.first;
expect(
response.headers['sentry-trace'],
<String>[span.toSentryTrace().value],
);
});
test('captured span do not add headers if NoOp', () async {
final sut = fixture.getSut(
client: fixture.getClient(statusCode: 200, reason: 'OK'),
);
await fixture._hub
.configureScope((scope) => scope.span = NoOpSentrySpan());
final response = await sut.get<dynamic>(requestOptions);
expect(response.headers['baggage'], null);
expect(response.headers['sentry-trace'], null);
});
test('do not throw if no span bound to the scope', () async {
final sut = fixture.getSut(
client: fixture.getClient(statusCode: 200, reason: 'OK'),
);
await sut.get<dynamic>(requestOptions);
});
});
}
MockHttpClientAdapter createThrowingClient() {
return MockHttpClientAdapter(
(options, _, __) async {
expect(options.uri, requestUri);
throw TestException();
},
);
}
class CloseableMockClient extends Mock implements BaseClient {}
class Fixture {
final _options = SentryOptions(dsn: fakeDsn);
late Hub _hub;
final transport = MockTransport();
Fixture() {
_options.transport = transport;
_options.tracesSampleRate = 1.0;
_hub = Hub(_options);
}
Dio getSut({MockHttpClientAdapter? client}) {
final mc = client ?? getClient();
final dio = Dio(BaseOptions(baseUrl: 'https://example.com'));
dio.httpClientAdapter = TracingClientAdapter(
client: mc,
hub: _hub,
);
return dio;
}
MockHttpClientAdapter getClient({int statusCode = 200, String? reason}) {
return MockHttpClientAdapter((options, requestStream, cancelFuture) async {
expect(options.uri, requestUri);
return ResponseBody.fromString(
'',
statusCode,
headers: options.headers.map(
(key, dynamic value) =>
MapEntry(key, <String>[value?.toString() ?? '']),
),
);
});
}
}
class TestException implements Exception {}