|
| 1 | +using System.Net; |
| 2 | +using System.Text.Json; |
| 3 | +using TurboHTTP.IntegrationTests.Container.Shared; |
| 4 | + |
| 5 | +namespace TurboHTTP.IntegrationTests.Container.Features; |
| 6 | + |
| 7 | +public sealed class CacheFeatureSpec : FeatureSpecBase |
| 8 | +{ |
| 9 | + public CacheFeatureSpec(ServerContainerFixture server, ActorSystemFixture systemFixture) |
| 10 | + : base(server, systemFixture) { } |
| 11 | + |
| 12 | + [Theory(Timeout = 15000)] |
| 13 | + [MemberData(nameof(Protocols))] |
| 14 | + public async Task Cache_should_serve_cached_response_on_second_request(HttpProtocol protocol) |
| 15 | + { |
| 16 | + await using var helper = CreateClient(protocol, b => b.WithCache()); |
| 17 | + var ct = TestContext.Current.CancellationToken; |
| 18 | + |
| 19 | + var r1 = await helper.Client.SendAsync( |
| 20 | + new HttpRequestMessage(HttpMethod.Get, "/cache/60"), ct); |
| 21 | + Assert.Equal(HttpStatusCode.OK, r1.StatusCode); |
| 22 | + |
| 23 | + var r2 = await helper.Client.SendAsync( |
| 24 | + new HttpRequestMessage(HttpMethod.Get, "/cache/60"), ct); |
| 25 | + Assert.Equal(HttpStatusCode.OK, r2.StatusCode); |
| 26 | + } |
| 27 | + |
| 28 | + [Theory(Timeout = 15000)] |
| 29 | + [MemberData(nameof(Protocols))] |
| 30 | + public async Task Cache_should_send_if_none_match_for_etag(HttpProtocol protocol) |
| 31 | + { |
| 32 | + await using var helper = CreateClient(protocol, b => b.WithCache()); |
| 33 | + var ct = TestContext.Current.CancellationToken; |
| 34 | + |
| 35 | + var r1 = await helper.Client.SendAsync( |
| 36 | + new HttpRequestMessage(HttpMethod.Get, "/etag/test-etag"), ct); |
| 37 | + Assert.Equal(HttpStatusCode.OK, r1.StatusCode); |
| 38 | + |
| 39 | + var r2 = await helper.Client.SendAsync( |
| 40 | + new HttpRequestMessage(HttpMethod.Get, "/etag/test-etag"), ct); |
| 41 | + |
| 42 | + Assert.True( |
| 43 | + r2.StatusCode is HttpStatusCode.OK or HttpStatusCode.NotModified, |
| 44 | + $"Expected OK or 304, got {r2.StatusCode}"); |
| 45 | + } |
| 46 | + |
| 47 | + [Theory(Timeout = 15000)] |
| 48 | + [MemberData(nameof(Protocols))] |
| 49 | + public async Task Cache_should_return_fresh_response_when_cache_disabled(HttpProtocol protocol) |
| 50 | + { |
| 51 | + await using var helper = CreateClient(protocol); |
| 52 | + var ct = TestContext.Current.CancellationToken; |
| 53 | + |
| 54 | + var r1 = await helper.Client.SendAsync( |
| 55 | + new HttpRequestMessage(HttpMethod.Get, "/cache/60"), ct); |
| 56 | + var b1 = await r1.Content.ReadAsStringAsync(ct); |
| 57 | + |
| 58 | + var r2 = await helper.Client.SendAsync( |
| 59 | + new HttpRequestMessage(HttpMethod.Get, "/cache/60"), ct); |
| 60 | + var b2 = await r2.Content.ReadAsStringAsync(ct); |
| 61 | + |
| 62 | + Assert.Equal(HttpStatusCode.OK, r1.StatusCode); |
| 63 | + Assert.Equal(HttpStatusCode.OK, r2.StatusCode); |
| 64 | + } |
| 65 | + |
| 66 | + [Theory(Timeout = 15000)] |
| 67 | + [MemberData(nameof(Protocols))] |
| 68 | + public async Task Cache_should_revalidate_with_no_cache(HttpProtocol protocol) |
| 69 | + { |
| 70 | + await using var helper = CreateClient(protocol, b => b.WithCache()); |
| 71 | + var ct = TestContext.Current.CancellationToken; |
| 72 | + |
| 73 | + var r1 = await helper.Client.SendAsync( |
| 74 | + new HttpRequestMessage(HttpMethod.Get, "/cache"), ct); |
| 75 | + Assert.Equal(HttpStatusCode.OK, r1.StatusCode); |
| 76 | + |
| 77 | + var request = new HttpRequestMessage(HttpMethod.Get, "/cache"); |
| 78 | + request.Headers.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue |
| 79 | + { |
| 80 | + NoCache = true |
| 81 | + }; |
| 82 | + var r2 = await helper.Client.SendAsync(request, ct); |
| 83 | + Assert.Equal(HttpStatusCode.OK, r2.StatusCode); |
| 84 | + } |
| 85 | +} |
0 commit comments