HttpClientExtensions Class
Definition
- Namespace
- Codebelt.Extensions.Xunit.Hosting.AspNetCore
- Assemblies
- Codebelt.Extensions.Xunit.Hosting.AspNetCore.dll
Provides extension methods for the HttpClient class.
public static class HttpClientExtensions
- Inheritance
-
HttpClientExtensions
Examples
The following example sends an HTTP request using the ToHttpResponseMessageAsync extension on an HttpClient. By default it performs a GET request to the root URL ("/"), or you can supply a custom response factory delegate. This method is designed to work with test host fixtures that provide a pre-configured HttpClient.
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Codebelt.Extensions.Xunit.Hosting.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.TestHost;
namespace WebFixtureTests;
public class HttpClientExample
{
public async Task DemonstrateAsync(HttpClient client)
{
var response = await client.ToHttpResponseMessageAsync().ConfigureAwait(false);
Console.WriteLine(response.StatusCode);
}
}
internal static class RunExample
{
static async Task Main()
{
using var app = WebHostTestFactory.Create(pipelineSetup: app => app.Run(context =>
{
context.Response.StatusCode = 202;
return Task.CompletedTask;
}));
using var client = app.Host.GetTestClient();
await new HttpClientExample().DemonstrateAsync(client);
}
}
Methods
| Name | Description |
|---|---|
| ToHttpResponseMessageAsync(HttpClient, Func<HttpClient, Task<HttpResponseMessage>>) | Provides a convenient way to return a HttpResponseMessage from a HttpClient using the specified |