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
Create RequestDecompressionMiddlewareTests.cs
  • Loading branch information
jamescrosswell committed Jul 4, 2025
commit ee1b9b9c01572d23c90521df28214d773657a357
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;

namespace Sentry.AspNetCore.Tests.RequestDecompressionMiddleware;

public class RequestDecompressionMiddlewareTests
{
[Fact]
public async Task AddRequestDecompression_CompressedBodyContent_IsDecompressed()
{
// Arrange
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddRouting();
services.AddRequestDecompression(); // No options needed for default gzip support
})
.UseSentry(o =>
{
o.Dsn = ValidDsn;
o.MaxRequestBodySize = RequestSize.Always;
})
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapPost("/echo", async context =>
{
using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
var body = await reader.ReadToEndAsync();
await context.Response.WriteAsync(body);
});
});
});

using var server = new TestServer(builder);
using var client = server.CreateClient();

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

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

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

private static byte[] CompressGzip(string str)
{
var bytes = Encoding.UTF8.GetBytes(str);
using var output = new MemoryStream();
using (var gzip = new GZipStream(output, CompressionLevel.Optimal, leaveOpen: true))
{
gzip.Write(bytes, 0, bytes.Length);
}
return output.ToArray();
}
}