Skip to content

Commit

Permalink
Fix reverse proxy not compressing responses as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Feb 21, 2022
1 parent 6d344ea commit 1fb2a8f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion API/Protocol/ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public static FlexibleContentType Parse(string header)

if (charsetIndex > 0)
{
return Get(contentType, span[charsetIndex..].Trim().ToString());
return Get(contentType, span[(charsetIndex + 1)..].Trim().ToString());
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Modules/Basics/CoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static bool HasType(this IRequest request, params RequestMethod[] methods
/// Specifies the content type of this response.
/// </summary>
/// <param name="contentType">The content type of this response</param>
public static IResponseBuilder Type(this IResponseBuilder builder, string contentType) => builder.Type(FlexibleContentType.Get(contentType));
public static IResponseBuilder Type(this IResponseBuilder builder, string contentType) => builder.Type(FlexibleContentType.Parse(contentType));

#endregion

Expand Down
21 changes: 21 additions & 0 deletions Testing/Engine/CompressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ public async Task TestVariyHeaderExtendedAdded()
Assert.IsTrue(response.Headers.Vary.Contains("Accept-Encoding"));
}

[TestMethod]
public async Task TestContentType()
{
var handler = new FunctionalHandler(responseProvider: (r) =>
{
return r.Respond()
.Content(Resource.FromString("Hello World").Build())
.Type("application/json; charset=utf-8")
.Build();
});

using var runner = TestRunner.Run(handler.Wrap());

var request = runner.GetRequest();
request.Headers.Add("Accept-Encoding", "gzip, deflate, br");

using var response = await runner.GetResponse(request);

Assert.AreEqual("br", response.Content.Headers.ContentEncoding.First());
}

}

}
24 changes: 23 additions & 1 deletion Testing/Modules/ReverseProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Linq;

Expand All @@ -11,11 +12,11 @@
using GenHTTP.Api.Protocol;

using GenHTTP.Modules.IO;
using GenHTTP.Modules.Practices;
using GenHTTP.Modules.ReverseProxy;
using GenHTTP.Modules.Layouting;

using Cookie = GenHTTP.Api.Protocol.Cookie;
using System.Net.Http;

namespace GenHTTP.Testing.Acceptance.Providers
{
Expand Down Expand Up @@ -57,6 +58,7 @@ public static TestSetup Create(Func<IRequest, IResponse?> response)

runner.Host.Handler(proxy)
.Development()
.Defaults()
.Start();

return new TestSetup(runner, testServer);
Expand Down Expand Up @@ -400,6 +402,26 @@ public async Task TestBadGateway()
Assert.AreEqual(HttpStatusCode.BadGateway, response.StatusCode);
}


[TestMethod]
public async Task TestCompression()
{
using var setup = TestSetup.Create((r) =>
{
return r.Respond().Content("Hello World!").Build();
});

var runner = setup.Runner;

var request = runner.GetRequest();

request.Headers.Add("Accept-Encoding", "br, gzip, deflate");

using var response = await runner.GetResponse(request);

Assert.AreEqual("br", response.GetContentHeader("Content-Encoding"));
}

}

}

0 comments on commit 1fb2a8f

Please sign in to comment.