Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update RequestDecompressionMiddlewareTests.cs
  • Loading branch information
jamescrosswell committed Jul 4, 2025
commit 19b5b33a85752ddb74cd7db1466654dc62e4bd01
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ private class Fixture : IDisposable
private TestServer _server;
private HttpClient _client;
private IRequestDecompressionProvider _provider;
public Action<SentryAspNetCoreOptions> ConfigureOptions;
public Exception LastException { get; private set; }

private IWebHostBuilder GetBuilder()
{
var exceptionProcessor = Substitute.For<ISentryEventExceptionProcessor>();
exceptionProcessor.Process(Arg.Do<Exception>(e => LastException = e),
Arg.Any<SentryEvent>());

var sentry = FakeSentryServer.CreateServer();
var sentryHttpClient = sentry.CreateClient();
return new WebHostBuilder()
.ConfigureServices(services =>
{
Expand All @@ -36,10 +42,8 @@ private IWebHostBuilder GetBuilder()
{
o.Dsn = ValidDsn;
o.MaxRequestBodySize = RequestSize.Always;
if (ConfigureOptions is not null)
{
ConfigureOptions(o);
}
o.SentryHttpClientFactory = new DelegateHttpClientFactory(_ => sentryHttpClient);
o.AddExceptionProcessor(exceptionProcessor);
})
.Configure(app =>
{
Expand Down Expand Up @@ -123,19 +127,6 @@ public async Task AddRequestDecompression_CompressedBodyContent_IsDecompressed()
public async Task DecompressionError_SentryCapturesException()
{
// Arrange
SentryEvent exceptionEvent = null;
var exceptionProcessor = Substitute.For<ISentryEventExceptionProcessor>();
exceptionProcessor.Process(Arg.Any<Exception>(), Arg.Do<SentryEvent>(
evt => exceptionEvent = evt
));

var sentry = FakeSentryServer.CreateServer();
var sentryHttpClient = sentry.CreateClient();
_fixture.ConfigureOptions = options =>
{
options.SentryHttpClientFactory = new DelegateHttpClientFactory(_ => sentryHttpClient);
options.AddExceptionProcessor(exceptionProcessor);
};
_fixture.FakeDecompressionError();
var client = _fixture.GetSut();

Expand All @@ -148,26 +139,18 @@ public async Task DecompressionError_SentryCapturesException()
// Act
try
{
var _ = await client.PostAsync("/echo", content);
_ = await client.PostAsync("/echo", content);
}
catch
{
// We're expecting an exception here... what we're interested in is what happens on the server
}

// Assert
exceptionEvent.Should().NotBeNull();
using (new AssertionScope())
{
exceptionEvent.Tags.Should().Contain(kvp =>
kvp.Key == "RequestPath" &&
kvp.Value == "/echo"
);
exceptionEvent.Exception.Should().NotBeNull();
if (exceptionEvent.Exception is not null)
{
exceptionEvent.Exception.Message.Should().Be("Flaky decompression error");
}
_fixture.LastException.Should().NotBeNull();
_fixture.LastException?.Message.Should().Be("Flaky decompression error");
}
}

Expand All @@ -181,7 +164,4 @@ private static byte[] CompressGzip(string str)
}
return output.ToArray();
}

// Dummy DSN for Sentry SDK initialization in tests
private const string ValidDsn = "https://[email protected]/1";
}
Loading