Skip to content

Commit

Permalink
Established integration tests for the new [TopicResponseCache] attr…
Browse files Browse the repository at this point in the history
…ibute

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.
  • Loading branch information
JeremyCaney committed Dec 17, 2021
1 parent ea40bbb commit 08db830
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
| Client Ignia, LLC
| Project Topics Library
\=============================================================================================================================*/
using System;
using System.Net;
using Microsoft.AspNetCore.Routing;

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

}

/*==========================================================================================================================
| TEST: USE RESPONSE CACHING: RETURNS CACHED PAGE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Evaluates a route with response caching, and confirms that the page remains unchanged after subsequent calls.
/// </summary>
/// <remarks>
/// The <c>Counter.cshtml</c> page will increment a number output for every request to a given path. The <c>CachedPage</c>
/// request will not increment because the cached result is being returned; the <c>UncachedPage</c> will increment because
/// the results are not cached.
/// </remarks>
[Theory]
[InlineData("/Web/CachedPage/", "1", "1", true)]
[InlineData("/Web/UncachedPage/", "1", "2", false)]
public async Task UseResponseCaching_ReturnsCachedPage(
string path,
string firstResult,
string secondResult,
bool validateHeaders
) {

var client = _factory.CreateClient();
var uri = new Uri(path, UriKind.Relative);

var response1 = await client.GetAsync(uri).ConfigureAwait(false);
var content1 = await response1.Content.ReadAsStringAsync().ConfigureAwait(false);

var response2 = await client.GetAsync(uri).ConfigureAwait(false);
var content2 = await response2.Content.ReadAsStringAsync().ConfigureAwait(false);

response1.EnsureSuccessStatusCode();

Assert.StartsWith(firstResult, content1, StringComparison.Ordinal);
Assert.StartsWith(secondResult, content2, StringComparison.Ordinal);
Assert.Equal(validateHeaders? true : null, response1.Headers.CacheControl?.Public);
Assert.Equal(validateHeaders? TimeSpan.FromSeconds(10) : null, response1?.Headers.CacheControl?.MaxAge);

}

} //Class
} //Namespace

0 comments on commit 08db830

Please sign in to comment.