Skip to content

Commit b331f23

Browse files
authored
Fix draining response in metrics test (#93379)
1 parent 0a77fd4 commit b331f23

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected static class InstrumentNames
3030
public const string ConnectionDuration = "http.client.connection.duration";
3131
public const string TimeInQueue = "http.client.request.time_in_queue";
3232
}
33-
33+
3434
protected HttpMetricsTestBase(ITestOutputHelper output) : base(output)
3535
{
3636
}
@@ -347,7 +347,7 @@ public Task RequestDuration_CustomTags_Recorded()
347347
{
348348
ctx.AddCustomTag("route", "/test");
349349
});
350-
350+
351351
using HttpResponseMessage response = await SendAsync(client, request);
352352

353353
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
@@ -455,6 +455,21 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
455455
using InstrumentRecorder<double> recorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
456456
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
457457
using HttpResponseMessage response = await client.SendAsync(TestAsync, request, completionOption);
458+
string responseContent = await response.Content.ReadAsStringAsync();
459+
460+
if (responseContentType == ResponseContentType.ContentLength)
461+
{
462+
Assert.NotNull(response.Content.Headers.ContentLength);
463+
}
464+
else if (responseContentType == ResponseContentType.TransferEncodingChunked)
465+
{
466+
Assert.NotNull(response.Headers.TransferEncodingChunked);
467+
}
468+
else
469+
{
470+
// Empty
471+
Assert.Empty(responseContent);
472+
}
458473

459474
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
460475
VerifyRequestDuration(m, uri, UseVersion, 200); ;
@@ -790,7 +805,7 @@ await Assert.ThrowsAsync<HttpRequestException>(async () =>
790805
using HttpResponseMessage response = await SendAsync(client, request);
791806
});
792807
}
793-
808+
794809
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
795810
VerifyRequestDuration(m, uri, UseVersion, 200);
796811
Assert.Equal("before!", m.Tags.ToArray().Single(t => t.Key == "before").Value);

0 commit comments

Comments
 (0)