1
1
import 'dart:convert' ;
2
2
3
3
import 'package:sentry/sentry.dart' ;
4
- import 'no_such_method_provider .dart' ;
4
+ import 'package:test/expect .dart' ;
5
5
6
- class MockTransport with NoSuchMethodProvider implements Transport {
6
+ class MockTransport implements Transport {
7
7
List <SentryEnvelope > envelopes = [];
8
8
List <SentryEvent > events = [];
9
- int calls = 0 ;
9
+
10
+ int _calls = 0 ;
11
+ String _exceptions = '' ;
12
+
13
+ int get calls {
14
+ expect (_exceptions, isEmpty);
15
+ return _calls;
16
+ }
10
17
11
18
bool called (int calls) {
12
19
return calls == calls;
13
20
}
14
21
15
22
@override
16
23
Future <SentryId > send (SentryEnvelope envelope) async {
17
- calls++ ;
18
-
19
- envelopes.add (envelope);
20
- final event = await _eventFromEnvelope (envelope);
21
- events.add (event);
24
+ _calls++ ;
25
+
26
+ // Exception here would be swallowed by Sentry, making it hard to find test
27
+ // failure causes. Instead, we log them and check on access to [calls].
28
+ try {
29
+ envelopes.add (envelope);
30
+ final event = await _eventFromEnvelope (envelope);
31
+ events.add (event);
32
+ } catch (e, stack) {
33
+ _exceptions += '$e \n $stack \n\n ' ;
34
+ rethrow ;
35
+ }
22
36
23
37
return envelope.header.eventId ?? SentryId .empty ();
24
38
}
@@ -28,36 +42,14 @@ class MockTransport with NoSuchMethodProvider implements Transport {
28
42
envelopeItemData.addAll (await envelope.items.first.envelopeItemStream ());
29
43
30
44
final envelopeItem = utf8.decode (envelopeItemData);
31
- final dynamic envelopeItemJson = jsonDecode (envelopeItem.split ('\n ' ).last);
32
- final envelopeMap = envelopeItemJson as Map <String , dynamic >;
33
- final requestJson = envelopeMap['request' ] as Map <String , dynamic >? ;
34
-
35
- // '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>'
36
- final headersMap = requestJson? ['headers' ] as Map <String , dynamic >? ;
37
- final newHeadersMap = < String , String > {};
38
- if (headersMap != null ) {
39
- for (final entry in headersMap.entries) {
40
- newHeadersMap[entry.key] = entry.value as String ;
41
- }
42
- envelopeMap['request' ]['headers' ] = newHeadersMap;
43
- }
44
-
45
- final otherMap = requestJson? ['other' ] as Map <String , dynamic >? ;
46
- final newOtherMap = < String , String > {};
47
- if (otherMap != null ) {
48
- for (final entry in otherMap.entries) {
49
- newOtherMap[entry.key] = entry.value as String ;
50
- }
51
- envelopeMap['request' ]['other' ] = newOtherMap;
52
- }
53
-
54
- return SentryEvent .fromJson (envelopeMap);
45
+ final envelopeItemJson = jsonDecode (envelopeItem.split ('\n ' ).last);
46
+ return SentryEvent .fromJson (envelopeItemJson);
55
47
}
56
48
57
49
void reset () {
58
50
envelopes.clear ();
59
51
events.clear ();
60
- calls = 0 ;
52
+ _calls = 0 ;
61
53
}
62
54
}
63
55
0 commit comments