Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix getting postUri in SentryClient
1. dsn may specify non-regular port
2. dsn may contains some paths before the projectID
  • Loading branch information
yrom committed Sep 2, 2019
commit 3484a90b824fb06164411d4f8d7d4cc24428c1ef
19 changes: 17 additions & 2 deletions lib/sentry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,23 @@ class SentryClient {
User userContext;

@visibleForTesting
String get postUri =>
'${dsnUri.scheme}://${dsnUri.host}/api/$projectId/store/';
String get postUri {
var port = dsnUri.hasPort &&
((dsnUri.scheme == 'http' && dsnUri.port != 80) ||
(dsnUri.scheme == 'https' && dsnUri.port != 443))
? ':${dsnUri.port}'
: '';
var pathLength = dsnUri.pathSegments.length;
var apiPath;
if (pathLength > 1) {
// some paths would present before the projectID in the dsnUri
apiPath =
(dsnUri.pathSegments.sublist(0, pathLength - 1) + ['api']).join('/');
} else {
apiPath = 'api';
}
return '${dsnUri.scheme}://${dsnUri.host}${port}/$apiPath/$projectId/store/';
}

/// Reports an [event] to Sentry.io.
Future<SentryResponse> capture(
Expand Down
21 changes: 20 additions & 1 deletion test/sentry_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import 'package:test/test.dart';

const String _testDsn = 'https://public:[email protected]/1';
const String _testDsnWithoutSecret = 'https://[email protected]/1';

const String _testDsnWithPath = 'https://public:[email protected]/path/1';
const String _testDsnWithPort = 'https://public:[email protected]:8888/1';
void main() {
group('$SentryClient', () {
test('can parse DSN', () async {
Expand All @@ -34,6 +35,24 @@ void main() {
await client.close();
});

test('can parse DSN with path', () async {
final SentryClient client = new SentryClient(dsn: _testDsnWithPath);
expect(client.dsnUri, Uri.parse(_testDsnWithPath));
expect(client.postUri, 'https://sentry.example.com/path/api/1/store/');
expect(client.publicKey, 'public');
expect(client.secretKey, 'secret');
expect(client.projectId, '1');
await client.close();
});
test('can parse DSN with port', () async {
final SentryClient client = new SentryClient(dsn: _testDsnWithPort);
expect(client.dsnUri, Uri.parse(_testDsnWithPort));
expect(client.postUri, 'https://sentry.example.com:8888/api/1/store/');
expect(client.publicKey, 'public');
expect(client.secretKey, 'secret');
expect(client.projectId, '1');
await client.close();
});
test('sends client auth header without secret', () async {
final MockClient httpMock = new MockClient();
final ClockProvider fakeClockProvider =
Expand Down