Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion src/Shared/EmbeddedResourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ private static bool IsGZipAccepted(HttpRequest httpRequest)

for (int i = 0; i < acceptEncoding.Count; i++)
{
if (string.Equals(acceptEncoding[i]?.Value.Value, GZipEncodingValue, StringComparison.OrdinalIgnoreCase))
var encoding = acceptEncoding[i];

if (encoding.Quality is not 0 &&
string.Equals(encoding.Value.Value, GZipEncodingValue, StringComparison.OrdinalIgnoreCase))
{
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Swashbuckle.AspNetCore.ReDoc/ReDocMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ private static string GetReDocVersion()
.DefaultIfEmpty(string.Empty)
.FirstOrDefault();

private static void SetCacheHeaders(HttpResponse response, ReDocOptions options, string etag = null)
private static void SetHeaders(HttpResponse response, ReDocOptions options, string etag)
{
var headers = response.GetTypedHeaders();
headers.Append("x-redoc-version", ReDocVersion);

if (options.CacheLifetime is { } maxAge)
{
Expand All @@ -98,7 +99,7 @@ private static void SetCacheHeaders(HttpResponse response, ReDocOptions options,
};
}

headers.ETag = new($"\"{etag ?? ReDocVersion}\"", isWeak: true);
headers.ETag = new($"\"{etag}\"", isWeak: true);
}

private static void RespondWithRedirect(HttpResponse response, string location)
Expand Down Expand Up @@ -154,7 +155,7 @@ private async Task RespondWithFile(
var text = content.ToString();
var etag = HashText(text);

SetCacheHeaders(response, _options, etag);
SetHeaders(response, _options, etag);

await response.WriteAsync(text, Encoding.UTF8, cancellationToken);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ private static string GetSwaggerUIVersion()
.DefaultIfEmpty(string.Empty)
.FirstOrDefault();

private static void SetCacheHeaders(HttpResponse response, SwaggerUIOptions options, string etag = null)
private static void SetHeaders(HttpResponse response, SwaggerUIOptions options, string etag)
{
var headers = response.GetTypedHeaders();
headers.Append("x-swagger-ui-version", SwaggerUIVersion);

if (options.CacheLifetime is { } maxAge)
{
Expand All @@ -105,7 +106,7 @@ private static void SetCacheHeaders(HttpResponse response, SwaggerUIOptions opti
};
}

headers.ETag = new($"\"{etag ?? SwaggerUIVersion}\"", isWeak: true);
headers.ETag = new($"\"{etag}\"", isWeak: true);
}

private static void RespondWithRedirect(HttpResponse response, string location)
Expand Down Expand Up @@ -154,7 +155,7 @@ private async Task RespondWithFile(
var text = content.ToString();
var etag = HashText(text);

SetCacheHeaders(response, _options, etag);
SetHeaders(response, _options, etag);

await response.WriteAsync(text, Encoding.UTF8, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,31 @@ public void ReDocOptions_Extensions()
Assert.True(options.ConfigObject.UntrustedSpec);
}

[Fact]
public async Task ReDocMiddleware_Returns_ExpectedAssetContents_Decompressed()
[Theory]
[InlineData(null)]
[InlineData("gzip;q=0, identity; q=0.5, *;q=0")]
[InlineData("deflate, br, zstd")]
public async Task ReDocMiddleware_Returns_ExpectedAssetContents_Decompressed(string acceptEncoding)
{
// Arrange
var cancellationToken = TestContext.Current.CancellationToken;

var site = new TestSite(typeof(ReDocApp.Startup), outputHelper);
using var client = site.BuildClient();

using var request = new HttpRequestMessage(HttpMethod.Get, "/Api-Docs/redoc.standalone.js");

var encodings = acceptEncoding?.Split(',')
.Select((p) => p.Trim())
.ToList();

foreach (var encoding in encodings ?? [])
{
request.Headers.AcceptEncoding.Add(StringWithQualityHeaderValue.Parse(encoding));
}

// Act
using var response = await client.GetAsync("/Api-Docs/redoc.standalone.js", cancellationToken);
using var response = await client.SendAsync(request, cancellationToken);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Expand Down