-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite System.Net.Http.Json functional tests to use a custom HttpMes…
…sageHandler (#38733) All of the tests start a socket-based loopback server which doesn't work on WebAssembly. We can do the tests using a custom HttpMessageHandler instead of the loopback server as well.
- Loading branch information
1 parent
87919a0
commit 4c1b842
Showing
8 changed files
with
210 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/libraries/Common/tests/System/Net/Http/HttpMessageHandlerLoopbackServer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace System.Net.Test.Common | ||
{ | ||
public class HttpMessageHandlerLoopbackServer : GenericLoopbackServer | ||
{ | ||
HttpRequestMessage _request; | ||
public HttpStatusCode ResponseStatusCode; | ||
public IList<HttpHeaderData> ResponseHeaders; | ||
public string ReponseContentString; | ||
public byte[] ReponseContentBytes; | ||
|
||
private HttpMessageHandlerLoopbackServer(HttpRequestMessage request) | ||
{ | ||
_request = request; | ||
} | ||
|
||
public static async Task CreateClientAndServerAsync(Func<HttpMessageHandler, Uri, Task> clientFunc, Func<HttpMessageHandlerLoopbackServer, Task> serverFunc) | ||
{ | ||
await clientFunc(new LoopbackServerHttpMessageHandler(serverFunc), new Uri("http://example.com")).ConfigureAwait(false); | ||
} | ||
|
||
public async override Task<HttpRequestData> HandleRequestAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList<HttpHeaderData> headers = null, string content = "") | ||
{ | ||
ResponseStatusCode = statusCode; | ||
ResponseHeaders = headers; | ||
ReponseContentString = content; | ||
return await HttpRequestData.FromHttpRequestMessageAsync(_request).ConfigureAwait(false); | ||
} | ||
|
||
public async Task<HttpRequestData> HandleRequestAsync(HttpStatusCode statusCode, IList<HttpHeaderData> headers, byte[] bytes) | ||
{ | ||
ResponseStatusCode = statusCode; | ||
ResponseHeaders = headers; | ||
ReponseContentBytes = bytes; | ||
return await HttpRequestData.FromHttpRequestMessageAsync(_request).ConfigureAwait(false); | ||
} | ||
|
||
public override Task AcceptConnectionAsync(Func<GenericLoopbackConnection, Task> funcAsync) => throw new NotImplementedException(); | ||
|
||
public override Task<GenericLoopbackConnection> EstablishGenericConnectionAsync() => throw new NotImplementedException(); | ||
|
||
public override void Dispose() { } | ||
|
||
class LoopbackServerHttpMessageHandler : HttpMessageHandler | ||
{ | ||
Func<HttpMessageHandlerLoopbackServer, Task> _serverFunc; | ||
|
||
public LoopbackServerHttpMessageHandler(Func<HttpMessageHandlerLoopbackServer, Task> serverFunc) | ||
{ | ||
_serverFunc = serverFunc; | ||
} | ||
|
||
#if NETCOREAPP | ||
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
return SendAsync(request, cancellationToken).GetAwaiter().GetResult(); | ||
} | ||
#endif | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var server = new HttpMessageHandlerLoopbackServer(request); | ||
await _serverFunc(server).ConfigureAwait(false); | ||
|
||
var response = new HttpResponseMessage(server.ResponseStatusCode); | ||
if (server.ReponseContentString != null) | ||
{ | ||
response.Content = new StringContent(server.ReponseContentString); | ||
} | ||
else | ||
{ | ||
response.Content = new ByteArrayContent(server.ReponseContentBytes); | ||
} | ||
|
||
foreach (var header in server.ResponseHeaders ?? Array.Empty<HttpHeaderData>()) | ||
{ | ||
if (String.Equals(header.Name, "Content-Type", StringComparison.InvariantCultureIgnoreCase)) | ||
{ | ||
response.Content.Headers.Remove("Content-Type"); | ||
response.Content.Headers.TryAddWithoutValidation("Content-Type", header.Value); | ||
} | ||
else | ||
{ | ||
response.Headers.Add(header.Name, header.Value); | ||
} | ||
} | ||
|
||
return response; | ||
} | ||
} | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
src/libraries/System.Net.Http.Json/tests/FunctionalTests/AssemblyInfo.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.