Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 586cffc

Browse files
authored
Fix ProxyExplicitlyProvided_DefaultCredentials_Ignored test (#28770)
I couldn't get it to fail locally, but I believe the issue was that sporadically WinHttpHandler would not reuse the original connection, in which case it would either not close the first and the test would hang because nothing was accepting the second connection, or it would close the first and fail because the server was expecting a second request on the connection. This fixes it by using Connection: close on the first response so that the client won't reuse the same connection.
1 parent 133d190 commit 586cffc

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.DefaultProxyCredentials.cs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,30 @@ public async Task ProxyExplicitlyProvided_DefaultCredentials_Ignored()
5050
{
5151
var explicitProxyCreds = new NetworkCredential("rightusername", "rightpassword");
5252
var defaultSystemProxyCreds = new NetworkCredential("wrongusername", "wrongpassword");
53-
string expectCreds = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{explicitProxyCreds.UserName}:{explicitProxyCreds.Password}"));
53+
string expectCreds = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{explicitProxyCreds.UserName}:{explicitProxyCreds.Password}"));
5454

55-
await LoopbackServer.CreateServerAsync(async (proxyServer, proxyUrl) =>
55+
await LoopbackServer.CreateClientAndServerAsync(async proxyUrl =>
5656
{
5757
using (HttpClientHandler handler = CreateHttpClientHandler())
5858
using (var client = new HttpClient(handler))
5959
{
6060
handler.Proxy = new UseSpecifiedUriWebProxy(proxyUrl, explicitProxyCreds);
6161
handler.DefaultProxyCredentials = defaultSystemProxyCreds;
62-
63-
// URL does not matter. We will get response from "proxy" code bellow.
64-
Task<HttpResponseMessage> responseTask = client.GetAsync("http://notatrealserver.com/");
65-
66-
await proxyServer.AcceptConnectionAsync(async connection =>
62+
using (HttpResponseMessage response = await client.GetAsync("http://notatrealserver.com/")) // URL does not matter
6763
{
68-
List<string> headers = await connection.ReadRequestHeaderAsync();
69-
70-
if (!IsCurlHandler)
71-
{
72-
// Curl sends Basic auth without asking, other handlers wait for 407.
73-
await connection.SendResponseAsync(HttpStatusCode.ProxyAuthenticationRequired, "Proxy-Authenticate: Basic\r\n");
74-
headers = await connection.ReadRequestHeaderAsync();
75-
}
76-
77-
// Verify that we got explicitProxyCreds.
78-
Assert.Equal(expectCreds, LoopbackServer.GetRequestHeaderValue(headers, "Proxy-Authorization"));
79-
80-
Task serverTask = connection.SendResponseAsync(HttpStatusCode.OK);
81-
82-
await TestHelper.WhenAllCompletedOrAnyFailed(serverTask, responseTask);
83-
HttpResponseMessage response = responseTask.Result;
84-
8564
Assert.Equal(response.StatusCode, HttpStatusCode.OK);
86-
});
87-
};
65+
}
66+
}
67+
}, async server =>
68+
{
69+
if (!IsCurlHandler) // libcurl sends Basic auth preemptively when only basic creds are provided; other handlers wait for 407.
70+
{
71+
await server.AcceptConnectionSendResponseAndCloseAsync(
72+
HttpStatusCode.ProxyAuthenticationRequired, "Connection: close\r\nProxy-Authenticate: Basic\r\n");
73+
}
74+
75+
List<string> headers = await server.AcceptConnectionSendResponseAndCloseAsync(HttpStatusCode.OK);
76+
Assert.Equal(expectCreds, LoopbackServer.GetRequestHeaderValue(headers, "Proxy-Authorization"));
8877
});
8978
}
9079

0 commit comments

Comments
 (0)