Ejemplo n.º 1
0
        public async Task SendEnvelopeAsync_ResponseNotOkWithJsonMessage_LogsError()
        {
            // Arrange
            const HttpStatusCode expectedCode    = HttpStatusCode.BadGateway;
            const string         expectedMessage = "Bad Gateway!";
            var expectedCauses          = new[] { "invalid file", "wrong arguments" };
            var expectedCausesFormatted = string.Join(", ", expectedCauses);

            var httpHandler = Substitute.For <MockableHttpMessageHandler>();

            httpHandler.VerifiableSendAsync(Arg.Any <HttpRequestMessage>(), Arg.Any <CancellationToken>())
            .Returns(_ => SentryResponses.GetJsonErrorResponse(expectedCode, expectedMessage, expectedCauses));

            var logger = new InMemoryDiagnosticLogger();

            var httpTransport = new HttpTransport(
                new SentryOptions
            {
                Dsn              = DsnSamples.ValidDsnWithSecret,
                Debug            = true,
                DiagnosticLogger = logger
            },
                new HttpClient(httpHandler)
                );

            var envelope = Envelope.FromEvent(new SentryEvent());

            // Act
            await httpTransport.SendEnvelopeAsync(envelope);

            // Assert
            logger.Entries.Any(e =>
                               e.Level == SentryLevel.Error &&
                               e.Message == "Sentry rejected the envelope {0}. Status code: {1}. Error detail: {2}. Error causes: {3}." &&
                               e.Exception == null &&
                               e.Args[0].ToString() == envelope.TryGetEventId().ToString() &&
                               e.Args[1].ToString() == expectedCode.ToString() &&
                               e.Args[2].ToString() == expectedMessage &&
                               e.Args[3].ToString() == expectedCausesFormatted
                               ).Should().BeTrue();
        }
Ejemplo n.º 2
0
        public async Task SendEnvelopeAsync_ResponseRequestEntityTooLargeWithoutPathDefined_DoesNotStoreFile()
        {
            // Arrange
            var httpHandler = Substitute.For <MockableHttpMessageHandler>();

            httpHandler.VerifiableSendAsync(Arg.Any <HttpRequestMessage>(), Arg.Any <CancellationToken>())
            .Returns(_ => SentryResponses.GetJsonErrorResponse(HttpStatusCode.RequestEntityTooLarge, ""));

            var logger = new InMemoryDiagnosticLogger();

            var func = Substitute.For <Func <string, string> >();

            func(Arg.Any <string>()).Returns(null as string);

            var httpTransport = new HttpTransport(
                new SentryOptions
            {
                Dsn              = DsnSamples.ValidDsnWithSecret,
                Debug            = true,
                DiagnosticLogger = logger
            },
                new HttpClient(httpHandler),
                func);

            // Act
            await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent()));

            // Assert
            logger.Entries.Any(e => e.Message == "Environment variable '{0}' set. Writing envelope to {1}")
            .Should()
            .BeFalse();

            logger.Entries.Any(e => e.Message == "Envelope's {0} bytes written to: {1}")
            .Should()
            .BeFalse();
        }
Ejemplo n.º 3
0
        public async Task SendEnvelopeAsync_ResponseRequestEntityTooLargeWithPathDefined_StoresFile()
        {
            // Arrange
            var httpHandler = Substitute.For <MockableHttpMessageHandler>();

            httpHandler.VerifiableSendAsync(Arg.Any <HttpRequestMessage>(), Arg.Any <CancellationToken>())
            .Returns(_ => SentryResponses.GetJsonErrorResponse(HttpStatusCode.RequestEntityTooLarge, ""));

            var logger = new InMemoryDiagnosticLogger();

            var          func           = Substitute.For <Func <string, string> >();
            var          path           = Path.GetTempPath();
            const string expectedEnvVar = "SENTRY_KEEP_LARGE_ENVELOPE_PATH";

            func(expectedEnvVar).Returns(path);

            var httpTransport = new HttpTransport(
                new SentryOptions
            {
                Dsn              = DsnSamples.ValidDsnWithSecret,
                Debug            = true,
                DiagnosticLogger = logger
            },
                new HttpClient(httpHandler),
                func);

            var envelope = Envelope.FromEvent(new SentryEvent());

            // Act
            await httpTransport.SendEnvelopeAsync(envelope);

            // Assert
            logger.Entries.Any(e =>
                               e.Level == SentryLevel.Debug &&
                               e.Message == "Environment variable '{0}' set. Writing envelope to {1}" &&
                               e.Exception == null &&
                               e.Args[0].ToString() == expectedEnvVar &&
                               e.Args[1].ToString() == path)
            .Should()
            .BeTrue();

            var fileStoredLogEntry = logger.Entries.FirstOrDefault(e =>
                                                                   e.Level == SentryLevel.Info &&
                                                                   e.Message == "Envelope's {0} bytes written to: {1}");

            Assert.NotNull(fileStoredLogEntry);
            var expectedFile = new FileInfo(fileStoredLogEntry.Args[1].ToString());

            Assert.True(expectedFile.Exists);
            try
            {
                Assert.Null(fileStoredLogEntry.Exception);
                // // Path is based on the provided path:
                Assert.Contains(path, fileStoredLogEntry.Args[1] as string);
                // // Path contains the envelope id in its name:
                Assert.Contains(envelope.TryGetEventId().ToString(), fileStoredLogEntry.Args[1] as string);
                Assert.Equal(expectedFile.Length, (long)fileStoredLogEntry.Args[0]);
            }
            finally
            {
                // It's in the temp folder but just to keep things tidy:
                expectedFile.Delete();
            }
        }