Skip to content

Commit

Permalink
test new behaviour in client
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Oct 25, 2022
1 parent e72e124 commit f35689f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
9 changes: 3 additions & 6 deletions dart/lib/src/sentry_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,15 @@ class SentryClient {
preparedEvent = _eventWithRemovedBreadcrumbsIfHandled(preparedEvent);
}

var attachments = scope?.attachments;
if (attachments != null) {
attachments = await _clientAttachmentProcessor.processAttachments(
attachments, preparedEvent);
}
final attachments = await _clientAttachmentProcessor.processAttachments(
scope?.attachments ?? [], preparedEvent);

final envelope = SentryEnvelope.fromEvent(
preparedEvent,
_options.sdk,
dsn: _options.dsn,
traceContext: scope?.span?.traceContext(),
attachments: attachments,
attachments: attachments.isNotEmpty ? attachments : null,
);

final id = await captureEnvelope(envelope);
Expand Down
60 changes: 60 additions & 0 deletions dart/test/sentry_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:collection/collection.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry/src/client_reports/client_report.dart';
import 'package:sentry/src/client_reports/discard_reason.dart';
Expand Down Expand Up @@ -1043,6 +1044,45 @@ void main() {
});
});

group('SentryClientAttachmentProcessor', () {
late Fixture fixture;

setUp(() {
fixture = Fixture();
});

test('processor filtering out attachments', () async {
fixture.options.clientAttachmentProcessor =
MockAttachmentProcessor(MockAttachmentProcessorMode.filter);
final scope = Scope(fixture.options);
scope.addAttachment(SentryAttachment.fromIntList([], "scope-attachment"));
final sut = fixture.getSut();

final event = SentryEvent();
await sut.captureEvent(event, scope: scope);

final capturedEnvelope = (fixture.transport).envelopes.first;
final attachmentItem = capturedEnvelope.items.firstWhereOrNull(
(element) => element.header.type == SentryItemType.attachment);
expect(attachmentItem, null);
});

test('processor adding attachments', () async {
fixture.options.clientAttachmentProcessor =
MockAttachmentProcessor(MockAttachmentProcessorMode.add);
final scope = Scope(fixture.options);
final sut = fixture.getSut();

final event = SentryEvent();
await sut.captureEvent(event, scope: scope);

final capturedEnvelope = (fixture.transport).envelopes.first;
final attachmentItem = capturedEnvelope.items.firstWhereOrNull(
(element) => element.header.type == SentryItemType.attachment);
expect(attachmentItem != null, true);
});
});

group('ClientReportRecorder', () {
late Fixture fixture;

Expand Down Expand Up @@ -1341,3 +1381,23 @@ class Fixture {
loggedException = exception;
}
}

enum MockAttachmentProcessorMode { filter, add }

/// Filtering out all attachments.
class MockAttachmentProcessor implements SentryClientAttachmentProcessor {
MockAttachmentProcessorMode mode;

MockAttachmentProcessor(this.mode);

@override
Future<List<SentryAttachment>> processAttachments(
List<SentryAttachment> attachments, SentryEvent event) async {
switch (mode) {
case MockAttachmentProcessorMode.filter:
return <SentryAttachment>[];
case MockAttachmentProcessorMode.add:
return <SentryAttachment>[SentryAttachment.fromIntList([], "added")];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class ScreenshotAttachmentProcessor implements SentryClientAttachmentProcessor {
@override
Future<List<SentryAttachment>> processAttachments(
List<SentryAttachment> attachments, SentryEvent event) async {
if (event.exceptions == null && event.throwable == null && _attachScreenshot) {
if (event.exceptions == null &&
event.throwable == null &&
_attachScreenshot) {
return attachments;
}
final schedulerBinding = _schedulerBindingProvider();
Expand Down

0 comments on commit f35689f

Please sign in to comment.