|
| 1 | +namespace Yllibed.HttpServer.Tests; |
| 2 | + |
| 3 | +using Yllibed.HttpServer.Handlers; |
| 4 | + |
| 5 | +[TestClass] |
| 6 | +public class GuardHandlerFixture : FixtureBase |
| 7 | +{ |
| 8 | + [TestMethod] |
| 9 | + public async Task GuardHandler_UrlTooLong_Returns414() |
| 10 | + { |
| 11 | + using var sut = new Server(); |
| 12 | + // Very small limit for test |
| 13 | + sut.RegisterHandler(new GuardHandler(maxUrlLength: 10)); |
| 14 | + sut.RegisterHandler(new StaticHandler("ok", "text/plain", "OK")); |
| 15 | + |
| 16 | + var (uri4, _) = sut.Start(); |
| 17 | + var longPath = new string('a', 50); |
| 18 | + var requestUri = new Uri(uri4, $"{longPath}"); |
| 19 | + |
| 20 | + using var client = new HttpClient(); |
| 21 | + var response = await client.GetAsync(requestUri, CT).ConfigureAwait(false); |
| 22 | + response.StatusCode.Should().Be(HttpStatusCode.RequestUriTooLong); |
| 23 | + } |
| 24 | + |
| 25 | + [TestMethod] |
| 26 | + public async Task GuardHandler_ContentLengthTooLarge_Returns413() |
| 27 | + { |
| 28 | + using var sut = new Server(); |
| 29 | + // Limit to 16 bytes |
| 30 | + sut.RegisterHandler(new GuardHandler(maxBodyBytes: 16)); |
| 31 | + sut.RegisterHandler(new StaticHandler("ok", "text/plain", "OK")); |
| 32 | + |
| 33 | + var (uri4, _) = sut.Start(); |
| 34 | + var requestUri = new Uri(uri4, "ok"); |
| 35 | + |
| 36 | + using var client = new HttpClient(); |
| 37 | + var content = new string('x', 100); // > 16 bytes |
| 38 | + var response = await client.PostAsync(requestUri, new StringContent(content), CT).ConfigureAwait(false); |
| 39 | + response.StatusCode.Should().Be(HttpStatusCode.RequestEntityTooLarge); |
| 40 | + } |
| 41 | + |
| 42 | + [TestMethod] |
| 43 | + public async Task GuardHandler_HeadersTooLarge_Returns431() |
| 44 | + { |
| 45 | + using var sut = new Server(); |
| 46 | + // Keep overall header size tiny to force 431 |
| 47 | + sut.RegisterHandler(new GuardHandler(maxHeadersTotalSize: 100)); |
| 48 | + sut.RegisterHandler(new StaticHandler("ok", "text/plain", "OK")); |
| 49 | + |
| 50 | + var (uri4, _) = sut.Start(); |
| 51 | + var requestUri = new Uri(uri4, "ok"); |
| 52 | + |
| 53 | + using var client = new HttpClient(); |
| 54 | + var msg = new HttpRequestMessage(HttpMethod.Get, requestUri); |
| 55 | + msg.Headers.Add("X-Long", new string('z', 1024)); |
| 56 | + var response = await client.SendAsync(msg, CT).ConfigureAwait(false); |
| 57 | + response.StatusCode.Should().Be((HttpStatusCode)431); |
| 58 | + } |
| 59 | + [TestMethod] |
| 60 | + public async Task GuardHandler_MethodNotAllowed_Returns405() |
| 61 | + { |
| 62 | + using var sut = new Server(); |
| 63 | + sut.RegisterHandler(new GuardHandler(allowedMethods: new[] { "GET" })); |
| 64 | + sut.RegisterHandler(new StaticHandler("/ok", "text/plain", "OK")); |
| 65 | + |
| 66 | + var (uri4, _) = sut.Start(); |
| 67 | + var requestUri = new Uri(uri4, "/ok"); |
| 68 | + |
| 69 | + using var client = new HttpClient(); |
| 70 | + var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri), CT).ConfigureAwait(false); |
| 71 | + response.StatusCode.Should().Be(HttpStatusCode.MethodNotAllowed); |
| 72 | + } |
| 73 | + |
| 74 | + [TestMethod] |
| 75 | + public async Task GuardHandler_ForbiddenHost_Returns403() |
| 76 | + { |
| 77 | + using var sut = new Server(); |
| 78 | + sut.RegisterHandler(new GuardHandler(allowedHosts: new[] { "localhost" })); |
| 79 | + sut.RegisterHandler(new StaticHandler("/ok", "text/plain", "OK")); |
| 80 | + |
| 81 | + var (uri4, _) = sut.Start(); |
| 82 | + var requestUri = new Uri(uri4, "/ok"); |
| 83 | + |
| 84 | + using var client = new HttpClient(); |
| 85 | + var msg = new HttpRequestMessage(HttpMethod.Get, requestUri); |
| 86 | + // Force a different Host header value to trigger 403: use machine name if not localhost |
| 87 | + msg.Headers.Host = "example.com"; |
| 88 | + var response = await client.SendAsync(msg, CT).ConfigureAwait(false); |
| 89 | + response.StatusCode.Should().Be(HttpStatusCode.Forbidden); |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public async Task GuardHandler_TooManyHeaders_Returns431() |
| 94 | + { |
| 95 | + using var sut = new Server(); |
| 96 | + // Set very low header count limit to trigger 431 (Host header alone is 1; we'll add another) |
| 97 | + sut.RegisterHandler(new GuardHandler(maxHeadersCount: 1)); |
| 98 | + sut.RegisterHandler(new StaticHandler("/ok", "text/plain", "OK")); |
| 99 | + |
| 100 | + var (uri4, _) = sut.Start(); |
| 101 | + var requestUri = new Uri(uri4, "/ok"); |
| 102 | + |
| 103 | + using var client = new HttpClient(); |
| 104 | + var msg = new HttpRequestMessage(HttpMethod.Get, requestUri); |
| 105 | + msg.Headers.Add("X-One", "1"); |
| 106 | + var response = await client.SendAsync(msg, CT).ConfigureAwait(false); |
| 107 | + response.StatusCode.Should().Be((HttpStatusCode)431); |
| 108 | + } |
| 109 | + |
| 110 | + [TestMethod] |
| 111 | + public async Task GuardHandler_PassesThrough_ToNextHandler_WhenValid() |
| 112 | + { |
| 113 | + using var sut = new Server(); |
| 114 | + sut.RegisterHandler(new GuardHandler(allowedMethods: new[] { "GET" })); |
| 115 | + sut.RegisterHandler(new StaticHandler("/ok", "text/plain", "OK")); |
| 116 | + |
| 117 | + var (uri4, _) = sut.Start(); |
| 118 | + var requestUri = new Uri(uri4, "/ok"); |
| 119 | + |
| 120 | + using var client = new HttpClient(); |
| 121 | + var response = await client.GetAsync(requestUri, CT).ConfigureAwait(false); |
| 122 | + response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 123 | + (await response.Content.ReadAsStringAsync(CT).ConfigureAwait(false)).Should().Be("OK"); |
| 124 | + } |
| 125 | + |
| 126 | + [TestMethod] |
| 127 | + public async Task GuardHandler_AsWrapper_CallsInnerOnlyOnPass() |
| 128 | + { |
| 129 | + using var sut = new Server(); |
| 130 | + var inner = new StaticHandler("/ok", "text/plain", "OK"); |
| 131 | + var guard = new GuardHandler(allowedMethods: new[] { "GET" }, inner: inner); |
| 132 | + sut.RegisterHandler(guard); |
| 133 | + |
| 134 | + var (uri4, _) = sut.Start(); |
| 135 | + var requestUri = new Uri(uri4, "/ok"); |
| 136 | + |
| 137 | + using var client = new HttpClient(); |
| 138 | + // DELETE should be blocked by protection and inner should not run |
| 139 | + var blocked = await client.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri), CT).ConfigureAwait(false); |
| 140 | + blocked.StatusCode.Should().Be(HttpStatusCode.MethodNotAllowed); |
| 141 | + |
| 142 | + // GET should pass and inner returns OK |
| 143 | + var allowed = await client.GetAsync(requestUri, CT).ConfigureAwait(false); |
| 144 | + allowed.StatusCode.Should().Be(HttpStatusCode.OK); |
| 145 | + } |
| 146 | +} |
0 commit comments