Skip to content

Commit 08db830

Browse files
committed
Established integration tests for the new [TopicResponseCache] attribute
This tests the new `/Web/CachedPage/` stub data (8cbf7da) to ensure that the `Counter.cshtml` (ea40bbb) doesn't increment after two subsequent calls, and that the cache headers are correctly set. It also checks a `/Web/UncachedPage/` stub data (8cbf7da) to ensure that the `Counter.cshtml` (ea40bbb) _is_ correctly incremented, as a control case. The completes the primary requirements for #89.
1 parent ea40bbb commit 08db830

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

OnTopic.AspNetCore.Mvc.IntegrationTests/ServiceCollectionExtensionsTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
| Client Ignia, LLC
44
| Project Topics Library
55
\=============================================================================================================================*/
6+
using System;
67
using System.Net;
78
using Microsoft.AspNetCore.Routing;
89

@@ -107,5 +108,44 @@ public async Task UseStatusCodePages_ReturnsExpectedStatusCode(string path, Http
107108

108109
}
109110

111+
/*==========================================================================================================================
112+
| TEST: USE RESPONSE CACHING: RETURNS CACHED PAGE
113+
\-------------------------------------------------------------------------------------------------------------------------*/
114+
/// <summary>
115+
/// Evaluates a route with response caching, and confirms that the page remains unchanged after subsequent calls.
116+
/// </summary>
117+
/// <remarks>
118+
/// The <c>Counter.cshtml</c> page will increment a number output for every request to a given path. The <c>CachedPage</c>
119+
/// request will not increment because the cached result is being returned; the <c>UncachedPage</c> will increment because
120+
/// the results are not cached.
121+
/// </remarks>
122+
[Theory]
123+
[InlineData("/Web/CachedPage/", "1", "1", true)]
124+
[InlineData("/Web/UncachedPage/", "1", "2", false)]
125+
public async Task UseResponseCaching_ReturnsCachedPage(
126+
string path,
127+
string firstResult,
128+
string secondResult,
129+
bool validateHeaders
130+
) {
131+
132+
var client = _factory.CreateClient();
133+
var uri = new Uri(path, UriKind.Relative);
134+
135+
var response1 = await client.GetAsync(uri).ConfigureAwait(false);
136+
var content1 = await response1.Content.ReadAsStringAsync().ConfigureAwait(false);
137+
138+
var response2 = await client.GetAsync(uri).ConfigureAwait(false);
139+
var content2 = await response2.Content.ReadAsStringAsync().ConfigureAwait(false);
140+
141+
response1.EnsureSuccessStatusCode();
142+
143+
Assert.StartsWith(firstResult, content1, StringComparison.Ordinal);
144+
Assert.StartsWith(secondResult, content2, StringComparison.Ordinal);
145+
Assert.Equal(validateHeaders? true : null, response1.Headers.CacheControl?.Public);
146+
Assert.Equal(validateHeaders? TimeSpan.FromSeconds(10) : null, response1?.Headers.CacheControl?.MaxAge);
147+
148+
}
149+
110150
} //Class
111151
} //Namespace

0 commit comments

Comments
 (0)