aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline.Tests/Helpers/CacheTestHelper.cs
blob: ef230cb0b4bcaf9b1f531abe48dadb05ba3718b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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.Should().NotBeNull();
                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));
                res.Headers.ETag.Should().NotBeNull();
                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);
                await res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
                    .And.HaveCommonBodyWithCodeAsync(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);
            }
        }
    }
}