Skip to content

Commit

Permalink
chore: update mock_transport.dart copy in the DIO integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Nov 4, 2022
1 parent 110b200 commit bf7a456
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
2 changes: 1 addition & 1 deletion dart/test/mocks/mock_transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MockTransport implements Transport {
final event = await _eventFromEnvelope(envelope);
events.add(event);
} catch (e, stack) {
_exceptions += "$e\n$stack\n\n";
_exceptions += '$e\n$stack\n\n';
rethrow;
}

Expand Down
58 changes: 25 additions & 33 deletions dio/test/mocks/mock_transport.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import 'dart:convert';

import 'package:sentry/sentry.dart';
import 'no_such_method_provider.dart';
import 'package:test/expect.dart';

class MockTransport with NoSuchMethodProvider implements Transport {
class MockTransport implements Transport {
List<SentryEnvelope> envelopes = [];
List<SentryEvent> events = [];
int calls = 0;

int _calls = 0;
String _exceptions = '';

int get calls {
expect(_exceptions, isEmpty);
return _calls;
}

bool called(int calls) {
return calls == calls;
}

@override
Future<SentryId> send(SentryEnvelope envelope) async {
calls++;

envelopes.add(envelope);
final event = await _eventFromEnvelope(envelope);
events.add(event);
_calls++;

// Exception here would be swallowed by Sentry, making it hard to find test
// failure causes. Instead, we log them and check on access to [calls].
try {
envelopes.add(envelope);
final event = await _eventFromEnvelope(envelope);
events.add(event);
} catch (e, stack) {
_exceptions += '$e\n$stack\n\n';
rethrow;
}

return envelope.header.eventId ?? SentryId.empty();
}
Expand All @@ -28,36 +42,14 @@ class MockTransport with NoSuchMethodProvider implements Transport {
envelopeItemData.addAll(await envelope.items.first.envelopeItemStream());

final envelopeItem = utf8.decode(envelopeItemData);
final dynamic envelopeItemJson = jsonDecode(envelopeItem.split('\n').last);
final envelopeMap = envelopeItemJson as Map<String, dynamic>;
final requestJson = envelopeMap['request'] as Map<String, dynamic>?;

// '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>'
final headersMap = requestJson?['headers'] as Map<String, dynamic>?;
final newHeadersMap = <String, String>{};
if (headersMap != null) {
for (final entry in headersMap.entries) {
newHeadersMap[entry.key] = entry.value as String;
}
envelopeMap['request']['headers'] = newHeadersMap;
}

final otherMap = requestJson?['other'] as Map<String, dynamic>?;
final newOtherMap = <String, String>{};
if (otherMap != null) {
for (final entry in otherMap.entries) {
newOtherMap[entry.key] = entry.value as String;
}
envelopeMap['request']['other'] = newOtherMap;
}

return SentryEvent.fromJson(envelopeMap);
final envelopeItemJson = jsonDecode(envelopeItem.split('\n').last);
return SentryEvent.fromJson(envelopeItemJson);
}

void reset() {
envelopes.clear();
events.clear();
calls = 0;
_calls = 0;
}
}

Expand Down

0 comments on commit bf7a456

Please sign in to comment.