Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Net.Http.Headers;
using System.Security.Authentication.ExtendedProtection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -405,22 +404,20 @@ public void Realm_SetDisposed_ThrowsObjectDisposedException()
public async Task<HttpResponseMessage> AuthenticationFailure(HttpClient client, HttpStatusCode errorCode)
{
Task<HttpResponseMessage> clientTask = client.GetAsync(_factory.ListeningUrl);

// The server task will hang forever if it is not cancelled.
var tokenSource = new CancellationTokenSource();
Task<HttpListenerContext> serverTask = Task.Run(() => _listener.GetContext(), tokenSource.Token);
Task<HttpListenerContext> serverTask = _listener.GetContextAsync();

Task resultTask = await Task.WhenAny(clientTask, serverTask);
tokenSource.Cancel();
if (resultTask == serverTask)
{
await serverTask;
}

Assert.Same(clientTask, resultTask);

Assert.Equal(errorCode, clientTask.Result.StatusCode);
return clientTask.Result;
HttpResponseMessage response = await clientTask;
Assert.Equal(errorCode, response.StatusCode);

return response;
}

public async Task<HttpResponseMessage> AuthenticationFailureAsyncContext(HttpClient client, HttpStatusCode errorCode)
Expand All @@ -436,8 +433,10 @@ public async Task<HttpResponseMessage> AuthenticationFailureAsyncContext(HttpCli

Assert.Same(clientTask, resultTask);

Assert.Equal(errorCode, clientTask.Result.StatusCode);
return clientTask.Result;
HttpResponseMessage response = await clientTask;
Assert.Equal(errorCode, response.StatusCode);

return response;
}

private async Task ValidateNullUser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private async Task GetSocketContext(string[] headers, Func<HttpListenerContext,
{
using (Socket client = Factory.GetConnectedSocket())
{
client.Send(Factory.GetContent("1.1", "POST", null, "Text", headers, true));
await client.SendAsync(Factory.GetContent("1.1", "POST", null, "Text", headers, true));

HttpListener listener = Factory.GetListener();
await contextAction(await listener.GetContextAsync());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public HttpListenerRequestTests()

public void Dispose()
{
Factory?.Dispose();
Client?.Dispose();
Factory?.Dispose();
}

[Theory]
Expand Down Expand Up @@ -271,7 +271,7 @@ public async Task GetClientCertificate_NoCertificate_ReturnsNull()
public async Task GetClientCertificateAsync_NoCertificate_ReturnsNull()
{
HttpListenerRequest request = await GetRequest("POST", null, null);
Assert.Null(request.GetClientCertificateAsync().Result);
Assert.Null(await request.GetClientCertificateAsync());
}

[Fact]
Expand Down Expand Up @@ -602,7 +602,7 @@ public async Task Headers_Get_ReturnsExpected(string[] headers, WebHeaderCollect

private async Task<HttpListenerRequest> GetRequest(string requestType, string query, string[] headers, string content = "Text\r\n", string httpVersion = "1.1")
{
Client.Send(Factory.GetContent(httpVersion, requestType, query, content, headers, true));
await Client.SendAsync(Factory.GetContent(httpVersion, requestType, query, content, headers, true));

HttpListener listener = Factory.GetListener();
return (await listener.GetContextAsync()).Request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected string GetClientResponse(int expectedLength)

protected async Task<HttpListenerResponse> GetResponse(string httpVersion = "1.1")
{
Client.Send(Factory.GetContent(httpVersion, "POST", null, "Give me a context, please", null, headerOnly: false));
await Client.SendAsync(Factory.GetContent(httpVersion, "POST", null, "Give me a context, please", null, headerOnly: false));
HttpListenerContext context = await Factory.GetListener().GetContextAsync();
return context.Response;
}
Expand Down Expand Up @@ -157,7 +157,7 @@ public async Task Redirect_Disposed_ThrowsObjectDisposedException()
[ConditionalFact(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementation))] // [ActiveIssue("https://github.com/dotnet/runtime/issues/21808", TestPlatforms.AnyUnix)]
public async Task Abort_Invoke_ForciblyTerminatesConnection()
{
Client.Send(Factory.GetContent("1.1", "POST", null, "Give me a context, please", null, headerOnly: false));
await Client.SendAsync(Factory.GetContent("1.1", "POST", null, "Give me a context, please", null, headerOnly: false));
HttpListenerContext context = await Factory.GetListener().GetContextAsync();
HttpListenerResponse response = context.Response;
Stream outputStream = response.OutputStream;
Expand Down Expand Up @@ -415,7 +415,7 @@ public async Task CloseResponseEntity_SendToClosedConnection_DoesNotThrow(bool w
using (Socket client = factory.GetConnectedSocket())
{
// Send a header to the HttpListener to give it a context.
client.Send(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
await client.SendAsync(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
HttpListener listener = factory.GetListener();
HttpListenerContext context = await listener.GetContextAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ public async Task Read_FromClosedConnectionAsynchronously_ThrowsHttpListenerExce
// If the content is missing, then the HttpListener needs
// to get the content. However, the socket has been closed
// before the reading of the content, so reading should fail.
client.Send(_factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
await client.SendAsync(_factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
HttpListenerContext context = await _listener.GetContextAsync();

// Disconnect the Socket from the HttpListener.
Expand All @@ -689,7 +689,7 @@ public async Task Read_FromClosedConnectionSynchronously_ThrowsHttpListenerExcep
// If the content is missing, then the HttpListener needs
// to get the content. However, the socket has been closed
// before the reading of the content, so reading should fail.
client.Send(_factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
await client.SendAsync(_factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
HttpListenerContext context = await _listener.GetContextAsync();

// Disconnect the Socket from the HttpListener.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public async Task Write_HeadersToClosedConnectionAsynchronously_ThrowsHttpListen
using (Socket client = factory.GetConnectedSocket())
{
// Send a header to the HttpListener to give it a context.
client.Send(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
await client.SendAsync(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
HttpListener listener = factory.GetListener();
listener.IgnoreWriteExceptions = ignoreWriteExceptions;
HttpListenerContext context = await listener.GetContextAsync();
Expand Down Expand Up @@ -428,7 +428,7 @@ public async Task Write_HeadersToClosedConnectionSynchronously_ThrowsHttpListene
using (Socket client = factory.GetConnectedSocket())
{
// Send a header to the HttpListener to give it a context.
client.Send(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
await client.SendAsync(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
HttpListener listener = factory.GetListener();
listener.IgnoreWriteExceptions = ignoreWriteExceptions;
HttpListenerContext context = await listener.GetContextAsync();
Expand Down Expand Up @@ -468,7 +468,7 @@ public async Task Write_ContentToClosedConnectionAsynchronously_ThrowsHttpListen
using (Socket client = factory.GetConnectedSocket())
{
// Send a header to the HttpListener to give it a context.
client.Send(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
await client.SendAsync(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
HttpListener listener = factory.GetListener();
listener.IgnoreWriteExceptions = ignoreWriteExceptions;
HttpListenerContext context = await listener.GetContextAsync();
Expand Down Expand Up @@ -508,7 +508,7 @@ public async Task Write_ContentToClosedConnectionSynchronously_ThrowsHttpListene
using (Socket client = factory.GetConnectedSocket())
{
// Send a header to the HttpListener to give it a context.
client.Send(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
await client.SendAsync(factory.GetContent(RequestTypes.POST, Text, headerOnly: true));
HttpListener listener = factory.GetListener();
listener.IgnoreWriteExceptions = ignoreWriteExceptions;
HttpListenerContext context = await listener.GetContextAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,17 @@ public async Task GetContext_InvalidRequest_DoesNotGetContext()
string fullRequest = $"{requestLineWithPathAndQuery}\r\n{host}{string.Join("\r\n", headers ?? new string[0])}{content}\r\n";

Task<HttpListenerContext> serverTask = Factory.GetListener().GetContextAsync();
client.Send(Encoding.Default.GetBytes(fullRequest));
await client.SendAsync(Encoding.Default.GetBytes(fullRequest));

byte[] errorMessageBytes = new byte[512];
Task<int> clientTask = Task.Run(() => client.Receive(errorMessageBytes));
Task<int> clientTask = client.ReceiveAsync(errorMessageBytes);

Task completedTask = await Task.WhenAny(clientTask, serverTask);

// Ignore the specific error message - just make sure that this failed.
Assert.Same(clientTask, completedTask);
string errorMessage = Encoding.Default.GetString(errorMessageBytes, 0, clientTask.Result);
int bytesReceived = await clientTask;
string errorMessage = Encoding.Default.GetString(errorMessageBytes, 0, bytesReceived);
Assert.Contains(expectedMessage, errorMessage);

Assert.False(serverTask.IsCompleted, $"Server task was completed: {serverTask.Status}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@

using Xunit;

// [ActiveIssue("https://github.com/dotnet/runtime/issues/21870")]: Disabling parallel execution of HttpListener tests
// until all of the hangs can be addressed
[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly, DisableTestParallelization = true, MaxParallelThreads = 1)]
[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)]
Loading