Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DS_Store
.atom/
.packages
.pub/
.dart_tool/
build/
packages
pubspec.lock
Expand Down
13 changes: 6 additions & 7 deletions lib/sentry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ class SentryClient {
@required this.secretKey,
@required this.compressPayload,
@required this.projectId,
})
: _httpClient = httpClient,
}) : _httpClient = httpClient,
_clock = clock,
_uuidGenerator = uuidGenerator;

Expand Down Expand Up @@ -161,19 +160,19 @@ class SentryClient {
'sentry_secret=$secretKey',
};

final Map<String, dynamic> json = <String, dynamic>{
final Map<String, dynamic> data = <String, dynamic>{
'project': projectId,
'event_id': _uuidGenerator(),
'timestamp': formatDateAsIso8601WithSecondPrecision(_clock.now()),
'logger': defaultLoggerName,
};

if (environmentAttributes != null)
mergeAttributes(environmentAttributes.toJson(), into: json);
mergeAttributes(environmentAttributes.toJson(), into: data);

mergeAttributes(event.toJson(), into: json);
mergeAttributes(event.toJson(), into: data);

List<int> body = UTF8.encode(JSON.encode(json));
List<int> body = utf8.encode(json.encode(data));
if (compressPayload) {
headers['Content-Encoding'] = 'gzip';
body = GZIP.encode(body);
Expand All @@ -190,7 +189,7 @@ class SentryClient {
return new SentryResponse.failure(errorMessage);
}

final String eventId = JSON.decode(response.body)['id'];
final String eventId = json.decode(response.body)['id'];
return new SentryResponse.success(eventId: eventId);
}

Expand Down
32 changes: 21 additions & 11 deletions test/sentry_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'dart:convert';
import 'dart:io';

import 'package:http/http.dart';
import 'package:mockito/mockito.dart';
import 'package:quiver/time.dart';
import 'package:sentry/sentry.dart';
import 'package:test/test.dart';
Expand All @@ -32,8 +31,8 @@ void main() {
String postUri;
Map<String, String> headers;
List<int> body;
when(httpMock.post(any, headers: any, body: any))
.thenAnswer((Invocation invocation) {
httpMock.answerWith((Invocation invocation) {
if (invocation.memberName == #close) return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we assert after this line that memberName is #post? Also, let's use { } for the if block so that the return statement gets its own line.

postUri = invocation.positionalArguments.single;
headers = invocation.namedArguments[#headers];
body = invocation.namedArguments[#body];
Expand Down Expand Up @@ -79,13 +78,13 @@ void main() {

expect(headers, expectedHeaders);

Map<String, dynamic> json;
Map<String, dynamic> data;
if (compressPayload) {
json = JSON.decode(UTF8.decode(GZIP.decode(body)));
data = json.decode(utf8.decode(GZIP.decode(body)));
} else {
json = JSON.decode(UTF8.decode(body));
data = json.decode(utf8.decode(body));
}
final Map<String, dynamic> stacktrace = json.remove('stacktrace');
final Map<String, dynamic> stacktrace = data.remove('stacktrace');
expect(stacktrace['frames'], const isInstanceOf<List>());
expect(stacktrace['frames'], isNotEmpty);

Expand All @@ -98,7 +97,7 @@ void main() {
expect(topFrame['in_app'], true);
expect(topFrame['filename'], 'sentry_test.dart');

expect(json, {
expect(data, {
'project': '1',
'event_id': 'X' * 32,
'timestamp': '2017-01-02T00:00:00',
Expand Down Expand Up @@ -128,8 +127,7 @@ void main() {
final MockClient httpMock = new MockClient();
final Clock fakeClock = new Clock.fixed(new DateTime(2017, 1, 2));

when(httpMock.post(any, headers: any, body: any))
.thenAnswer((Invocation invocation) {
httpMock.answerWith((Invocation invocation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here we can also assert that only post is being called.

return new Response('', 401, headers: <String, String>{
'x-sentry-error': 'Invalid api key',
});
Expand Down Expand Up @@ -199,4 +197,16 @@ void main() {
});
}

class MockClient extends Mock implements Client {}
typedef Answer = dynamic Function(Invocation invocation);

class MockClient implements Client {
Answer _answer;

void answerWith(Answer answer) {
_answer = answer;
}

noSuchMethod(Invocation invocation) {
return _answer(invocation);
}
}
6 changes: 6 additions & 0 deletions tool/dart2_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
# Temporary workaround until Pub supports --preview-dart-2 flag
set -e
for filename in test/*_test.dart; do
dart --preview-dart-2 --enable_asserts "$filename"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's echo the name of the file we're running. Alternatively, set -x should have roughly the same effect.

done
1 change: 1 addition & 0 deletions tool/presubmit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ set -x
pub get
dartanalyzer --strong --fatal-warnings ./
pub run test --platform vm
./tool/dart2_test.sh
dartfmt -n --set-exit-if-changed ./