Skip to main content

Definition

Assembly: System.Net.Http.dll Namespace: System.Net.Http

Syntax

System.Net.Http.HttpClient

Summary

This type is defined in System.Net.Http.

Remarks

See Microsoft documentation for more information about the rest of the API.

Methods

ExecuteTestRequest Extension

Extension method from System.Net.Http.Breakdance_WebApi_HttpClientExtensions
Creates an HttpRequestMessage for the given configuration and executes it asynchronously through the HttpClient.

Syntax

public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> ExecuteTestRequest(System.Net.Http.HttpClient httpClient, System.Net.Http.HttpMethod httpMethod, string host = "http://localhost/", string routePrefix = "api/tests/", string resource = null, string acceptHeader = "application/json", object payload = null, Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = null)

Parameters

NameTypeDescription
httpClientSystem.Net.Http.HttpClientThe HttpClient instance to use.
httpMethodSystem.Net.Http.HttpMethodThe HttpMethod to use for the request.
hoststringThe hostname to use for this request. Defaults to “http://localhost”, only change it if that collides with other services running on the local machine.
routePrefixstringThe routePrefix corresponding to the route already mapped in MapRestierRoute or GetTestableConfiguration. Defaults to “api/test”, only change it if absolutely necessary.
resourcestringThe resource on the API to be requested.
acceptHeaderstringThe inbound MIME types to accept. Defaults to “application/json”.
payloadobject-
jsonSerializerSettingsNewtonsoft.Json.JsonSerializerSettings-

Returns

Type: System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> An HttpResponseMessage containing the results of the attempted request.

Examples

This sample shows the simplest way to create a testable HttpClient and execute a test request, using MSTest and FluentAssertions.
[TestClass]
 ApiTests
{
hod]
sync Task TestApi_Companies_ReturnsResults()
    {
httpClient = WebApiTestHelpers.GetTestableHttpClient();
result = await httpClient.ExecuteTestRequest(HttpMethods.Get, resource = "/Companies");
lt.Should().NotBeNull();
lt.StatusCode.Should().Be(HttpStatusCode.OK);
content = await result.Content.ReadAsStringAsync();
ent.Should().NotBeNullOrWhiteSpace();
    }
}