Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit 70fce81

Browse files
moozzykmoozzyk
authored andcommitted
Adding additional test coverage
1 parent 300a052 commit 70fce81

File tree

2 files changed

+206
-13
lines changed

2 files changed

+206
-13
lines changed

test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachingTests.cs

Lines changed: 184 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Net;
56
using System.Net.Http;
67
using System.Threading;
78
using System.Threading.Tasks;
@@ -16,8 +17,10 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
1617
{
1718
public class ResponseCachingTests
1819
{
19-
[Fact]
20-
public async void ServesCachedContent_IfAvailable()
20+
[Theory]
21+
[InlineData("GET")]
22+
[InlineData("HEAD")]
23+
public async void ServesCachedContent_IfAvailable(string method)
2124
{
2225
var builders = TestUtils.CreateBuildersWithResponseCaching();
2326

@@ -26,16 +29,36 @@ public async void ServesCachedContent_IfAvailable()
2629
using (var server = new TestServer(builder))
2730
{
2831
var client = server.CreateClient();
29-
var initialResponse = await client.GetAsync("");
30-
var subsequentResponse = await client.GetAsync("");
32+
var initialResponse = await client.SendAsync(TestUtils.CreateRequest(method, ""));
33+
var subsequentResponse = await client.SendAsync(TestUtils.CreateRequest(method, ""));
3134

3235
await AssertCachedResponseAsync(initialResponse, subsequentResponse);
3336
}
3437
}
3538
}
3639

40+
[Theory]
41+
[InlineData("GET")]
42+
[InlineData("HEAD")]
43+
public async void ServesFreshContent_IfNotAvailable(string method)
44+
{
45+
var builders = TestUtils.CreateBuildersWithResponseCaching();
46+
47+
foreach (var builder in builders)
48+
{
49+
using (var server = new TestServer(builder))
50+
{
51+
var client = server.CreateClient();
52+
var initialResponse = await client.SendAsync(TestUtils.CreateRequest(method, ""));
53+
var subsequentResponse = await client.SendAsync(TestUtils.CreateRequest(method, "different"));
54+
55+
await AssertFreshResponseAsync(initialResponse, subsequentResponse);
56+
}
57+
}
58+
}
59+
3760
[Fact]
38-
public async void ServesFreshContent_IfNotAvailable()
61+
public async void ServesFreshContent_Post()
3962
{
4063
var builders = TestUtils.CreateBuildersWithResponseCaching();
4164

@@ -44,8 +67,151 @@ public async void ServesFreshContent_IfNotAvailable()
4467
using (var server = new TestServer(builder))
4568
{
4669
var client = server.CreateClient();
47-
var initialResponse = await client.GetAsync("");
48-
var subsequentResponse = await client.GetAsync("/different");
70+
var initialResponse = await client.PostAsync("", new StringContent(string.Empty));
71+
var subsequentResponse = await client.PostAsync("", new StringContent(string.Empty));
72+
73+
await AssertFreshResponseAsync(initialResponse, subsequentResponse);
74+
}
75+
}
76+
}
77+
78+
[Fact]
79+
public async void ServesFreshContent_Head_Get()
80+
{
81+
var builders = TestUtils.CreateBuildersWithResponseCaching();
82+
83+
foreach (var builder in builders)
84+
{
85+
using (var server = new TestServer(builder))
86+
{
87+
var client = server.CreateClient();
88+
var subsequentResponse = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, ""));
89+
var initialResponse = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, ""));
90+
91+
await AssertFreshResponseAsync(initialResponse, subsequentResponse);
92+
}
93+
}
94+
}
95+
96+
[Fact]
97+
public async void ServesFreshContent_Get_Head()
98+
{
99+
var builders = TestUtils.CreateBuildersWithResponseCaching();
100+
101+
foreach (var builder in builders)
102+
{
103+
using (var server = new TestServer(builder))
104+
{
105+
var client = server.CreateClient();
106+
var initialResponse = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, ""));
107+
var subsequentResponse = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, ""));
108+
109+
await AssertFreshResponseAsync(initialResponse, subsequentResponse);
110+
}
111+
}
112+
}
113+
114+
[Theory]
115+
[InlineData("GET")]
116+
[InlineData("HEAD")]
117+
public async void ServesFreshContent_If_CacheControlNoCache(string method)
118+
{
119+
var builders = TestUtils.CreateBuildersWithResponseCaching();
120+
121+
foreach (var builder in builders)
122+
{
123+
using (var server = new TestServer(builder))
124+
{
125+
var client = server.CreateClient();
126+
client.DefaultRequestHeaders.CacheControl =
127+
new System.Net.Http.Headers.CacheControlHeaderValue { NoCache = true };
128+
129+
var initialResponse = await client.SendAsync(TestUtils.CreateRequest(method, ""));
130+
var subsequentResponse = await client.SendAsync(TestUtils.CreateRequest(method, ""));
131+
132+
await AssertFreshResponseAsync(initialResponse, subsequentResponse);
133+
}
134+
}
135+
}
136+
137+
[Theory]
138+
[InlineData("GET")]
139+
[InlineData("HEAD")]
140+
public async void ServesFreshContent_If_PragmaNoCache(string method)
141+
{
142+
var builders = TestUtils.CreateBuildersWithResponseCaching();
143+
144+
foreach (var builder in builders)
145+
{
146+
using (var server = new TestServer(builder))
147+
{
148+
var client = server.CreateClient();
149+
client.DefaultRequestHeaders.Pragma.Clear();
150+
client.DefaultRequestHeaders.Pragma.Add(new System.Net.Http.Headers.NameValueHeaderValue("no-cache"));
151+
152+
var initialResponse = await client.SendAsync(TestUtils.CreateRequest(method, ""));
153+
var subsequentResponse = await client.SendAsync(TestUtils.CreateRequest(method, ""));
154+
155+
await AssertFreshResponseAsync(initialResponse, subsequentResponse);
156+
}
157+
}
158+
}
159+
160+
[Theory]
161+
[InlineData("GET")]
162+
[InlineData("HEAD")]
163+
public async void ServesCachedContent_If_PathCasingDiffers(string method)
164+
{
165+
var builders = TestUtils.CreateBuildersWithResponseCaching();
166+
167+
foreach (var builder in builders)
168+
{
169+
using (var server = new TestServer(builder))
170+
{
171+
var client = server.CreateClient();
172+
var initialResponse = await client.SendAsync(TestUtils.CreateRequest(method, "path"));
173+
var subsequentResponse = await client.SendAsync(TestUtils.CreateRequest(method, "PATH"));
174+
175+
await AssertCachedResponseAsync(initialResponse, subsequentResponse);
176+
}
177+
}
178+
}
179+
180+
[Theory]
181+
[InlineData("GET")]
182+
[InlineData("HEAD")]
183+
public async void ServesFreshContent_If_ResponseExpired(string method)
184+
{
185+
var builders = TestUtils.CreateBuildersWithResponseCaching();
186+
187+
foreach (var builder in builders)
188+
{
189+
using (var server = new TestServer(builder))
190+
{
191+
var client = server.CreateClient();
192+
var initialResponse = await client.SendAsync(TestUtils.CreateRequest(method, "?Expires=0"));
193+
var subsequentResponse = await client.SendAsync(TestUtils.CreateRequest(method, ""));
194+
195+
await AssertFreshResponseAsync(initialResponse, subsequentResponse);
196+
}
197+
}
198+
}
199+
200+
[Theory]
201+
[InlineData("GET")]
202+
[InlineData("HEAD")]
203+
public async void ServesFreshContent_If_Authorization_HeaderExists(string method)
204+
{
205+
var builders = TestUtils.CreateBuildersWithResponseCaching();
206+
207+
foreach (var builder in builders)
208+
{
209+
using (var server = new TestServer(builder))
210+
{
211+
var client = server.CreateClient();
212+
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("abc");
213+
var initialResponse = await client.SendAsync(TestUtils.CreateRequest(method, ""));
214+
var subsequentResponse = await client.SendAsync(TestUtils.CreateRequest(method, ""));
49215

50216
await AssertFreshResponseAsync(initialResponse, subsequentResponse);
51217
}
@@ -706,7 +872,17 @@ private static async Task AssertFreshResponseAsync(HttpResponseMessage initialRe
706872
subsequentResponse.EnsureSuccessStatusCode();
707873

708874
Assert.False(subsequentResponse.Headers.Contains(HeaderNames.Age));
709-
Assert.NotEqual(await initialResponse.Content.ReadAsStringAsync(), await subsequentResponse.Content.ReadAsStringAsync());
875+
876+
if (initialResponse.RequestMessage.Method == HttpMethod.Head &&
877+
subsequentResponse.RequestMessage.Method == HttpMethod.Head)
878+
{
879+
Assert.True(initialResponse.Headers.Contains("X-Value"));
880+
Assert.NotEqual(initialResponse.Headers.GetValues("X-Value"), subsequentResponse.Headers.GetValues("X-Value"));
881+
}
882+
else
883+
{
884+
Assert.NotEqual(await initialResponse.Content.ReadAsStringAsync(), await subsequentResponse.Content.ReadAsStringAsync());
885+
}
710886
}
711887
}
712888
}

test/Microsoft.AspNetCore.ResponseCaching.Tests/TestUtils.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Net.Http;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.AspNetCore.Builder;
@@ -30,18 +31,29 @@ static TestUtils()
3031
StreamUtilities.BodySegmentSize = 10;
3132
}
3233

33-
internal static RequestDelegate TestRequestDelegate = async (context) =>
34+
internal static RequestDelegate TestRequestDelegate = async context =>
3435
{
35-
var uniqueId = Guid.NewGuid().ToString();
3636
var headers = context.Response.GetTypedHeaders();
37-
headers.CacheControl = new CacheControlHeaderValue()
37+
38+
var expires = context.Request.Query["Expires"];
39+
if (!string.IsNullOrEmpty(expires))
40+
{
41+
headers.Expires = DateTimeOffset.Now.AddSeconds(int.Parse(expires));
42+
}
43+
44+
var uniqueId = Guid.NewGuid().ToString();
45+
headers.CacheControl = new CacheControlHeaderValue
3846
{
3947
Public = true,
40-
MaxAge = TimeSpan.FromSeconds(10)
48+
MaxAge = string.IsNullOrEmpty(expires) ? TimeSpan.FromSeconds(10) : (TimeSpan?)null
4149
};
4250
headers.Date = DateTimeOffset.UtcNow;
4351
headers.Headers["X-Value"] = uniqueId;
44-
await context.Response.WriteAsync(uniqueId);
52+
53+
if (context.Request.Method != "HEAD")
54+
{
55+
await context.Response.WriteAsync(uniqueId);
56+
}
4557
};
4658

4759
internal static IResponseCachingKeyProvider CreateTestKeyProvider()
@@ -148,6 +160,11 @@ internal static void AssertLoggedMessages(List<WriteContext> messages, params Lo
148160
Assert.Equal(expectedMessages[i].LogLevel, messages[i].LogLevel);
149161
}
150162
}
163+
164+
public static HttpRequestMessage CreateRequest(string method, string requestUri)
165+
{
166+
return new HttpRequestMessage(new HttpMethod(method), requestUri);
167+
}
151168
}
152169

153170
internal class LoggedMessage

0 commit comments

Comments
 (0)