|
1 | 1 | // ignore_for_file: deprecated_member_use |
2 | 2 |
|
| 3 | +import 'dart:typed_data'; |
| 4 | + |
3 | 5 | import 'package:dio/dio.dart'; |
4 | 6 | import 'package:mockito/mockito.dart'; |
5 | 7 | import 'package:sentry/sentry.dart'; |
@@ -148,6 +150,39 @@ void main() { |
148 | 150 | expect(breadcrumb.data?['duration'], isNotNull); |
149 | 151 | }); |
150 | 152 |
|
| 153 | + test('breadcrumb gets added when DioException with response is thrown', |
| 154 | + () async { |
| 155 | + final sut = fixture.getSut( |
| 156 | + DioExceptionWithResponseAdapter( |
| 157 | + statusCode: 404, |
| 158 | + statusMessage: 'Not Found', |
| 159 | + headers: { |
| 160 | + 'content-length': ['123'], |
| 161 | + }, |
| 162 | + ), |
| 163 | + ); |
| 164 | + |
| 165 | + try { |
| 166 | + await sut.get<dynamic>(''); |
| 167 | + fail('Method did not throw'); |
| 168 | + } on DioException catch (_) {} |
| 169 | + |
| 170 | + expect(fixture.hub.addBreadcrumbCalls.length, 1); |
| 171 | + |
| 172 | + final breadcrumb = fixture.hub.addBreadcrumbCalls.first.crumb; |
| 173 | + |
| 174 | + expect(breadcrumb.type, 'http'); |
| 175 | + expect(breadcrumb.data?['url'], 'https://example.com'); |
| 176 | + expect(breadcrumb.data?['method'], 'GET'); |
| 177 | + expect(breadcrumb.data?['http.query'], 'foo=bar'); |
| 178 | + expect(breadcrumb.data?['http.fragment'], 'baz'); |
| 179 | + expect(breadcrumb.level, SentryLevel.error); |
| 180 | + expect(breadcrumb.data?['duration'], isNotNull); |
| 181 | + expect(breadcrumb.data?['status_code'], 404); |
| 182 | + expect(breadcrumb.data?['reason'], 'Not Found'); |
| 183 | + expect(breadcrumb.data?['response_body_size'], 123); |
| 184 | + }); |
| 185 | + |
151 | 186 | test('close does get called for user defined client', () async { |
152 | 187 | final mockHub = MockHub(); |
153 | 188 |
|
@@ -185,8 +220,45 @@ void main() { |
185 | 220 |
|
186 | 221 | class CloseableMockClientAdapter extends Mock implements HttpClientAdapter {} |
187 | 222 |
|
| 223 | +class DioExceptionWithResponseAdapter implements HttpClientAdapter { |
| 224 | + DioExceptionWithResponseAdapter({ |
| 225 | + required this.statusCode, |
| 226 | + required this.statusMessage, |
| 227 | + required this.headers, |
| 228 | + }); |
| 229 | + |
| 230 | + final int statusCode; |
| 231 | + final String statusMessage; |
| 232 | + final Map<String, List<String>> headers; |
| 233 | + |
| 234 | + @override |
| 235 | + Future<ResponseBody> fetch( |
| 236 | + RequestOptions options, |
| 237 | + Stream<Uint8List>? requestStream, |
| 238 | + Future<dynamic>? cancelFuture, |
| 239 | + ) async { |
| 240 | + final response = Response<dynamic>( |
| 241 | + requestOptions: options, |
| 242 | + statusCode: statusCode, |
| 243 | + statusMessage: statusMessage, |
| 244 | + headers: Headers.fromMap(headers), |
| 245 | + ); |
| 246 | + |
| 247 | + throw DioException.badResponse( |
| 248 | + requestOptions: options, |
| 249 | + response: response, |
| 250 | + statusCode: statusCode, |
| 251 | + ); |
| 252 | + } |
| 253 | + |
| 254 | + @override |
| 255 | + void close({bool force = false}) { |
| 256 | + // No-op for testing |
| 257 | + } |
| 258 | +} |
| 259 | + |
188 | 260 | class Fixture { |
189 | | - Dio getSut([MockHttpClientAdapter? client]) { |
| 261 | + Dio getSut([HttpClientAdapter? client]) { |
190 | 262 | final mc = client ?? getClient(); |
191 | 263 | final dio = Dio( |
192 | 264 | BaseOptions(baseUrl: 'https://example.com?foo=bar#baz'), |
|
0 commit comments