Use HttpMethods
This commit is contained in:
parent
44b0dfd5bb
commit
10381b0456
|
|
@ -16,8 +16,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
|
||||||
{
|
{
|
||||||
// Verify the method
|
// Verify the method
|
||||||
var request = context.HttpContext.Request;
|
var request = context.HttpContext.Request;
|
||||||
if (!string.Equals("GET", request.Method, StringComparison.OrdinalIgnoreCase) &&
|
if (!HttpMethods.IsGet(request.Method) && !HttpMethods.IsHead(request.Method))
|
||||||
!string.Equals("HEAD", request.Method, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,10 +36,10 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
UseCaseSensitivePaths = false
|
UseCaseSensitivePaths = false
|
||||||
});
|
});
|
||||||
var context = TestUtils.CreateTestContext();
|
var context = TestUtils.CreateTestContext();
|
||||||
context.HttpContext.Request.Method = "GET";
|
context.HttpContext.Request.Method = HttpMethods.Get;
|
||||||
context.HttpContext.Request.Path = "/Path";
|
context.HttpContext.Request.Path = "/Path";
|
||||||
|
|
||||||
Assert.Equal($"GET{KeyDelimiter}/PATH", cacheKeyProvider.CreateBaseKey(context));
|
Assert.Equal($"{HttpMethods.Get}{KeyDelimiter}/PATH", cacheKeyProvider.CreateBaseKey(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -50,10 +50,10 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
UseCaseSensitivePaths = true
|
UseCaseSensitivePaths = true
|
||||||
});
|
});
|
||||||
var context = TestUtils.CreateTestContext();
|
var context = TestUtils.CreateTestContext();
|
||||||
context.HttpContext.Request.Method = "GET";
|
context.HttpContext.Request.Method = HttpMethods.Get;
|
||||||
context.HttpContext.Request.Path = "/Path";
|
context.HttpContext.Request.Path = "/Path";
|
||||||
|
|
||||||
Assert.Equal($"GET{KeyDelimiter}/Path", cacheKeyProvider.CreateBaseKey(context));
|
Assert.Equal($"{HttpMethods.Get}{KeyDelimiter}/Path", cacheKeyProvider.CreateBaseKey(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,20 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
{
|
{
|
||||||
public class ResponseCachePolicyProviderTests
|
public class ResponseCachePolicyProviderTests
|
||||||
{
|
{
|
||||||
|
public static TheoryData<string> CacheableMethods
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return new TheoryData<string>
|
||||||
|
{
|
||||||
|
HttpMethods.Get,
|
||||||
|
HttpMethods.Head
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("GET")]
|
[MemberData(nameof(CacheableMethods))]
|
||||||
[InlineData("HEAD")]
|
|
||||||
public void IsRequestCacheable_CacheableMethods_Allowed(string method)
|
public void IsRequestCacheable_CacheableMethods_Allowed(string method)
|
||||||
{
|
{
|
||||||
var context = TestUtils.CreateTestContext();
|
var context = TestUtils.CreateTestContext();
|
||||||
|
|
@ -21,16 +32,26 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
|
|
||||||
Assert.True(new ResponseCachePolicyProvider().IsRequestCacheable(context));
|
Assert.True(new ResponseCachePolicyProvider().IsRequestCacheable(context));
|
||||||
}
|
}
|
||||||
|
public static TheoryData<string> NonCacheableMethods
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return new TheoryData<string>
|
||||||
|
{
|
||||||
|
HttpMethods.Post,
|
||||||
|
HttpMethods.Put,
|
||||||
|
HttpMethods.Delete,
|
||||||
|
HttpMethods.Trace,
|
||||||
|
HttpMethods.Connect,
|
||||||
|
HttpMethods.Options,
|
||||||
|
"",
|
||||||
|
null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("POST")]
|
[MemberData(nameof(NonCacheableMethods))]
|
||||||
[InlineData("OPTIONS")]
|
|
||||||
[InlineData("PUT")]
|
|
||||||
[InlineData("DELETE")]
|
|
||||||
[InlineData("TRACE")]
|
|
||||||
[InlineData("CONNECT")]
|
|
||||||
[InlineData("")]
|
|
||||||
[InlineData(null)]
|
|
||||||
public void IsRequestCacheable_UncacheableMethods_NotAllowed(string method)
|
public void IsRequestCacheable_UncacheableMethods_NotAllowed(string method)
|
||||||
{
|
{
|
||||||
var context = TestUtils.CreateTestContext();
|
var context = TestUtils.CreateTestContext();
|
||||||
|
|
@ -43,7 +64,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
public void IsRequestCacheable_AuthorizationHeaders_NotAllowed()
|
public void IsRequestCacheable_AuthorizationHeaders_NotAllowed()
|
||||||
{
|
{
|
||||||
var context = TestUtils.CreateTestContext();
|
var context = TestUtils.CreateTestContext();
|
||||||
context.HttpContext.Request.Method = "GET";
|
context.HttpContext.Request.Method = HttpMethods.Get;
|
||||||
context.HttpContext.Request.Headers[HeaderNames.Authorization] = "Basic plaintextUN:plaintextPW";
|
context.HttpContext.Request.Headers[HeaderNames.Authorization] = "Basic plaintextUN:plaintextPW";
|
||||||
|
|
||||||
Assert.False(new ResponseCachePolicyProvider().IsRequestCacheable(context));
|
Assert.False(new ResponseCachePolicyProvider().IsRequestCacheable(context));
|
||||||
|
|
@ -53,7 +74,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
public void IsRequestCacheable_NoCache_NotAllowed()
|
public void IsRequestCacheable_NoCache_NotAllowed()
|
||||||
{
|
{
|
||||||
var context = TestUtils.CreateTestContext();
|
var context = TestUtils.CreateTestContext();
|
||||||
context.HttpContext.Request.Method = "GET";
|
context.HttpContext.Request.Method = HttpMethods.Get;
|
||||||
context.TypedRequestHeaders.CacheControl = new CacheControlHeaderValue()
|
context.TypedRequestHeaders.CacheControl = new CacheControlHeaderValue()
|
||||||
{
|
{
|
||||||
NoCache = true
|
NoCache = true
|
||||||
|
|
@ -66,7 +87,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
public void IsRequestCacheable_NoStore_Allowed()
|
public void IsRequestCacheable_NoStore_Allowed()
|
||||||
{
|
{
|
||||||
var context = TestUtils.CreateTestContext();
|
var context = TestUtils.CreateTestContext();
|
||||||
context.HttpContext.Request.Method = "GET";
|
context.HttpContext.Request.Method = HttpMethods.Get;
|
||||||
context.TypedRequestHeaders.CacheControl = new CacheControlHeaderValue()
|
context.TypedRequestHeaders.CacheControl = new CacheControlHeaderValue()
|
||||||
{
|
{
|
||||||
NoStore = true
|
NoStore = true
|
||||||
|
|
@ -79,7 +100,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
public void IsRequestCacheable_LegacyDirectives_NotAllowed()
|
public void IsRequestCacheable_LegacyDirectives_NotAllowed()
|
||||||
{
|
{
|
||||||
var context = TestUtils.CreateTestContext();
|
var context = TestUtils.CreateTestContext();
|
||||||
context.HttpContext.Request.Method = "GET";
|
context.HttpContext.Request.Method = HttpMethods.Get;
|
||||||
context.HttpContext.Request.Headers[HeaderNames.Pragma] = "no-cache";
|
context.HttpContext.Request.Headers[HeaderNames.Pragma] = "no-cache";
|
||||||
|
|
||||||
Assert.False(new ResponseCachePolicyProvider().IsRequestCacheable(context));
|
Assert.False(new ResponseCachePolicyProvider().IsRequestCacheable(context));
|
||||||
|
|
@ -89,7 +110,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
public void IsRequestCacheable_LegacyDirectives_OverridenByCacheControl()
|
public void IsRequestCacheable_LegacyDirectives_OverridenByCacheControl()
|
||||||
{
|
{
|
||||||
var context = TestUtils.CreateTestContext();
|
var context = TestUtils.CreateTestContext();
|
||||||
context.HttpContext.Request.Method = "GET";
|
context.HttpContext.Request.Method = HttpMethods.Get;
|
||||||
context.HttpContext.Request.Headers[HeaderNames.Pragma] = "no-cache";
|
context.HttpContext.Request.Headers[HeaderNames.Pragma] = "no-cache";
|
||||||
context.HttpContext.Request.Headers[HeaderNames.CacheControl] = "max-age=10";
|
context.HttpContext.Request.Headers[HeaderNames.CacheControl] = "max-age=10";
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue