-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Rewrite System.Net.Http.Json functional tests to use a custom HttpMessageHandler #38733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0b54d70
ffb5084
4140891
25d60de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
akoeplinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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; | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,14 +25,13 @@ private static readonly JsonSerializerOptions s_defaultSerializerOptions | |
public async Task TestGetFromJsonAsync() | ||
{ | ||
const string json = @"{""Name"":""David"",""Age"":24}"; | ||
const int NumRequests = 4; | ||
HttpHeaderData header = new HttpHeaderData("Content-Type", "application/json"); | ||
List<HttpHeaderData> headers = new List<HttpHeaderData> { header }; | ||
|
||
await LoopbackServer.CreateClientAndServerAsync( | ||
async uri => | ||
await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync( | ||
async (handler, uri) => | ||
{ | ||
using (HttpClient client = new HttpClient()) | ||
using (HttpClient client = new HttpClient(handler)) | ||
{ | ||
Person per = (Person)await client.GetFromJsonAsync(uri, typeof(Person)); | ||
per.Validate(); | ||
|
@@ -47,45 +46,31 @@ await LoopbackServer.CreateClientAndServerAsync( | |
per.Validate(); | ||
} | ||
}, | ||
async server => | ||
{ | ||
for (int i = 0; i < NumRequests; i++) | ||
{ | ||
await server.HandleRequestAsync(content: json, headers: headers); | ||
} | ||
Comment on lines
-52
to
-55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is that you only have to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jozkee In the previous implementation we actually had to create four responses from the socket-based loopback server, one for each request since the callback was only invoked once. Now we invoke the callback for each request so this is no longer necessary. |
||
}); | ||
server => server.HandleRequestAsync(content: json, headers: headers)); | ||
} | ||
|
||
[Fact] | ||
public async Task TestGetFromJsonAsyncUnsuccessfulResponseAsync() | ||
{ | ||
const int NumRequests = 2; | ||
await LoopbackServer.CreateClientAndServerAsync( | ||
async uri => | ||
await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync( | ||
async (handler, uri) => | ||
{ | ||
using (HttpClient client = new HttpClient()) | ||
using (HttpClient client = new HttpClient(handler)) | ||
{ | ||
await Assert.ThrowsAsync<HttpRequestException>(() => client.GetFromJsonAsync(uri, typeof(Person))); | ||
await Assert.ThrowsAsync<HttpRequestException>(() => client.GetFromJsonAsync<Person>(uri)); | ||
} | ||
}, | ||
async server => | ||
{ | ||
for (int i = 0; i < NumRequests; i++) | ||
{ | ||
await server.HandleRequestAsync(statusCode: HttpStatusCode.InternalServerError); | ||
} | ||
}); | ||
server => server.HandleRequestAsync(statusCode: HttpStatusCode.InternalServerError)); | ||
} | ||
|
||
[Fact] | ||
public async Task TestPostAsJsonAsync() | ||
{ | ||
const int NumRequests = 4; | ||
await LoopbackServer.CreateClientAndServerAsync( | ||
async uri => | ||
await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync( | ||
async (handler, uri) => | ||
{ | ||
using (HttpClient client = new HttpClient()) | ||
using (HttpClient client = new HttpClient(handler)) | ||
{ | ||
Person person = Person.Create(); | ||
|
||
|
@@ -103,24 +88,20 @@ await LoopbackServer.CreateClientAndServerAsync( | |
} | ||
}, | ||
async server => { | ||
for (int i = 0; i < NumRequests; i++) | ||
{ | ||
HttpRequestData request = await server.HandleRequestAsync(); | ||
ValidateRequest(request); | ||
Person per = JsonSerializer.Deserialize<Person>(request.Body, s_defaultSerializerOptions); | ||
per.Validate(); | ||
} | ||
HttpRequestData request = await server.HandleRequestAsync(); | ||
ValidateRequest(request); | ||
Person per = JsonSerializer.Deserialize<Person>(request.Body, s_defaultSerializerOptions); | ||
per.Validate(); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public async Task TestPutAsJsonAsync() | ||
{ | ||
const int NumRequests = 4; | ||
await LoopbackServer.CreateClientAndServerAsync( | ||
async uri => | ||
await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync( | ||
async (handler, uri) => | ||
{ | ||
using (HttpClient client = new HttpClient()) | ||
using (HttpClient client = new HttpClient(handler)) | ||
{ | ||
Person person = Person.Create(); | ||
Type typePerson = typeof(Person); | ||
|
@@ -139,13 +120,10 @@ await LoopbackServer.CreateClientAndServerAsync( | |
} | ||
}, | ||
async server => { | ||
for (int i = 0; i < NumRequests; i++) | ||
{ | ||
HttpRequestData request = await server.HandleRequestAsync(); | ||
ValidateRequest(request); | ||
Person obj = JsonSerializer.Deserialize<Person>(request.Body, s_defaultSerializerOptions); | ||
obj.Validate(); | ||
} | ||
HttpRequestData request = await server.HandleRequestAsync(); | ||
ValidateRequest(request); | ||
Person obj = JsonSerializer.Deserialize<Person>(request.Body, s_defaultSerializerOptions); | ||
obj.Validate(); | ||
}); | ||
} | ||
|
||
|
@@ -177,11 +155,10 @@ private void ValidateRequest(HttpRequestData requestData) | |
[Fact] | ||
public async Task AllowNullRequesturlAsync() | ||
{ | ||
const int NumRequests = 4; | ||
await LoopbackServer.CreateClientAndServerAsync( | ||
async uri => | ||
await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync( | ||
async (handler, uri) => | ||
{ | ||
using (HttpClient client = new HttpClient()) | ||
using (HttpClient client = new HttpClient(handler)) | ||
{ | ||
client.BaseAddress = uri; | ||
|
||
|
@@ -196,10 +173,7 @@ await LoopbackServer.CreateClientAndServerAsync( | |
List<HttpHeaderData> headers = new List<HttpHeaderData> { new HttpHeaderData("Content-Type", "application/json") }; | ||
string json = Person.Create().Serialize(); | ||
|
||
for (int i = 0; i < NumRequests; i++) | ||
{ | ||
await server.HandleRequestAsync(content: json, headers: headers); | ||
} | ||
await server.HandleRequestAsync(content: json, headers: headers); | ||
}); | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.