aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers/CacheTestHelper.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-03-13 17:58:32 +0800
committercrupest <crupest@outlook.com>2020-03-13 17:58:32 +0800
commit636cf3839d92e884987e4e3aec7f23953d02fe37 (patch)
treedd53c68673c84a0cbf157ef0378dbdc1b4610d53 /Timeline.Tests/Helpers/CacheTestHelper.cs
parent8a1ecbf49673cb2bed538ac8bc4e82691b90d973 (diff)
downloadtimeline-636cf3839d92e884987e4e3aec7f23953d02fe37.tar.gz
timeline-636cf3839d92e884987e4e3aec7f23953d02fe37.tar.bz2
timeline-636cf3839d92e884987e4e3aec7f23953d02fe37.zip
Add cache for timeline post data.
Diffstat (limited to 'Timeline.Tests/Helpers/CacheTestHelper.cs')
-rw-r--r--Timeline.Tests/Helpers/CacheTestHelper.cs64
1 files changed, 64 insertions, 0 deletions
diff --git a/Timeline.Tests/Helpers/CacheTestHelper.cs b/Timeline.Tests/Helpers/CacheTestHelper.cs
new file mode 100644
index 00000000..b3709a28
--- /dev/null
+++ b/Timeline.Tests/Helpers/CacheTestHelper.cs
@@ -0,0 +1,64 @@
+using FluentAssertions;
+using System;
+using System.Net;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Threading.Tasks;
+using Timeline.Models.Http;
+
+namespace Timeline.Tests.Helpers
+{
+ public static class CacheTestHelper
+ {
+ public static async Task TestCache(HttpClient client, string getUrl)
+ {
+ EntityTagHeaderValue eTag;
+ {
+ var res = await client.GetAsync(getUrl);
+ res.Should().HaveStatusCode(200);
+ var cacheControlHeader = res.Headers.CacheControl;
+ cacheControlHeader.NoCache.Should().BeTrue();
+ cacheControlHeader.NoStore.Should().BeFalse();
+ cacheControlHeader.Private.Should().BeTrue();
+ cacheControlHeader.Public.Should().BeFalse();
+ cacheControlHeader.MustRevalidate.Should().BeTrue();
+ cacheControlHeader.MaxAge.Should().NotBeNull().And.Be(TimeSpan.FromDays(14));
+ eTag = res.Headers.ETag;
+ }
+
+ {
+ using var request = new HttpRequestMessage()
+ {
+ RequestUri = new Uri(client.BaseAddress, getUrl),
+ Method = HttpMethod.Get,
+ };
+ request.Headers.TryAddWithoutValidation("If-None-Match", "\"dsdfd");
+ var res = await client.SendAsync(request);
+ res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
+ .And.HaveCommonBody(ErrorCodes.Common.Header.IfNonMatch_BadFormat);
+ }
+
+ {
+ using var request = new HttpRequestMessage()
+ {
+ RequestUri = new Uri(client.BaseAddress, getUrl),
+ Method = HttpMethod.Get,
+ };
+ request.Headers.TryAddWithoutValidation("If-None-Match", "\"aaa\"");
+ var res = await client.SendAsync(request);
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+
+ {
+ using var request = new HttpRequestMessage()
+ {
+ RequestUri = new Uri(client.BaseAddress, getUrl),
+ Method = HttpMethod.Get,
+ };
+ request.Headers.Add("If-None-Match", eTag.ToString());
+ var res = await client.SendAsync(request);
+ res.Should().HaveStatusCode(HttpStatusCode.NotModified);
+ }
+ }
+ }
+}