|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http.Headers; |
| 3 | +using System.Text; |
| 4 | +using DebugProbe.AspNetCore.Handlers; |
| 5 | +using DebugProbe.AspNetCore.Models; |
| 6 | +using DebugProbe.AspNetCore.Options; |
| 7 | + |
| 8 | +namespace DebugProbe.AspNetCore.Tests.Handlers; |
| 9 | + |
| 10 | +public class DebugProbeHttpClientHandlerBodyTests |
| 11 | +{ |
| 12 | + // --------------------------------------------------------------------------- |
| 13 | + // Helpers |
| 14 | + // --------------------------------------------------------------------------- |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Builds a handler wired to the given DebugEntry and options, with a stub |
| 18 | + /// inner handler that echoes <paramref name="responseContent"/> back. |
| 19 | + /// </summary> |
| 20 | + private static (HttpClient client, DebugEntry entry) BuildClient( |
| 21 | + DebugProbeOptions options, |
| 22 | + HttpContent? responseContent = null) |
| 23 | + { |
| 24 | + var entry = new DebugEntry(); |
| 25 | + var context = new DefaultHttpContext(); |
| 26 | + context.Items["DebugProbeEntry"] = entry; |
| 27 | + |
| 28 | + var handler = new DebugProbeHttpClientHandler( |
| 29 | + new HttpContextAccessor { HttpContext = context }, |
| 30 | + options) |
| 31 | + { |
| 32 | + InnerHandler = new StubHandler(_ => |
| 33 | + new HttpResponseMessage(HttpStatusCode.OK) |
| 34 | + { |
| 35 | + Content = responseContent |
| 36 | + }) |
| 37 | + }; |
| 38 | + |
| 39 | + return (new HttpClient(handler), entry); |
| 40 | + } |
| 41 | + |
| 42 | + // --------------------------------------------------------------------------- |
| 43 | + // Test cases |
| 44 | + // --------------------------------------------------------------------------- |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task Body_under_limit_is_captured_without_truncation_marker() |
| 48 | + { |
| 49 | + const int limitKb = 1; // 1 KB limit |
| 50 | + var bodyText = new string('A', 100); // 100 bytes — well under 1 KB |
| 51 | + |
| 52 | + var options = new DebugProbeOptions { MaxBodyCaptureSizeKb = limitKb }; |
| 53 | + var (client, entry) = BuildClient(options, |
| 54 | + responseContent: new StringContent(bodyText, Encoding.UTF8, "text/plain")); |
| 55 | + |
| 56 | + await client.GetAsync("https://example.test/"); |
| 57 | + |
| 58 | + var outgoing = Assert.Single(entry.OutgoingRequests); |
| 59 | + Assert.DoesNotContain("[truncated]", outgoing.ResponseBody); |
| 60 | + Assert.Contains(bodyText, outgoing.ResponseBody); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task Body_exactly_at_limit_is_captured_without_truncation_marker() |
| 65 | + { |
| 66 | + const int limitKb = 1; |
| 67 | + var bodyText = new string('B', 1024); // exactly 1 KB |
| 68 | + |
| 69 | + var options = new DebugProbeOptions { MaxBodyCaptureSizeKb = limitKb }; |
| 70 | + var (client, entry) = BuildClient(options, |
| 71 | + responseContent: new StringContent(bodyText, Encoding.UTF8, "text/plain")); |
| 72 | + |
| 73 | + await client.GetAsync("https://example.test/"); |
| 74 | + |
| 75 | + var outgoing = Assert.Single(entry.OutgoingRequests); |
| 76 | + Assert.DoesNotContain("[truncated]", outgoing.ResponseBody); |
| 77 | + Assert.Contains(bodyText, outgoing.ResponseBody); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public async Task Body_over_limit_is_truncated_and_marker_is_appended() |
| 82 | + { |
| 83 | + const int limitKb = 1; |
| 84 | + var bodyText = new string('C', 2048); // 2 KB — double the limit |
| 85 | + |
| 86 | + var options = new DebugProbeOptions { MaxBodyCaptureSizeKb = limitKb }; |
| 87 | + var (client, entry) = BuildClient(options, |
| 88 | + responseContent: new StringContent(bodyText, Encoding.UTF8, "text/plain")); |
| 89 | + |
| 90 | + await client.GetAsync("https://example.test/"); |
| 91 | + |
| 92 | + var outgoing = Assert.Single(entry.OutgoingRequests); |
| 93 | + Assert.EndsWith("[truncated]", outgoing.ResponseBody); |
| 94 | + // Captured prefix must be exactly the limit (1024 'C' chars) |
| 95 | + Assert.StartsWith(new string('C', 1024), outgoing.ResponseBody); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public async Task Capturing_streamed_response_body_does_not_consume_body_for_caller() |
| 100 | + { |
| 101 | + const int limitKb = 1; |
| 102 | + var bodyText = new string('D', 2048); |
| 103 | + var responseContent = new StreamContent(new NonSeekableMemoryStream(Encoding.UTF8.GetBytes(bodyText))); |
| 104 | + responseContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain") |
| 105 | + { |
| 106 | + CharSet = Encoding.UTF8.WebName |
| 107 | + }; |
| 108 | + |
| 109 | + var options = new DebugProbeOptions { MaxBodyCaptureSizeKb = limitKb }; |
| 110 | + var (client, entry) = BuildClient(options, responseContent); |
| 111 | + |
| 112 | + using var response = await client.GetAsync("https://example.test/"); |
| 113 | + |
| 114 | + Assert.Equal(bodyText, await response.Content.ReadAsStringAsync()); |
| 115 | + |
| 116 | + var outgoing = Assert.Single(entry.OutgoingRequests); |
| 117 | + Assert.EndsWith("[truncated]", outgoing.ResponseBody); |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public async Task Captured_body_uses_content_type_charset() |
| 122 | + { |
| 123 | + const string bodyText = "Hello \u0100"; |
| 124 | + var responseContent = new StringContent(bodyText, Encoding.Unicode, "text/plain"); |
| 125 | + |
| 126 | + var options = new DebugProbeOptions { MaxBodyCaptureSizeKb = 1 }; |
| 127 | + var (client, entry) = BuildClient(options, responseContent); |
| 128 | + |
| 129 | + using var response = await client.GetAsync("https://example.test/"); |
| 130 | + |
| 131 | + Assert.Equal(bodyText, await response.Content.ReadAsStringAsync()); |
| 132 | + |
| 133 | + var outgoing = Assert.Single(entry.OutgoingRequests); |
| 134 | + Assert.Contains(bodyText, outgoing.ResponseBody); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact] |
| 138 | + public async Task Null_content_returns_empty_string() |
| 139 | + { |
| 140 | + var options = new DebugProbeOptions(); |
| 141 | + var (client, entry) = BuildClient(options, responseContent: null); |
| 142 | + |
| 143 | + await client.GetAsync("https://example.test/"); |
| 144 | + |
| 145 | + var outgoing = Assert.Single(entry.OutgoingRequests); |
| 146 | + Assert.Equal(string.Empty, outgoing.ResponseBody); |
| 147 | + } |
| 148 | + |
| 149 | + [Fact] |
| 150 | + public async Task Non_text_content_returns_empty_string() |
| 151 | + { |
| 152 | + var binaryContent = new ByteArrayContent([0x89, 0x50, 0x4E, 0x47]); // PNG header bytes |
| 153 | + binaryContent.Headers.ContentType = |
| 154 | + new System.Net.Http.Headers.MediaTypeHeaderValue("image/png"); |
| 155 | + |
| 156 | + var options = new DebugProbeOptions(); |
| 157 | + var (client, entry) = BuildClient(options, responseContent: binaryContent); |
| 158 | + |
| 159 | + await client.GetAsync("https://example.test/"); |
| 160 | + |
| 161 | + var outgoing = Assert.Single(entry.OutgoingRequests); |
| 162 | + Assert.Equal(string.Empty, outgoing.ResponseBody); |
| 163 | + } |
| 164 | + |
| 165 | + // --------------------------------------------------------------------------- |
| 166 | + // Stub inner handler |
| 167 | + // --------------------------------------------------------------------------- |
| 168 | + |
| 169 | + private sealed class StubHandler(Func<HttpRequestMessage, HttpResponseMessage> send) : HttpMessageHandler |
| 170 | + { |
| 171 | + protected override Task<HttpResponseMessage> SendAsync( |
| 172 | + HttpRequestMessage request, CancellationToken cancellationToken) |
| 173 | + => Task.FromResult(send(request)); |
| 174 | + } |
| 175 | + |
| 176 | + private sealed class NonSeekableMemoryStream(byte[] buffer) : MemoryStream(buffer) |
| 177 | + { |
| 178 | + public override bool CanSeek => false; |
| 179 | + } |
| 180 | +} |
0 commit comments