|
| 1 | +using System.Net; |
| 2 | +using TurboHttp.IntegrationTests.Shared; |
| 3 | + |
| 4 | +namespace TurboHttp.IntegrationTests.TLS; |
| 5 | + |
| 6 | +[Collection("TLS")] |
| 7 | +public sealed class CompressionIntegrationTests |
| 8 | +{ |
| 9 | + private readonly ServerFixture _server; |
| 10 | + private readonly ActorSystemFixture _systemFixture; |
| 11 | + |
| 12 | + public CompressionIntegrationTests(ServerFixture server, ActorSystemFixture systemFixture) |
| 13 | + { |
| 14 | + _server = server; |
| 15 | + _systemFixture = systemFixture; |
| 16 | + } |
| 17 | + |
| 18 | + private ClientHelper CreateClient() |
| 19 | + { |
| 20 | + return ClientHelper.CreateClient( |
| 21 | + _server.HttpsPort, |
| 22 | + new Version(1, 1), |
| 23 | + scheme: "https", |
| 24 | + system: _systemFixture.System); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact(DisplayName = "Compression-TLS-001: gzip response transparently decompressed over HTTPS")] |
| 28 | + public async Task Gzip_Response_Transparently_Decompressed_Over_Https() |
| 29 | + { |
| 30 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 31 | + await using var helper = CreateClient(); |
| 32 | + |
| 33 | + var request = new HttpRequestMessage(HttpMethod.Get, "/compress/gzip/4"); |
| 34 | + var response = await helper.Client.SendAsync(request, cts.Token); |
| 35 | + |
| 36 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 37 | + var body = await response.Content.ReadAsByteArrayAsync(cts.Token); |
| 38 | + Assert.Equal(4 * 1024, body.Length); |
| 39 | + |
| 40 | + for (var i = 0; i < body.Length; i++) |
| 41 | + { |
| 42 | + Assert.Equal((byte)('A' + i % 26), body[i]); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + [Fact(DisplayName = "Compression-TLS-002: deflate response transparently decompressed over HTTPS")] |
| 47 | + public async Task Deflate_Response_Transparently_Decompressed_Over_Https() |
| 48 | + { |
| 49 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 50 | + await using var helper = CreateClient(); |
| 51 | + |
| 52 | + var request = new HttpRequestMessage(HttpMethod.Get, "/compress/deflate/2"); |
| 53 | + var response = await helper.Client.SendAsync(request, cts.Token); |
| 54 | + |
| 55 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 56 | + var body = await response.Content.ReadAsByteArrayAsync(cts.Token); |
| 57 | + Assert.Equal(2 * 1024, body.Length); |
| 58 | + |
| 59 | + for (var i = 0; i < body.Length; i++) |
| 60 | + { |
| 61 | + Assert.Equal((byte)('A' + i % 26), body[i]); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + [Fact(DisplayName = "Compression-TLS-003: brotli response transparently decompressed over HTTPS")] |
| 66 | + public async Task Brotli_Response_Transparently_Decompressed_Over_Https() |
| 67 | + { |
| 68 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 69 | + await using var helper = CreateClient(); |
| 70 | + |
| 71 | + var request = new HttpRequestMessage(HttpMethod.Get, "/compress/br/3"); |
| 72 | + var response = await helper.Client.SendAsync(request, cts.Token); |
| 73 | + |
| 74 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 75 | + var body = await response.Content.ReadAsByteArrayAsync(cts.Token); |
| 76 | + Assert.Equal(3 * 1024, body.Length); |
| 77 | + |
| 78 | + for (var i = 0; i < body.Length; i++) |
| 79 | + { |
| 80 | + Assert.Equal((byte)('A' + i % 26), body[i]); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + [Fact(DisplayName = "Compression-TLS-004: identity encoding passes through unchanged over HTTPS")] |
| 85 | + public async Task Identity_Encoding_Passes_Through_Unchanged_Over_Https() |
| 86 | + { |
| 87 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 88 | + await using var helper = CreateClient(); |
| 89 | + |
| 90 | + var request = new HttpRequestMessage(HttpMethod.Get, "/compress/identity/1"); |
| 91 | + var response = await helper.Client.SendAsync(request, cts.Token); |
| 92 | + |
| 93 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 94 | + var body = await response.Content.ReadAsByteArrayAsync(cts.Token); |
| 95 | + Assert.Equal(1 * 1024, body.Length); |
| 96 | + |
| 97 | + for (var i = 0; i < body.Length; i++) |
| 98 | + { |
| 99 | + Assert.Equal((byte)('A' + i % 26), body[i]); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [Fact(DisplayName = "Compression-TLS-005: content negotiation with Accept-Encoding gzip returns gzip over HTTPS")] |
| 104 | + public async Task Negotiate_AcceptEncoding_Gzip_Over_Https() |
| 105 | + { |
| 106 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 107 | + await using var helper = CreateClient(); |
| 108 | + |
| 109 | + var request = new HttpRequestMessage(HttpMethod.Get, "/compress/negotiate"); |
| 110 | + request.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip"); |
| 111 | + var response = await helper.Client.SendAsync(request, cts.Token); |
| 112 | + |
| 113 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 114 | + var body = await response.Content.ReadAsByteArrayAsync(cts.Token); |
| 115 | + |
| 116 | + Assert.Equal(1024, body.Length); |
| 117 | + |
| 118 | + for (var i = 0; i < body.Length; i++) |
| 119 | + { |
| 120 | + Assert.Equal((byte)('A' + i % 26), body[i]); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + [Fact(DisplayName = "Compression-TLS-006: content negotiation with Accept-Encoding br returns brotli over HTTPS")] |
| 125 | + public async Task Negotiate_AcceptEncoding_Brotli_Over_Https() |
| 126 | + { |
| 127 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 128 | + await using var helper = CreateClient(); |
| 129 | + |
| 130 | + var request = new HttpRequestMessage(HttpMethod.Get, "/compress/negotiate"); |
| 131 | + request.Headers.TryAddWithoutValidation("Accept-Encoding", "br"); |
| 132 | + var response = await helper.Client.SendAsync(request, cts.Token); |
| 133 | + |
| 134 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 135 | + var body = await response.Content.ReadAsByteArrayAsync(cts.Token); |
| 136 | + |
| 137 | + Assert.Equal(1024, body.Length); |
| 138 | + |
| 139 | + for (var i = 0; i < body.Length; i++) |
| 140 | + { |
| 141 | + Assert.Equal((byte)('A' + i % 26), body[i]); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + [Fact(DisplayName = "Compression-TLS-007: content negotiation with no Accept-Encoding returns identity over HTTPS")] |
| 146 | + public async Task Negotiate_NoAcceptEncoding_Returns_Identity_Over_Https() |
| 147 | + { |
| 148 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 149 | + await using var helper = CreateClient(); |
| 150 | + |
| 151 | + var request = new HttpRequestMessage(HttpMethod.Get, "/compress/negotiate"); |
| 152 | + request.Headers.Remove("Accept-Encoding"); |
| 153 | + var response = await helper.Client.SendAsync(request, cts.Token); |
| 154 | + |
| 155 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 156 | + var body = await response.Content.ReadAsByteArrayAsync(cts.Token); |
| 157 | + |
| 158 | + Assert.Equal(1024, body.Length); |
| 159 | + |
| 160 | + for (var i = 0; i < body.Length; i++) |
| 161 | + { |
| 162 | + Assert.Equal((byte)('A' + i % 26), body[i]); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments