Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
28792c6
ioClient : set a default Client
rxlabz Oct 26, 2020
a5e3fb8
options.compressPayload default value + minor
rxlabz Oct 26, 2020
4b5e8ea
captureMessage as a static method
rxlabz Oct 26, 2020
ee35bad
io example : send a formatted message
rxlabz Oct 26, 2020
51d18de
add a Dart web example
rxlabz Oct 26, 2020
f0ab2b9
changelog
rxlabz Oct 26, 2020
cc4e835
update web example
rxlabz Oct 26, 2020
c43d5bd
Merge remote-tracking branch 'origin/feature/unified-api' into fix/un…
rxlabz Oct 26, 2020
c2f89fa
Merge remote-tracking branch 'origin/feature/unified-api' into fix/un…
rxlabz Oct 26, 2020
27a8d31
add tests ( applyScope, captureMessage )
rxlabz Oct 26, 2020
070bfa5
minor
rxlabz Oct 26, 2020
eaa39e0
more applyScope tests
rxlabz Oct 26, 2020
352a270
refacto sentry client tests
rxlabz Oct 26, 2020
9c5110c
add a client.captureException test
rxlabz Oct 26, 2020
904b7b9
clean
rxlabz Oct 26, 2020
d0728c7
Update dart/examples/web_example/pubspec.yaml
rxlabz Oct 27, 2020
3f6f198
update the web example readme
rxlabz Oct 27, 2020
f5dc698
rename example methods
rxlabz Oct 27, 2020
c2e6b4d
clear the SentryOptions ctor
rxlabz Oct 27, 2020
fa1a8b1
refactor clients and update tests
rxlabz Oct 27, 2020
1f5828d
fix a test
rxlabz Oct 27, 2020
b0de53b
typo
rxlabz Oct 27, 2020
91beccc
clear tests
rxlabz Oct 27, 2020
1e36ab9
update examples
rxlabz Oct 27, 2020
d30a89b
move back example to /example ( pub.dev visibility)
rxlabz Oct 27, 2020
7b618c7
update example events
rxlabz Oct 27, 2020
edc6f35
update example events
rxlabz Oct 27, 2020
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
Prev Previous commit
Next Next commit
refacto sentry client tests
  • Loading branch information
rxlabz committed Oct 26, 2020
commit 352a270076f4c55ad1f75081400a272c26a16874
137 changes: 38 additions & 99 deletions dart/test/sentry_client_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:mockito/mockito.dart';
import 'package:sentry/sentry.dart';
import 'package:test/test.dart';
Expand All @@ -12,28 +8,10 @@ void main() {
group('SentryClient captures message', () {
SentryOptions options;

String formattedMessage;
String template;
List<dynamic> params;

setUp(() {
options = SentryOptions(dsn: fakeDsn);
options.compressPayload = false;
options.httpClient = MockClient((request) async {
if (request.method == 'POST') {
final body = const JsonDecoder()
.convert(Utf8Codec().decode(request.bodyBytes));

formattedMessage = body['message']['formatted'];
template = body['message']['message'];
params = body['message']['params'];

return Response('{"id": "test-event-id"}', 200);
}
fail(
'Unexpected request on ${request.method} ${request.url} in HttpMock',
);
});
options.transport = MockTransport();
});

test('should captures message', () async {
Expand All @@ -44,24 +22,20 @@ void main() {
params: [1],
level: SentryLevel.error,
);
expect(formattedMessage, 'simple message 1');
expect(template, 'simple message %d');
expect(params, [1]);

final capturedEvent = (verify(
options.transport.send(captureAny),
).captured.first) as SentryEvent;

expect(capturedEvent.message.formatted, 'simple message 1');
expect(capturedEvent.message.template, 'simple message %d');
expect(capturedEvent.message.params, [1]);
});
});

group('SentryClient : apply scope to the captured event', () {
SentryOptions options;
String eventLevel;
String eventTransaction;
String eventBreadcrumbMessage;
List<dynamic> eventFingerprint;
String userId;
Scope scope;
String capturedScopeTagValue;
String capturedScopeExtraValue;
String capturedEventTagValue;
String capturedEventExtraValue;

final level = SentryLevel.error;
final transaction = '/test/scope';
Expand All @@ -83,33 +57,11 @@ void main() {
level: SentryLevel.warning,
);

final mockClient = MockClient((request) async {
if (request.method == 'POST') {
final body = const JsonDecoder().convert(
Utf8Codec().decode(request.bodyBytes),
);

eventLevel = body['level'];
eventTransaction = body['transaction'];
eventFingerprint = body['fingerprint'];
userId = body['user']['id'];
eventBreadcrumbMessage = body['breadcrumbs']['values'].first['message'];
capturedScopeTagValue = body['tags'][scopeTagKey];
capturedEventTagValue = body['tags'][eventTagKey];
capturedScopeExtraValue = body['extra'][scopeExtraKey];
capturedEventExtraValue = body['extra'][eventExtraKey];

return Response('{"id": "test-event-id"}', 200);
}
fail(
'Unexpected request on ${request.method} ${request.url} in HttpMock',
);
});

setUp(() {
options = SentryOptions(dsn: fakeDsn);
options.compressPayload = false;
options.httpClient = mockClient;
options.transport = MockTransport();

scope = Scope(options)
..user = user
..level = level
Expand All @@ -124,25 +76,28 @@ void main() {
final client = SentryClient(options);
await client.captureEvent(event, scope: scope);

expect(userId, user.id);
expect(eventLevel, SentryLevel.error.name);
expect(eventTransaction, transaction);
expect(eventFingerprint, fingerprint);
expect(eventBreadcrumbMessage, crumb.message);
expect(capturedScopeTagValue, scopeTagValue);
expect(capturedEventTagValue, eventTagValue);
expect(capturedScopeExtraValue, scopeExtraValue);
expect(capturedEventExtraValue, eventExtraValue);
final capturedEvent = (verify(
options.transport.send(captureAny),
).captured.first) as SentryEvent;

expect(capturedEvent.userContext?.id, user.id);
expect(capturedEvent.level.name, SentryLevel.error.name);
expect(capturedEvent.transaction, transaction);
expect(capturedEvent.fingerprint, fingerprint);
expect(capturedEvent.breadcrumbs.first, crumb);
expect(capturedEvent.tags, {
scopeTagKey: scopeTagValue,
eventTagKey: eventTagValue,
});
expect(capturedEvent.extra, {
scopeExtraKey: scopeExtraValue,
eventExtraKey: eventExtraValue,
});
});
});

group('SentryClient : apply partial scope to the captured event', () {
SentryOptions options;
String capturedLevel;
String capturedTransaction;
String capturedBreadcrumbMessage;
List<dynamic> capturedFingerprint;
String capturedUserId;
Scope scope;

final transaction = '/test/scope';
Expand All @@ -162,30 +117,10 @@ void main() {
breadcrumbs: eventCrumbs,
);

final mockClient = MockClient((request) async {
if (request.method == 'POST') {
final body = const JsonDecoder().convert(
Utf8Codec().decode(request.bodyBytes),
);

capturedLevel = body['level'];
capturedTransaction = body['transaction'];
capturedFingerprint = body['fingerprint'];
capturedUserId = body['user']['id'];
capturedBreadcrumbMessage =
body['breadcrumbs']['values'].first['message'];

return Response('{"id": "test-event-id"}', 200);
}
fail(
'Unexpected request on ${request.method} ${request.url} in HttpMock',
);
});

setUp(() {
options = SentryOptions(dsn: fakeDsn);
options.compressPayload = false;
options.httpClient = mockClient;
options.transport = MockTransport();
scope = Scope(options)
..user = user
..transaction = transaction
Expand All @@ -197,11 +132,15 @@ void main() {
final client = SentryClient(options);
await client.captureEvent(event, scope: scope);

expect(capturedUserId, eventUser.id);
expect(capturedLevel, SentryLevel.warning.name);
expect(capturedTransaction, eventTransaction);
expect(capturedFingerprint, eventFingerprint);
expect(capturedBreadcrumbMessage, eventCrumbs.first.message);
final capturedEvent = (verify(
options.transport.send(captureAny),
).captured.first) as SentryEvent;

expect(capturedEvent.userContext.id, eventUser.id);
expect(capturedEvent.level.name, SentryLevel.warning.name);
expect(capturedEvent.transaction, eventTransaction);
expect(capturedEvent.fingerprint, eventFingerprint);
expect(capturedEvent.breadcrumbs, eventCrumbs);
});
});

Expand Down
156 changes: 156 additions & 0 deletions dart/test/transport_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
group('SentryClient : apply scope to the captured event', () {
SentryOptions options;
String eventLevel;
String eventTransaction;
String eventBreadcrumbMessage;
List<dynamic> eventFingerprint;
String userId;
Scope scope;
String capturedScopeTagValue;
String capturedScopeExtraValue;
String capturedEventTagValue;
String capturedEventExtraValue;

final level = SentryLevel.error;
final transaction = '/test/scope';
final fingerprint = ['foo', 'bar', 'baz'];
final user = User(id: '123', username: 'test');
final crumb = Breadcrumb(message: 'bread');
final scopeTagKey = 'scope-tag';
final scopeTagValue = 'scope-tag-value';
final eventTagKey = 'event-tag';
final eventTagValue = 'event-tag-value';
final scopeExtraKey = 'scope-extra';
final scopeExtraValue = 'scope-extra-value';
final eventExtraKey = 'event-extra';
final eventExtraValue = 'event-extra-value';

final event = SentryEvent(
tags: {eventTagKey: eventTagValue},
extra: {eventExtraKey: eventExtraValue},
level: SentryLevel.warning,
);

final mockClient = MockClient((request) async {
if (request.method == 'POST') {
final body = const JsonDecoder().convert(
Utf8Codec().decode(request.bodyBytes),
);

eventLevel = body['level'];
eventTransaction = body['transaction'];
eventFingerprint = body['fingerprint'];
userId = body['user']['id'];
eventBreadcrumbMessage = body['breadcrumbs']['values'].first['message'];
capturedScopeTagValue = body['tags'][scopeTagKey];
capturedEventTagValue = body['tags'][eventTagKey];
capturedScopeExtraValue = body['extra'][scopeExtraKey];
capturedEventExtraValue = body['extra'][eventExtraKey];

return Response('{"id": "test-event-id"}', 200);
}
fail(
'Unexpected request on ${request.method} ${request.url} in HttpMock',
);
});

setUp(() {
options = SentryOptions(dsn: fakeDsn);
options.compressPayload = false;
options.httpClient = mockClient;
scope = Scope(options)
..user = user
..level = level
..transaction = transaction
..fingerprint = fingerprint
..addBreadcrumb(crumb)
..setTag(scopeTagKey, scopeTagValue)
..setExtra(scopeExtraKey, scopeExtraValue);
});

test('should apply the scope', () async {
final client = SentryClient(options);
await client.captureEvent(event, scope: scope);

expect(userId, user.id);
expect(eventLevel, SentryLevel.error.name);
expect(eventTransaction, transaction);
expect(eventFingerprint, fingerprint);
expect(eventBreadcrumbMessage, crumb.message);
expect(capturedScopeTagValue, scopeTagValue);
expect(capturedEventTagValue, eventTagValue);
expect(capturedScopeExtraValue, scopeExtraValue);
expect(capturedEventExtraValue, eventExtraValue);
});
});

group('SentryClient : apply partial scope to the captured event', () {
SentryOptions options;
String capturedLevel;
String capturedTransaction;
String capturedBreadcrumbMessage;
List<dynamic> capturedFingerprint;
String capturedUserId;
Scope scope;

final transaction = '/test/scope';
final eventTransaction = '/event/transaction';
final fingerprint = ['foo', 'bar', 'baz'];
final eventFingerprint = ['123', '456', '798'];
final user = User(id: '123');
final eventUser = User(id: '987');
final crumb = Breadcrumb(message: 'bread');
final eventCrumbs = [Breadcrumb(message: 'bread')];

final event = SentryEvent(
level: SentryLevel.warning,
transaction: eventTransaction,
userContext: eventUser,
fingerprint: eventFingerprint,
breadcrumbs: eventCrumbs,
);

final mockClient = MockClient((request) async {
if (request.method == 'POST') {
final body = const JsonDecoder().convert(
Utf8Codec().decode(request.bodyBytes),
);

capturedLevel = body['level'];
capturedTransaction = body['transaction'];
capturedFingerprint = body['fingerprint'];
capturedUserId = body['user']['id'];
capturedBreadcrumbMessage =
body['breadcrumbs']['values'].first['message'];

return Response('{"id": "test-event-id"}', 200);
}
fail(
'Unexpected request on ${request.method} ${request.url} in HttpMock',
);
});

setUp(() {
options = SentryOptions(dsn: fakeDsn);
options.compressPayload = false;
options.httpClient = mockClient;
scope = Scope(options)
..user = user
..transaction = transaction
..fingerprint = fingerprint
..addBreadcrumb(crumb);
});

test('should not apply the scope to non null event fields ', () async {
final client = SentryClient(options);
await client.captureEvent(event, scope: scope);

expect(capturedUserId, eventUser.id);
expect(capturedLevel, SentryLevel.warning.name);
expect(capturedTransaction, eventTransaction);
expect(capturedFingerprint, eventFingerprint);
expect(capturedBreadcrumbMessage, eventCrumbs.first.message);
});
});
*/