@@ -6,11 +6,11 @@ import 'dart:convert';
66import 'dart:io' ;
77
88import 'package:http/http.dart' ;
9- import 'package:quiver/time.dart' ;
109import 'package:sentry/sentry.dart' ;
1110import 'package:test/test.dart' ;
1211
1312const String _testDsn
= 'https://public:[email protected] /1' ;
13+ const String _testDsnWithoutSecret
= 'https://[email protected] /1' ;
1414
1515void main () {
1616 group ('$SentryClient ' , () {
@@ -24,9 +24,75 @@ void main() {
2424 await client.close ();
2525 });
2626
27+ test ('can parse DSN without secret' , () async {
28+ final SentryClient client = new SentryClient (dsn: _testDsnWithoutSecret);
29+ expect (client.dsnUri, Uri .parse (_testDsnWithoutSecret));
30+ expect (client.postUri, 'https://sentry.example.com/api/1/store/' );
31+ expect (client.publicKey, 'public' );
32+ expect (client.secretKey, null );
33+ expect (client.projectId, '1' );
34+ await client.close ();
35+ });
36+
37+ test ('sends client auth header without secret' , () async {
38+ final MockClient httpMock = new MockClient ();
39+ final ClockProvider fakeClockProvider =
40+ () => new DateTime .utc (2017 , 1 , 2 );
41+
42+ Map <String , String > headers;
43+
44+ httpMock.answerWith ((Invocation invocation) async {
45+ if (invocation.memberName == #close) {
46+ return null ;
47+ }
48+ if (invocation.memberName == #post) {
49+ headers = invocation.namedArguments[#headers];
50+ return new Response ('{"id": "test-event-id"}' , 200 );
51+ }
52+ fail ('Unexpected invocation of ${invocation .memberName } in HttpMock' );
53+ });
54+
55+ final SentryClient client = new SentryClient (
56+ dsn: _testDsnWithoutSecret,
57+ httpClient: httpMock,
58+ clock: fakeClockProvider,
59+ compressPayload: false ,
60+ uuidGenerator: () => 'X' * 32 ,
61+ environmentAttributes: const Event (
62+ serverName: 'test.server.com' ,
63+ release: '1.2.3' ,
64+ environment: 'staging' ,
65+ ),
66+ );
67+
68+ try {
69+ throw new ArgumentError ('Test error' );
70+ } catch (error, stackTrace) {
71+ final SentryResponse response = await client.captureException (
72+ exception: error, stackTrace: stackTrace);
73+ expect (response.isSuccessful, true );
74+ expect (response.eventId, 'test-event-id' );
75+ expect (response.error, null );
76+ }
77+
78+ final Map <String , String > expectedHeaders = < String , String > {
79+ 'User-Agent' : '$sdkName /$sdkVersion ' ,
80+ 'Content-Type' : 'application/json' ,
81+ 'X-Sentry-Auth' : 'Sentry sentry_version=6, '
82+ 'sentry_client=${SentryClient .sentryClient }, '
83+ 'sentry_timestamp=${fakeClockProvider ().millisecondsSinceEpoch }, '
84+ 'sentry_key=public' ,
85+ };
86+
87+ expect (headers, expectedHeaders);
88+
89+ await client.close ();
90+ });
91+
2792 testCaptureException (bool compressPayload) async {
2893 final MockClient httpMock = new MockClient ();
29- final Clock fakeClock = new Clock .fixed (new DateTime .utc (2017 , 1 , 2 ));
94+ final ClockProvider fakeClockProvider =
95+ () => new DateTime .utc (2017 , 1 , 2 );
3096
3197 String postUri;
3298 Map <String , String > headers;
@@ -47,7 +113,7 @@ void main() {
47113 final SentryClient client = new SentryClient (
48114 dsn: _testDsn,
49115 httpClient: httpMock,
50- clock: fakeClock ,
116+ clock: fakeClockProvider ,
51117 uuidGenerator: () => 'X' * 32 ,
52118 compressPayload: compressPayload,
53119 environmentAttributes: const Event (
@@ -74,9 +140,7 @@ void main() {
74140 'Content-Type' : 'application/json' ,
75141 'X-Sentry-Auth' : 'Sentry sentry_version=6, '
76142 'sentry_client=${SentryClient .sentryClient }, '
77- 'sentry_timestamp=${fakeClock
78- .now ()
79- .millisecondsSinceEpoch }, '
143+ 'sentry_timestamp=${fakeClockProvider ().millisecondsSinceEpoch }, '
80144 'sentry_key=public, '
81145 'sentry_secret=secret' ,
82146 };
@@ -133,7 +197,8 @@ void main() {
133197
134198 test ('reads error message from the x-sentry-error header' , () async {
135199 final MockClient httpMock = new MockClient ();
136- final Clock fakeClock = new Clock .fixed (new DateTime (2017 , 1 , 2 ));
200+ final ClockProvider fakeClockProvider =
201+ () => new DateTime .utc (2017 , 1 , 2 );
137202
138203 httpMock.answerWith ((Invocation invocation) async {
139204 if (invocation.memberName == #close) {
@@ -150,7 +215,7 @@ void main() {
150215 final SentryClient client = new SentryClient (
151216 dsn: _testDsn,
152217 httpClient: httpMock,
153- clock: fakeClock ,
218+ clock: fakeClockProvider ,
154219 uuidGenerator: () => 'X' * 32 ,
155220 compressPayload: false ,
156221 environmentAttributes: const Event (
@@ -176,7 +241,8 @@ void main() {
176241
177242 test ('$Event userContext overrides client' , () async {
178243 final MockClient httpMock = new MockClient ();
179- final Clock fakeClock = new Clock .fixed (new DateTime (2017 , 1 , 2 ));
244+ final ClockProvider fakeClockProvider =
245+ () => new DateTime .utc (2017 , 1 , 2 );
180246
181247 String loggedUserId; // used to find out what user context was sent
182248 httpMock.answerWith ((Invocation invocation) async {
@@ -211,7 +277,7 @@ void main() {
211277 final SentryClient client = new SentryClient (
212278 dsn: _testDsn,
213279 httpClient: httpMock,
214- clock: fakeClock ,
280+ clock: fakeClockProvider ,
215281 uuidGenerator: () => 'X' * 32 ,
216282 compressPayload: false ,
217283 environmentAttributes: const Event (
0 commit comments