public Fixture() { HttpMessageHandler.VerifyableSendAsync(Arg.Any <HttpRequestMessage>(), Arg.Any <CancellationToken>()) .Returns(_ => SentryResponses.GetOkResponse()); HttpClient = new HttpClient(HttpMessageHandler); }
public async Task SendEnvelopeAsync_CancellationToken_PassedToClient() { // Arrange using var source = new CancellationTokenSource(); source.Cancel(); var token = source.Token; var httpHandler = Substitute.For <MockableHttpMessageHandler>(); httpHandler.VerifiableSendAsync(Arg.Any <HttpRequestMessage>(), Arg.Any <CancellationToken>()) .Returns(_ => SentryResponses.GetOkResponse()); var httpTransport = new HttpTransport( new SentryOptions { Dsn = DsnSamples.ValidDsnWithSecret }, new HttpClient(httpHandler) ); var envelope = Envelope.FromEvent( new SentryEvent(eventId: SentryResponses.ResponseId) ); #if NET5_0 await Assert.ThrowsAsync <TaskCanceledException>(() => httpTransport.SendEnvelopeAsync(envelope, token)); #else // Act await httpTransport.SendEnvelopeAsync(envelope, token); // Assert await httpHandler .Received(1) .VerifiableSendAsync(Arg.Any <HttpRequestMessage>(), Arg.Is <CancellationToken>(c => c.IsCancellationRequested)); #endif }
public async Task SendEnvelopeAsync_AttachmentTooLarge_DropsItem() { // Arrange using var httpHandler = new FakeHttpClientHandler( _ => SentryResponses.GetOkResponse() ); var logger = new AccumulativeDiagnosticLogger(); var httpTransport = new HttpTransport( new SentryOptions { Dsn = DsnSamples.ValidDsnWithSecret, MaxAttachmentSize = 1, DiagnosticLogger = logger, Debug = true }, new HttpClient(httpHandler) ); var attachmentNormal = new Attachment( AttachmentType.Default, new StreamAttachmentContent(new MemoryStream(new byte[] { 1 })), "test1.txt", null ); var attachmentTooBig = new Attachment( AttachmentType.Default, new StreamAttachmentContent(new MemoryStream(new byte[] { 1, 2, 3, 4, 5 })), "test2.txt", null ); using var envelope = Envelope.FromEvent( new SentryEvent(), new[] { attachmentNormal, attachmentTooBig } ); // Act await httpTransport.SendEnvelopeAsync(envelope); var lastRequest = httpHandler.GetRequests().Last(); var actualEnvelopeSerialized = await lastRequest.Content.ReadAsStringAsync(); // Assert // (the envelope should have only one item) logger.Entries.Should().Contain(e => e.Message == "Attachment '{0}' dropped because it's too large ({1} bytes)." && e.Args[0].ToString() == "test2.txt" && e.Args[1].ToString() == "5" ); actualEnvelopeSerialized.Should().NotContain("test2.txt"); }