|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:convert'; |
| 3 | + |
1 | 4 | import 'package:flutter/widgets.dart';
|
2 | 5 | import 'package:flutter_test/flutter_test.dart';
|
3 | 6 | import 'package:sentry_flutter/sentry_flutter.dart';
|
4 | 7 | import 'package:sentry_flutter_example/main.dart';
|
| 8 | +import 'package:http/http.dart'; |
5 | 9 |
|
6 | 10 | void main() {
|
| 11 | + const org = 'sentry-sdks'; |
| 12 | + const slug = 'sentry-flutter'; |
| 13 | + const authToken = String.fromEnvironment('SENTRY_AUTH_TOKEN'); |
| 14 | + const fakeDsn = 'https://[email protected]/1234567'; |
| 15 | + |
7 | 16 | TestWidgetsFlutterBinding.ensureInitialized();
|
8 | 17 |
|
9 | 18 | tearDown(() async {
|
10 | 19 | await Sentry.close();
|
11 | 20 | });
|
12 | 21 |
|
13 | 22 | // Using fake DSN for testing purposes.
|
14 |
| - Future<void> setupSentryAndApp(WidgetTester tester) async { |
15 |
| - await setupSentry(() async { |
16 |
| - await tester.pumpWidget(SentryScreenshotWidget( |
17 |
| - child: DefaultAssetBundle( |
18 |
| - bundle: SentryAssetBundle(enableStructuredDataTracing: true), |
19 |
| - child: const MyApp(), |
20 |
| - ))); |
21 |
| - }, 'https://[email protected]/1234567'); |
| 23 | + Future<void> setupSentryAndApp(WidgetTester tester, |
| 24 | + {String? dsn, BeforeSendCallback? beforeSendCallback}) async { |
| 25 | + await setupSentry( |
| 26 | + () async { |
| 27 | + await tester.pumpWidget(SentryScreenshotWidget( |
| 28 | + child: DefaultAssetBundle( |
| 29 | + bundle: SentryAssetBundle(enableStructuredDataTracing: true), |
| 30 | + child: const MyApp(), |
| 31 | + ))); |
| 32 | + }, |
| 33 | + dsn ?? fakeDsn, |
| 34 | + isIntegrationTest: true, |
| 35 | + beforeSendCallback: beforeSendCallback, |
| 36 | + ); |
22 | 37 | }
|
23 | 38 |
|
24 | 39 | // Tests
|
@@ -123,4 +138,96 @@ void main() {
|
123 | 138 | final transaction = Sentry.startTransactionWithContext(context);
|
124 | 139 | await transaction.finish();
|
125 | 140 | });
|
| 141 | + |
| 142 | + group('e2e', () { |
| 143 | + var output = find.byKey(const Key('output')); |
| 144 | + late Fixture fixture; |
| 145 | + |
| 146 | + setUp(() { |
| 147 | + fixture = Fixture(); |
| 148 | + }); |
| 149 | + |
| 150 | + testWidgets('captureException', (tester) async { |
| 151 | + await setupSentryAndApp(tester, |
| 152 | + dsn: exampleDsn, beforeSendCallback: fixture.beforeSend); |
| 153 | + |
| 154 | + await tester.tap(find.text('captureException')); |
| 155 | + await tester.pumpAndSettle(); |
| 156 | + |
| 157 | + final text = output.evaluate().single.widget as Text; |
| 158 | + final id = text.data!; |
| 159 | + |
| 160 | + final uri = Uri.parse( |
| 161 | + 'https://sentry.io/api/0/projects/$org/$slug/events/$id/', |
| 162 | + ); |
| 163 | + |
| 164 | + final event = await fixture.poll(uri, authToken); |
| 165 | + expect(event, isNotNull); |
| 166 | + |
| 167 | + final sentEvent = fixture.sentEvent; |
| 168 | + expect(sentEvent, isNotNull); |
| 169 | + |
| 170 | + final tags = event!["tags"] as List<dynamic>; |
| 171 | + |
| 172 | + expect(sentEvent!.eventId.toString(), event["id"]); |
| 173 | + expect("_Exception: Exception: captureException", event["title"]); |
| 174 | + expect(sentEvent.release, event["release"]["version"]); |
| 175 | + expect( |
| 176 | + 2, |
| 177 | + (tags.firstWhere((e) => e["value"] == sentEvent.environment) as Map) |
| 178 | + .length); |
| 179 | + expect(sentEvent.fingerprint, event["fingerprint"] ?? []); |
| 180 | + expect( |
| 181 | + 2, |
| 182 | + (tags.firstWhere((e) => e["value"] == SentryLevel.error.name) as Map) |
| 183 | + .length); |
| 184 | + expect(sentEvent.logger, event["logger"]); |
| 185 | + |
| 186 | + final dist = tags.firstWhere((element) => element['key'] == 'dist'); |
| 187 | + expect('1', dist['value']); |
| 188 | + |
| 189 | + final environment = |
| 190 | + tags.firstWhere((element) => element['key'] == 'environment'); |
| 191 | + expect('integration', environment['value']); |
| 192 | + }); |
| 193 | + }); |
| 194 | +} |
| 195 | + |
| 196 | +class Fixture { |
| 197 | + SentryEvent? sentEvent; |
| 198 | + |
| 199 | + FutureOr<SentryEvent?> beforeSend(SentryEvent event, {Hint? hint}) async { |
| 200 | + sentEvent = event; |
| 201 | + return event; |
| 202 | + } |
| 203 | + |
| 204 | + Future<Map<String, dynamic>?> poll(Uri url, String authToken) async { |
| 205 | + final client = Client(); |
| 206 | + |
| 207 | + const maxRetries = 10; |
| 208 | + const initialDelay = Duration(seconds: 2); |
| 209 | + const factor = 2; |
| 210 | + |
| 211 | + var retries = 0; |
| 212 | + var delay = initialDelay; |
| 213 | + |
| 214 | + while (retries < maxRetries) { |
| 215 | + try { |
| 216 | + final response = await client.get( |
| 217 | + url, |
| 218 | + headers: <String, String>{'Authorization': 'Bearer $authToken'}, |
| 219 | + ); |
| 220 | + if (response.statusCode == 200) { |
| 221 | + return jsonDecode(utf8.decode(response.bodyBytes)); |
| 222 | + } |
| 223 | + } catch (e) { |
| 224 | + // Do nothing |
| 225 | + } finally { |
| 226 | + retries++; |
| 227 | + await Future.delayed(delay); |
| 228 | + delay *= factor; |
| 229 | + } |
| 230 | + } |
| 231 | + return null; |
| 232 | + } |
126 | 233 | }
|
0 commit comments