Skip to content
Merged
Show file tree
Hide file tree
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
Decompression error test
  • Loading branch information
jamescrosswell committed Jul 4, 2025
commit 073c1713b0db185e66293ffbb4dbe452162feed8
2 changes: 1 addition & 1 deletion test/Sentry.AspNetCore.TestUtils/FakeSentryServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Sentry.AspNetCore.TestUtils;

internal static class FakeSentryServer
public static class FakeSentryServer
{
public static TestServer CreateServer(IReadOnlyCollection<RequestHandler> handlers)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
using System.IO.Compression;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.RequestDecompression;
using Xunit;
using Sentry.AspNetCore.TestUtils;

namespace Sentry.AspNetCore.Tests.RequestDecompressionMiddleware;

Expand All @@ -20,15 +14,18 @@ private class Fixture : IDisposable
{
private TestServer _server;
private HttpClient _client;
private IRequestDecompressionProvider provider;
private IRequestDecompressionProvider _provider;
public Action<SentryAspNetCoreOptions> ConfigureOptions;

private IWebHostBuilder Builder => new WebHostBuilder()
private IWebHostBuilder GetBuilder()
{
return new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddRouting();
if (provider is not null)
if (_provider is not null)
{
services.AddSingleton(provider);
services.AddSingleton(_provider);
}
else
{
Expand All @@ -39,6 +36,10 @@ private class Fixture : IDisposable
{
o.Dsn = ValidDsn;
o.MaxRequestBodySize = RequestSize.Always;
if (ConfigureOptions is not null)
{
ConfigureOptions(o);
}
})
.Configure(app =>
{
Expand All @@ -53,13 +54,14 @@ private class Fixture : IDisposable
});
});
});
}

public void FakeDecompressionError()
{
provider = new FlakyDecompressionProvider();
_provider = new FlakyDecompressionProvider();
}

class FlakyDecompressionProvider : IRequestDecompressionProvider
private class FlakyDecompressionProvider : IRequestDecompressionProvider
{
public Stream GetDecompressionStream(HttpContext context)
{
Expand All @@ -70,7 +72,7 @@ public Stream GetDecompressionStream(HttpContext context)

public HttpClient GetSut()
{
_server = new TestServer(Builder);
_server = new TestServer(GetBuilder());
_client = _server.CreateClient();
return _client;
}
Expand All @@ -82,7 +84,22 @@ public void Dispose()
}
}

private readonly Fixture _fixture = new Fixture();
private readonly Fixture _fixture = new ();

[Fact]
public async Task AddRequestDecompression_PlainBodyContent_IsUnaltered()
{
var client = _fixture.GetSut();

var json = "{\"Foo\":\"Bar\"}";
var content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await client.PostAsync("/echo", content);
var responseBody = await response.Content.ReadAsStringAsync();

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(json, responseBody);
}

[Fact]
public async Task AddRequestDecompression_CompressedBodyContent_IsDecompressed()
Expand All @@ -103,18 +120,55 @@ public async Task AddRequestDecompression_CompressedBodyContent_IsDecompressed()
}

[Fact]
public async Task AddRequestDecompression_PlainBodyContent_IsUnaltered()
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();

var json = "{\"Foo\":\"Bar\"}";
var content = new StringContent(json, Encoding.UTF8, "application/json");
var gzipped = CompressGzip(json);
var content = new ByteArrayContent(gzipped);
content.Headers.Add("Content-Encoding", "gzip");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

var response = await client.PostAsync("/echo", content);
var responseBody = await response.Content.ReadAsStringAsync();
// Act
try
{
var _ = await client.PostAsync("/echo", content);
}
catch
{
// We're expecting an exception here... what we're interested in is what happens on the server
}

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(json, responseBody);
// 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");
}
}
}

private static byte[] CompressGzip(string str)
Expand Down