Add options to configure case sensitivity of request paths
This commit is contained in:
parent
0f711ce279
commit
411681e8d6
|
|
@ -153,7 +153,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
|
||||||
builder
|
builder
|
||||||
.Append(request.Method.ToUpperInvariant())
|
.Append(request.Method.ToUpperInvariant())
|
||||||
.Append(KeyDelimiter)
|
.Append(KeyDelimiter)
|
||||||
.Append(request.Path.Value.ToUpperInvariant());
|
.Append(_options.CaseSensitivePaths ? request.Path.Value : request.Path.Value.ToUpperInvariant());
|
||||||
|
|
||||||
// Vary by headers
|
// Vary by headers
|
||||||
if (varyBy?.Headers.Count > 0)
|
if (varyBy?.Headers.Count > 0)
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,15 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
public class ResponseCachingOptions
|
public class ResponseCachingOptions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The largest cacheable size for the response body in bytes.
|
/// The largest cacheable size for the response body in bytes. The default is set to 1 MB.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public long MaximumCachedBodySize { get; set; } = 1024 * 1024;
|
public long MaximumCachedBodySize { get; set; } = 1024 * 1024;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// <c>true</c> if request paths are case-sensitive; otherwise <c>false</c>. The default is to treat paths as case-insensitive.
|
||||||
|
/// </summary>
|
||||||
|
public bool CaseSensitivePaths { get; set; } = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// For testing purposes only.
|
/// For testing purposes only.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -280,6 +280,34 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CreateCacheKey_CaseInsensitivePath_NormalizesPath()
|
||||||
|
{
|
||||||
|
var httpContext = new DefaultHttpContext();
|
||||||
|
httpContext.Request.Method = "GET";
|
||||||
|
httpContext.Request.Path = "/Path";
|
||||||
|
var context = CreateTestContext(httpContext, new ResponseCachingOptions()
|
||||||
|
{
|
||||||
|
CaseSensitivePaths = false
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.Equal($"GET{KeyDelimiter}/PATH", context.CreateCacheKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CreateCacheKey_CaseSensitivePath_PreservesPathCase()
|
||||||
|
{
|
||||||
|
var httpContext = new DefaultHttpContext();
|
||||||
|
httpContext.Request.Method = "GET";
|
||||||
|
httpContext.Request.Path = "/Path";
|
||||||
|
var context = CreateTestContext(httpContext, new ResponseCachingOptions()
|
||||||
|
{
|
||||||
|
CaseSensitivePaths = true
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.Equal($"GET{KeyDelimiter}/Path", context.CreateCacheKey());
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ResponseIsCacheable_NoPublic_NotAllowed()
|
public void ResponseIsCacheable_NoPublic_NotAllowed()
|
||||||
{
|
{
|
||||||
|
|
@ -832,6 +860,16 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
{
|
{
|
||||||
return CreateTestContext(
|
return CreateTestContext(
|
||||||
httpContext,
|
httpContext,
|
||||||
|
new ResponseCachingOptions(),
|
||||||
|
new NoopCacheKeyModifier(),
|
||||||
|
new NoopCacheabilityValidator());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ResponseCachingContext CreateTestContext(HttpContext httpContext, ResponseCachingOptions options)
|
||||||
|
{
|
||||||
|
return CreateTestContext(
|
||||||
|
httpContext,
|
||||||
|
options,
|
||||||
new NoopCacheKeyModifier(),
|
new NoopCacheKeyModifier(),
|
||||||
new NoopCacheabilityValidator());
|
new NoopCacheabilityValidator());
|
||||||
}
|
}
|
||||||
|
|
@ -840,6 +878,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
{
|
{
|
||||||
return CreateTestContext(
|
return CreateTestContext(
|
||||||
httpContext,
|
httpContext,
|
||||||
|
new ResponseCachingOptions(),
|
||||||
cacheKeyModifier,
|
cacheKeyModifier,
|
||||||
new NoopCacheabilityValidator());
|
new NoopCacheabilityValidator());
|
||||||
}
|
}
|
||||||
|
|
@ -848,19 +887,21 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
{
|
{
|
||||||
return CreateTestContext(
|
return CreateTestContext(
|
||||||
httpContext,
|
httpContext,
|
||||||
|
new ResponseCachingOptions(),
|
||||||
new NoopCacheKeyModifier(),
|
new NoopCacheKeyModifier(),
|
||||||
cacheabilityValidator);
|
cacheabilityValidator);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ResponseCachingContext CreateTestContext(
|
private static ResponseCachingContext CreateTestContext(
|
||||||
HttpContext httpContext,
|
HttpContext httpContext,
|
||||||
|
ResponseCachingOptions options,
|
||||||
IResponseCachingCacheKeyModifier cacheKeyModifier,
|
IResponseCachingCacheKeyModifier cacheKeyModifier,
|
||||||
IResponseCachingCacheabilityValidator cacheabilityValidator)
|
IResponseCachingCacheabilityValidator cacheabilityValidator)
|
||||||
{
|
{
|
||||||
return new ResponseCachingContext(
|
return new ResponseCachingContext(
|
||||||
httpContext,
|
httpContext,
|
||||||
new TestResponseCache(),
|
new TestResponseCache(),
|
||||||
new ResponseCachingOptions(),
|
options,
|
||||||
new DefaultObjectPool<StringBuilder>(new StringBuilderPooledObjectPolicy()),
|
new DefaultObjectPool<StringBuilder>(new StringBuilderPooledObjectPolicy()),
|
||||||
cacheabilityValidator,
|
cacheabilityValidator,
|
||||||
cacheKeyModifier);
|
cacheKeyModifier);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue