diff --git a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachePolicyProvider.cs b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachePolicyProvider.cs index 351faa0df9..341c2e0d00 100644 --- a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachePolicyProvider.cs +++ b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachePolicyProvider.cs @@ -16,8 +16,7 @@ namespace Microsoft.AspNetCore.ResponseCaching { // Verify the method var request = context.HttpContext.Request; - if (!string.Equals("GET", request.Method, StringComparison.OrdinalIgnoreCase) && - !string.Equals("HEAD", request.Method, StringComparison.OrdinalIgnoreCase)) + if (!HttpMethods.IsGet(request.Method) && !HttpMethods.IsHead(request.Method)) { return false; } diff --git a/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCacheKeyProviderTests.cs b/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCacheKeyProviderTests.cs index 99d42298b2..87126b90e8 100644 --- a/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCacheKeyProviderTests.cs +++ b/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCacheKeyProviderTests.cs @@ -36,10 +36,10 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests UseCaseSensitivePaths = false }); var context = TestUtils.CreateTestContext(); - context.HttpContext.Request.Method = "GET"; + context.HttpContext.Request.Method = HttpMethods.Get; context.HttpContext.Request.Path = "/Path"; - Assert.Equal($"GET{KeyDelimiter}/PATH", cacheKeyProvider.CreateBaseKey(context)); + Assert.Equal($"{HttpMethods.Get}{KeyDelimiter}/PATH", cacheKeyProvider.CreateBaseKey(context)); } [Fact] @@ -50,10 +50,10 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests UseCaseSensitivePaths = true }); var context = TestUtils.CreateTestContext(); - context.HttpContext.Request.Method = "GET"; + context.HttpContext.Request.Method = HttpMethods.Get; context.HttpContext.Request.Path = "/Path"; - Assert.Equal($"GET{KeyDelimiter}/Path", cacheKeyProvider.CreateBaseKey(context)); + Assert.Equal($"{HttpMethods.Get}{KeyDelimiter}/Path", cacheKeyProvider.CreateBaseKey(context)); } [Fact] diff --git a/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachePolicyProviderTests.cs b/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachePolicyProviderTests.cs index 071ab2fbb3..65bbf5434b 100644 --- a/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachePolicyProviderTests.cs +++ b/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachePolicyProviderTests.cs @@ -11,9 +11,20 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests { public class ResponseCachePolicyProviderTests { + public static TheoryData CacheableMethods + { + get + { + return new TheoryData + { + HttpMethods.Get, + HttpMethods.Head + }; + } + } + [Theory] - [InlineData("GET")] - [InlineData("HEAD")] + [MemberData(nameof(CacheableMethods))] public void IsRequestCacheable_CacheableMethods_Allowed(string method) { var context = TestUtils.CreateTestContext(); @@ -21,16 +32,26 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests Assert.True(new ResponseCachePolicyProvider().IsRequestCacheable(context)); } + public static TheoryData NonCacheableMethods + { + get + { + return new TheoryData + { + HttpMethods.Post, + HttpMethods.Put, + HttpMethods.Delete, + HttpMethods.Trace, + HttpMethods.Connect, + HttpMethods.Options, + "", + null + }; + } + } [Theory] - [InlineData("POST")] - [InlineData("OPTIONS")] - [InlineData("PUT")] - [InlineData("DELETE")] - [InlineData("TRACE")] - [InlineData("CONNECT")] - [InlineData("")] - [InlineData(null)] + [MemberData(nameof(NonCacheableMethods))] public void IsRequestCacheable_UncacheableMethods_NotAllowed(string method) { var context = TestUtils.CreateTestContext(); @@ -43,7 +64,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests public void IsRequestCacheable_AuthorizationHeaders_NotAllowed() { var context = TestUtils.CreateTestContext(); - context.HttpContext.Request.Method = "GET"; + context.HttpContext.Request.Method = HttpMethods.Get; context.HttpContext.Request.Headers[HeaderNames.Authorization] = "Basic plaintextUN:plaintextPW"; Assert.False(new ResponseCachePolicyProvider().IsRequestCacheable(context)); @@ -53,7 +74,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests public void IsRequestCacheable_NoCache_NotAllowed() { var context = TestUtils.CreateTestContext(); - context.HttpContext.Request.Method = "GET"; + context.HttpContext.Request.Method = HttpMethods.Get; context.TypedRequestHeaders.CacheControl = new CacheControlHeaderValue() { NoCache = true @@ -66,7 +87,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests public void IsRequestCacheable_NoStore_Allowed() { var context = TestUtils.CreateTestContext(); - context.HttpContext.Request.Method = "GET"; + context.HttpContext.Request.Method = HttpMethods.Get; context.TypedRequestHeaders.CacheControl = new CacheControlHeaderValue() { NoStore = true @@ -79,7 +100,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests public void IsRequestCacheable_LegacyDirectives_NotAllowed() { var context = TestUtils.CreateTestContext(); - context.HttpContext.Request.Method = "GET"; + context.HttpContext.Request.Method = HttpMethods.Get; context.HttpContext.Request.Headers[HeaderNames.Pragma] = "no-cache"; Assert.False(new ResponseCachePolicyProvider().IsRequestCacheable(context)); @@ -89,7 +110,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests public void IsRequestCacheable_LegacyDirectives_OverridenByCacheControl() { 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.CacheControl] = "max-age=10";