forked from KevinDockx/HttpCacheHeaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultConfigurationFacts.cs
179 lines (150 loc) · 6.95 KB
/
DefaultConfigurationFacts.cs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Any comments, input: @KevinDockx
// Any issues, requests: https://github.com/KevinDockx/HttpCacheHeaders
using Marvin.Cache.Headers.Test.TestStartups;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Net.Http.Headers;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Xunit;
using EntityTagHeaderValue = System.Net.Http.Headers.EntityTagHeaderValue;
namespace Marvin.Cache.Headers.Test
{
public class DefaultConfigurationFacts
{
private readonly IWebHostBuilder _hostBuilder = new WebHostBuilder()
.UseStartup<DefaultStartup>();
private readonly TestServer _server;
public DefaultConfigurationFacts()
{
_server = new TestServer(_hostBuilder);
}
[Fact]
public async Task Adds_Default_Validation_And_ExpirationHeaders()
{
using (var client = _server.CreateClient())
{
var response = await client.GetAsync("/");
Assert.True(response.IsSuccessStatusCode);
Assert.Collection(response.Headers,
pair => Assert.True(pair.Key == HeaderNames.CacheControl && pair.Value.First() == "public, max-age=60"),
pair => Assert.True(pair.Key == HeaderNames.ETag),
pair =>
{
Assert.True(pair.Key == HeaderNames.Vary);
Assert.Collection(response.Headers.Vary,
vary => Assert.Equal("Accept", vary),
vary => Assert.Equal("Accept-Language", vary),
vary => Assert.Equal("Accept-Encoding", vary));
});
}
}
[Fact]
public async Task Returns_Same_Etag_For_Same_Request()
{
using (var client = _server.CreateClient())
{
var response1 = await client.GetAsync("/");
var response2 = await client.GetAsync("/");
Assert.True(response1.IsSuccessStatusCode);
Assert.True(response2.IsSuccessStatusCode);
Assert.Equal(
response1.Headers.GetValues(HeaderNames.ETag).First(),
response2.Headers.GetValues(HeaderNames.ETag).First());
}
}
[Fact]
public async Task Returns_Different_Etag_For_Different_Request()
{
using (var client = _server.CreateClient())
{
var response1 = await client.GetAsync("/foo");
var response2 = await client.GetAsync("/bar");
Assert.True(response1.IsSuccessStatusCode);
Assert.True(response2.IsSuccessStatusCode);
Assert.NotEqual(
response1.Headers.GetValues(HeaderNames.ETag).First(),
response2.Headers.GetValues(HeaderNames.ETag).First());
}
}
[Theory]
[InlineData(500)]
[InlineData(1000)]
[InlineData(1500)]
public async Task Return_304_When_Request_Is_Cached(int delayInMs)
{
using (var client = _server.CreateClient())
{
var response1 = await client.GetAsync("/");
var lastmodified = response1.Content.Headers.LastModified;
var etag = response1.Headers.GetValues(HeaderNames.ETag).First();
await Task.Delay(delayInMs);
client.DefaultRequestHeaders.IfNoneMatch.Add(new EntityTagHeaderValue(etag, false));
client.DefaultRequestHeaders.IfModifiedSince = lastmodified.Value.AddMilliseconds(delayInMs);
var response2 = await client.GetAsync("/");
Assert.True(response1.IsSuccessStatusCode);
Assert.True(response2.StatusCode == HttpStatusCode.NotModified);
Assert.Equal(
response1.Headers.GetValues(HeaderNames.ETag).First(),
response2.Headers.GetValues(HeaderNames.ETag).First());
}
}
[Fact]
public async Task Returns_304_NotModified_When_Request_Has_Matching_Etag_And_No_IfModifiedSince()
{
using (var client = _server.CreateClient())
{
var response1 = await client.GetAsync("/");
var etag = response1.Headers.GetValues(HeaderNames.ETag).First();
client.DefaultRequestHeaders.IfNoneMatch.Add(new EntityTagHeaderValue(etag, false));
var response2 = await client.GetAsync("/");
Assert.True(response1.IsSuccessStatusCode);
Assert.True(response2.StatusCode == HttpStatusCode.NotModified);
}
}
[Fact]
public async Task Returns_304_NotModified_When_Request_HasValid_IfModifiedSince_AndNo_Etag()
{
using (var client = _server.CreateClient())
{
var response1 = await client.GetAsync("/");
var lastmodified = response1.Content.Headers.LastModified;
client.DefaultRequestHeaders.IfModifiedSince = lastmodified;
var response2 = await client.GetAsync("/");
Assert.True(response1.IsSuccessStatusCode);
Assert.True(response2.StatusCode == HttpStatusCode.NotModified);
}
}
[Fact]
public async Task IfModifiedSince_OnRequest_IsIgnored_When_RequestContains_Valid_IfNoneMatch_Header()
{
using (var client = _server.CreateClient())
{
var response1 = await client.GetAsync("/");
var lastmodified = response1.Content.Headers.LastModified;
var etag = response1.Headers.GetValues(HeaderNames.ETag).First();
var expiredLastModified = lastmodified.Value.AddMilliseconds(-600000);
client.DefaultRequestHeaders.IfNoneMatch.Add(new EntityTagHeaderValue(etag, false));
client.DefaultRequestHeaders.IfModifiedSince = expiredLastModified;
var response2 = await client.GetAsync("/");
Assert.True(response1.IsSuccessStatusCode);
Assert.True(response2.StatusCode == HttpStatusCode.NotModified);
}
}
[Fact]
public async Task Returns_200_Ok_When_Request_HasValid_IfModifiedSince_But_Invalid_Etag()
{
using (var client = _server.CreateClient())
{
var response1 = await client.GetAsync("/");
var lastmodified = response1.Content.Headers.LastModified;
client.DefaultRequestHeaders.IfNoneMatch.Add(new EntityTagHeaderValue("\"invalid-etag\"", false));
client.DefaultRequestHeaders.IfModifiedSince = lastmodified;
var response2 = await client.GetAsync("/");
Assert.True(response1.IsSuccessStatusCode);
Assert.True(response2.StatusCode == HttpStatusCode.OK);
}
}
}
}