|
| 1 | +using System.Net; |
| 2 | +using TurboHttp.IntegrationTests.Shared; |
| 3 | +using TurboHttp.Protocol.RFC9111; |
| 4 | + |
| 5 | +namespace TurboHttp.IntegrationTests.TLS; |
| 6 | + |
| 7 | +[Collection("TLS")] |
| 8 | +public sealed class CacheIntegrationTests |
| 9 | +{ |
| 10 | + private readonly ServerFixture _server; |
| 11 | + private readonly ActorSystemFixture _systemFixture; |
| 12 | + |
| 13 | + public CacheIntegrationTests(ServerFixture server, ActorSystemFixture systemFixture) |
| 14 | + { |
| 15 | + _server = server; |
| 16 | + _systemFixture = systemFixture; |
| 17 | + } |
| 18 | + |
| 19 | + private ClientHelper CreateCacheClient() |
| 20 | + { |
| 21 | + return ClientHelper.CreateClient( |
| 22 | + _server.HttpsPort, |
| 23 | + new Version(1, 1), |
| 24 | + scheme: "https", |
| 25 | + configure: builder => builder.WithCache(CachePolicy.Default), |
| 26 | + system: _systemFixture.System); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact(DisplayName = "Cache-TLS-001: max-age response served from cache on second request over HTTPS")] |
| 30 | + public async Task MaxAge_Response_Served_From_Cache() |
| 31 | + { |
| 32 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 33 | + await using var helper = CreateCacheClient(); |
| 34 | + |
| 35 | + // First request — populates the cache |
| 36 | + var request1 = new HttpRequestMessage(HttpMethod.Get, "/cache/max-age/3600"); |
| 37 | + var response1 = await helper.Client.SendAsync(request1, cts.Token); |
| 38 | + Assert.Equal(HttpStatusCode.OK, response1.StatusCode); |
| 39 | + var body1 = await response1.Content.ReadAsStringAsync(cts.Token); |
| 40 | + Assert.False(string.IsNullOrEmpty(body1), "First response body should be non-empty"); |
| 41 | + |
| 42 | + // Second request — should be served from cache (identical timestamp) |
| 43 | + var request2 = new HttpRequestMessage(HttpMethod.Get, "/cache/max-age/3600"); |
| 44 | + var response2 = await helper.Client.SendAsync(request2, cts.Token); |
| 45 | + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); |
| 46 | + var body2 = await response2.Content.ReadAsStringAsync(cts.Token); |
| 47 | + |
| 48 | + Assert.Equal(body1, body2); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact(DisplayName = "Cache-TLS-002: no-cache forces revalidation with server over HTTPS")] |
| 52 | + public async Task NoCache_Forces_Revalidation() |
| 53 | + { |
| 54 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 55 | + await using var helper = CreateCacheClient(); |
| 56 | + |
| 57 | + var request1 = new HttpRequestMessage(HttpMethod.Get, "/cache/no-cache"); |
| 58 | + var response1 = await helper.Client.SendAsync(request1, cts.Token); |
| 59 | + Assert.Equal(HttpStatusCode.OK, response1.StatusCode); |
| 60 | + var body1 = await response1.Content.ReadAsStringAsync(cts.Token); |
| 61 | + |
| 62 | + // Small delay to ensure server timestamp differs |
| 63 | + await Task.Delay(TimeSpan.FromMilliseconds(50), cts.Token); |
| 64 | + |
| 65 | + var request2 = new HttpRequestMessage(HttpMethod.Get, "/cache/no-cache"); |
| 66 | + var response2 = await helper.Client.SendAsync(request2, cts.Token); |
| 67 | + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); |
| 68 | + var body2 = await response2.Content.ReadAsStringAsync(cts.Token); |
| 69 | + |
| 70 | + Assert.NotEqual(body1, body2); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact(DisplayName = "Cache-TLS-003: no-store response never cached over HTTPS")] |
| 74 | + public async Task NoStore_Response_Never_Cached() |
| 75 | + { |
| 76 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 77 | + await using var helper = CreateCacheClient(); |
| 78 | + |
| 79 | + var request1 = new HttpRequestMessage(HttpMethod.Get, "/cache/no-store"); |
| 80 | + var response1 = await helper.Client.SendAsync(request1, cts.Token); |
| 81 | + Assert.Equal(HttpStatusCode.OK, response1.StatusCode); |
| 82 | + var body1 = await response1.Content.ReadAsStringAsync(cts.Token); |
| 83 | + |
| 84 | + var request2 = new HttpRequestMessage(HttpMethod.Get, "/cache/no-store"); |
| 85 | + var response2 = await helper.Client.SendAsync(request2, cts.Token); |
| 86 | + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); |
| 87 | + var body2 = await response2.Content.ReadAsStringAsync(cts.Token); |
| 88 | + |
| 89 | + // no-store returns a fixed string "no-store-resource", so bodies will be equal, |
| 90 | + // but the response must not come from cache. |
| 91 | + Assert.Equal("no-store-resource", body1); |
| 92 | + Assert.Equal("no-store-resource", body2); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact(DisplayName = "Cache-TLS-004: ETag revalidation sends If-None-Match over HTTPS")] |
| 96 | + public async Task ETag_Revalidation_Sends_IfNoneMatch() |
| 97 | + { |
| 98 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 99 | + await using var helper = CreateCacheClient(); |
| 100 | + |
| 101 | + var request1 = new HttpRequestMessage(HttpMethod.Get, "/cache/etag/test1"); |
| 102 | + var response1 = await helper.Client.SendAsync(request1, cts.Token); |
| 103 | + Assert.Equal(HttpStatusCode.OK, response1.StatusCode); |
| 104 | + var body1 = await response1.Content.ReadAsStringAsync(cts.Token); |
| 105 | + Assert.Equal("etag-resource-test1", body1); |
| 106 | + |
| 107 | + // Second request should serve from cache (max-age=3600) |
| 108 | + var request2 = new HttpRequestMessage(HttpMethod.Get, "/cache/etag/test1"); |
| 109 | + var response2 = await helper.Client.SendAsync(request2, cts.Token); |
| 110 | + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); |
| 111 | + var body2 = await response2.Content.ReadAsStringAsync(cts.Token); |
| 112 | + |
| 113 | + Assert.Equal(body1, body2); |
| 114 | + } |
| 115 | + |
| 116 | + [Fact(DisplayName = "Cache-TLS-005: Last-Modified revalidation sends If-Modified-Since over HTTPS")] |
| 117 | + public async Task LastModified_Revalidation_Sends_IfModifiedSince() |
| 118 | + { |
| 119 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 120 | + await using var helper = CreateCacheClient(); |
| 121 | + |
| 122 | + var request1 = new HttpRequestMessage(HttpMethod.Get, "/cache/last-modified/doc1"); |
| 123 | + var response1 = await helper.Client.SendAsync(request1, cts.Token); |
| 124 | + Assert.Equal(HttpStatusCode.OK, response1.StatusCode); |
| 125 | + var body1 = await response1.Content.ReadAsStringAsync(cts.Token); |
| 126 | + Assert.Equal("last-modified-resource-doc1", body1); |
| 127 | + |
| 128 | + // Second request should serve from cache (max-age=3600) |
| 129 | + var request2 = new HttpRequestMessage(HttpMethod.Get, "/cache/last-modified/doc1"); |
| 130 | + var response2 = await helper.Client.SendAsync(request2, cts.Token); |
| 131 | + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); |
| 132 | + var body2 = await response2.Content.ReadAsStringAsync(cts.Token); |
| 133 | + |
| 134 | + Assert.Equal(body1, body2); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact(DisplayName = "Cache-TLS-006: Vary header produces different cache entries per header value over HTTPS")] |
| 138 | + public async Task Vary_Header_Produces_Different_Cache_Entries() |
| 139 | + { |
| 140 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 141 | + await using var helper = CreateCacheClient(); |
| 142 | + |
| 143 | + // Request with Accept-Language: en |
| 144 | + var request1 = new HttpRequestMessage(HttpMethod.Get, "/cache/vary/Accept-Language"); |
| 145 | + request1.Headers.TryAddWithoutValidation("Accept-Language", "en"); |
| 146 | + var response1 = await helper.Client.SendAsync(request1, cts.Token); |
| 147 | + Assert.Equal(HttpStatusCode.OK, response1.StatusCode); |
| 148 | + var body1 = await response1.Content.ReadAsStringAsync(cts.Token); |
| 149 | + Assert.Equal("vary-Accept-Language:en", body1); |
| 150 | + |
| 151 | + // Request with Accept-Language: de — should NOT come from cache |
| 152 | + var request2 = new HttpRequestMessage(HttpMethod.Get, "/cache/vary/Accept-Language"); |
| 153 | + request2.Headers.TryAddWithoutValidation("Accept-Language", "de"); |
| 154 | + var response2 = await helper.Client.SendAsync(request2, cts.Token); |
| 155 | + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); |
| 156 | + var body2 = await response2.Content.ReadAsStringAsync(cts.Token); |
| 157 | + Assert.Equal("vary-Accept-Language:de", body2); |
| 158 | + |
| 159 | + Assert.NotEqual(body1, body2); |
| 160 | + |
| 161 | + // Request again with Accept-Language: en — should come from cache |
| 162 | + var request3 = new HttpRequestMessage(HttpMethod.Get, "/cache/vary/Accept-Language"); |
| 163 | + request3.Headers.TryAddWithoutValidation("Accept-Language", "en"); |
| 164 | + var response3 = await helper.Client.SendAsync(request3, cts.Token); |
| 165 | + Assert.Equal(HttpStatusCode.OK, response3.StatusCode); |
| 166 | + var body3 = await response3.Content.ReadAsStringAsync(cts.Token); |
| 167 | + |
| 168 | + Assert.Equal(body1, body3); |
| 169 | + } |
| 170 | + |
| 171 | + [Fact(DisplayName = "Cache-TLS-007: must-revalidate with max-age=0 forces revalidation over HTTPS")] |
| 172 | + public async Task MustRevalidate_Forces_Revalidation() |
| 173 | + { |
| 174 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 175 | + await using var helper = CreateCacheClient(); |
| 176 | + |
| 177 | + var request1 = new HttpRequestMessage(HttpMethod.Get, "/cache/must-revalidate"); |
| 178 | + var response1 = await helper.Client.SendAsync(request1, cts.Token); |
| 179 | + Assert.Equal(HttpStatusCode.OK, response1.StatusCode); |
| 180 | + var body1 = await response1.Content.ReadAsStringAsync(cts.Token); |
| 181 | + |
| 182 | + // Second request — must revalidate (max-age=0), server returns 304 if ETag matches, |
| 183 | + // client should get the same cached body back after merge |
| 184 | + var request2 = new HttpRequestMessage(HttpMethod.Get, "/cache/must-revalidate"); |
| 185 | + var response2 = await helper.Client.SendAsync(request2, cts.Token); |
| 186 | + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); |
| 187 | + var body2 = await response2.Content.ReadAsStringAsync(cts.Token); |
| 188 | + |
| 189 | + Assert.Equal(body1, body2); |
| 190 | + } |
| 191 | + |
| 192 | + [Fact(DisplayName = "Cache-TLS-008: s-maxage respected by shared cache over HTTPS")] |
| 193 | + public async Task SMaxAge_Respected_By_SharedCache() |
| 194 | + { |
| 195 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 196 | + // Use shared cache policy to honour s-maxage |
| 197 | + await using var helper = ClientHelper.CreateClient( |
| 198 | + _server.HttpsPort, |
| 199 | + new Version(1, 1), |
| 200 | + scheme: "https", |
| 201 | + configure: builder => builder.WithCache(new CachePolicy { SharedCache = true })); |
| 202 | + |
| 203 | + var request1 = new HttpRequestMessage(HttpMethod.Get, "/cache/s-maxage/3600"); |
| 204 | + var response1 = await helper.Client.SendAsync(request1, cts.Token); |
| 205 | + Assert.Equal(HttpStatusCode.OK, response1.StatusCode); |
| 206 | + var body1 = await response1.Content.ReadAsStringAsync(cts.Token); |
| 207 | + |
| 208 | + var request2 = new HttpRequestMessage(HttpMethod.Get, "/cache/s-maxage/3600"); |
| 209 | + var response2 = await helper.Client.SendAsync(request2, cts.Token); |
| 210 | + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); |
| 211 | + var body2 = await response2.Content.ReadAsStringAsync(cts.Token); |
| 212 | + |
| 213 | + Assert.Equal(body1, body2); |
| 214 | + } |
| 215 | + |
| 216 | + [Fact(DisplayName = "Cache-TLS-009: Expires header enables caching over HTTPS")] |
| 217 | + public async Task Expires_Header_Enables_Caching() |
| 218 | + { |
| 219 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 220 | + await using var helper = CreateCacheClient(); |
| 221 | + |
| 222 | + var request1 = new HttpRequestMessage(HttpMethod.Get, "/cache/expires"); |
| 223 | + var response1 = await helper.Client.SendAsync(request1, cts.Token); |
| 224 | + Assert.Equal(HttpStatusCode.OK, response1.StatusCode); |
| 225 | + var body1 = await response1.Content.ReadAsStringAsync(cts.Token); |
| 226 | + |
| 227 | + var request2 = new HttpRequestMessage(HttpMethod.Get, "/cache/expires"); |
| 228 | + var response2 = await helper.Client.SendAsync(request2, cts.Token); |
| 229 | + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); |
| 230 | + var body2 = await response2.Content.ReadAsStringAsync(cts.Token); |
| 231 | + |
| 232 | + Assert.Equal(body1, body2); |
| 233 | + } |
| 234 | + |
| 235 | + [Fact(DisplayName = "Cache-TLS-010: private response cached by private cache over HTTPS")] |
| 236 | + public async Task Private_Response_Cached_By_Private_Cache() |
| 237 | + { |
| 238 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 239 | + await using var helper = CreateCacheClient(); |
| 240 | + |
| 241 | + var request1 = new HttpRequestMessage(HttpMethod.Get, "/cache/private"); |
| 242 | + var response1 = await helper.Client.SendAsync(request1, cts.Token); |
| 243 | + Assert.Equal(HttpStatusCode.OK, response1.StatusCode); |
| 244 | + var body1 = await response1.Content.ReadAsStringAsync(cts.Token); |
| 245 | + |
| 246 | + var request2 = new HttpRequestMessage(HttpMethod.Get, "/cache/private"); |
| 247 | + var response2 = await helper.Client.SendAsync(request2, cts.Token); |
| 248 | + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); |
| 249 | + var body2 = await response2.Content.ReadAsStringAsync(cts.Token); |
| 250 | + |
| 251 | + // Private cache should still cache private responses |
| 252 | + Assert.Equal(body1, body2); |
| 253 | + } |
| 254 | + |
| 255 | + [Fact(DisplayName = "Cache-TLS-011: private response not cached by shared cache over HTTPS")] |
| 256 | + public async Task Private_Response_Not_Cached_By_Shared_Cache() |
| 257 | + { |
| 258 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 259 | + await using var helper = ClientHelper.CreateClient( |
| 260 | + _server.HttpsPort, |
| 261 | + new Version(1, 1), |
| 262 | + scheme: "https", |
| 263 | + configure: builder => builder.WithCache(new CachePolicy { SharedCache = true })); |
| 264 | + |
| 265 | + var request1 = new HttpRequestMessage(HttpMethod.Get, "/cache/private"); |
| 266 | + var response1 = await helper.Client.SendAsync(request1, cts.Token); |
| 267 | + Assert.Equal(HttpStatusCode.OK, response1.StatusCode); |
| 268 | + var body1 = await response1.Content.ReadAsStringAsync(cts.Token); |
| 269 | + |
| 270 | + // Small delay to ensure server timestamp differs |
| 271 | + await Task.Delay(TimeSpan.FromMilliseconds(50), cts.Token); |
| 272 | + |
| 273 | + var request2 = new HttpRequestMessage(HttpMethod.Get, "/cache/private"); |
| 274 | + var response2 = await helper.Client.SendAsync(request2, cts.Token); |
| 275 | + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); |
| 276 | + var body2 = await response2.Content.ReadAsStringAsync(cts.Token); |
| 277 | + |
| 278 | + // Shared cache must not cache private responses |
| 279 | + Assert.NotEqual(body1, body2); |
| 280 | + } |
| 281 | +} |
0 commit comments