|
| 1 | +using System.Net; |
| 2 | +using DebugProbe.AspNetCore.Tests.Infrastructure; |
| 3 | +using Microsoft.AspNetCore.Http; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | + |
| 6 | +namespace DebugProbe.AspNetCore.Tests.Handlers; |
| 7 | + |
| 8 | +public class OutgoingHttpClientCaptureOptionsTests |
| 9 | +{ |
| 10 | + [Fact] |
| 11 | + public async Task Disabled_outgoing_capture_does_not_store_outgoing_traces() |
| 12 | + { |
| 13 | + await using var app = await DebugProbeTestApp.CreateAsync( |
| 14 | + endpoints => |
| 15 | + { |
| 16 | + endpoints.MapGet("/proxy", async (IHttpClientFactory httpClientFactory) => |
| 17 | + { |
| 18 | + var client = httpClientFactory.CreateClient("outgoing"); |
| 19 | + var body = await client.GetStringAsync("https://api.example.test/ping"); |
| 20 | + |
| 21 | + return Results.Text(body); |
| 22 | + }); |
| 23 | + }, |
| 24 | + configureOptions: options => options.CaptureOutgoingHttpClientRequests = false, |
| 25 | + configureServices: services => |
| 26 | + { |
| 27 | + services.AddHttpClient("outgoing") |
| 28 | + .ConfigurePrimaryHttpMessageHandler(() => |
| 29 | + new StubHandler(_ => new HttpResponseMessage(HttpStatusCode.OK) |
| 30 | + { |
| 31 | + Content = new StringContent("pong") |
| 32 | + })); |
| 33 | + }); |
| 34 | + |
| 35 | + var response = await app.Client.GetAsync("/proxy"); |
| 36 | + |
| 37 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 38 | + Assert.Equal("pong", await response.Content.ReadAsStringAsync()); |
| 39 | + |
| 40 | + var entry = app.SingleEntry; |
| 41 | + Assert.Equal("GET", entry.Method); |
| 42 | + Assert.Equal("/proxy", entry.Path); |
| 43 | + Assert.Empty(entry.OutgoingRequests); |
| 44 | + } |
| 45 | + |
| 46 | + private sealed class StubHandler(Func<HttpRequestMessage, HttpResponseMessage> send) : HttpMessageHandler |
| 47 | + { |
| 48 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 49 | + { |
| 50 | + return Task.FromResult(send(request)); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments