Skip to main content
Total Downloads
9.1K
9,111
Published
Dec 13, 2022
4y ago

Get Started

dotnet add package FsHttpClientMock --version 1.5.5.62151
ProductVersions
.NET
net6.0
.NET Standard
netstandard2.0

README

FsHttpClientMock

.NET

A really simple http client mock.

Examples

Match by simple url

[Fact]
public async void match_by_simple_url()
{
    var builder = new MockedHttpClientBuilder();
    builder
        .WhenGet("/Test")
        .Respond(HttpStatusCode.OK);s
    using var httpClient = builder.Build("http://localhost:1122");
    var response = await httpClient.GetAsync("http://localhost:1122/test");
    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

Return some response

[Fact]
public async void return_some_response()
{
    var builder = new MockedHttpClientBuilder();
    builder
        .WhenGet("/Test")
        .Respond(HttpStatusCode.OK, new People { Name = "John Doe"});

    using var httpClient = builder.Build("http://localhost:1122");
    var response = await httpClient.GetAsync("http://localhost:1122/test");
    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
    var people = await response.Content.ReadFromJsonAsync<People>();
    Assert.Equal("John Doe", people?.Name);
}

Capture request being sent

[Fact]
public async void capture_request_being_sent()
{
    var builder = new MockedHttpClientBuilder();
    var capture = builder.WhenGet("/test").Respond(HttpStatusCode.OK).Capture();
    using var httpClient = builder.Build("http://localhost:1122");
    // no request being sent yet
    Assert.Null(capture());

    // when send the request
    var response = await httpClient.GetAsync("http://localhost:1122/test");

    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
    // should get the request by retriever
    var requestCapture = capture();
    Assert.NotNull(requestCapture);
    Assert.Equal(HttpMethod.Get, requestCapture.HttpMethod);
    Assert.Equal("http://localhost:1122/test", requestCapture.RequestUri.ToString());
}

Hamcrest Style Matchers

  • Matchers.Regex
[Fact]
public async void hamcrest_style_matchers()
{
    var builder = new MockedHttpClientBuilder();
    builder
        .WhenGet(UrlMatchers.Regex(@"/(staff)|(user)s"))
        .Respond(HttpStatusCode.InternalServerError);

    using var httpClient = builder.Build("http://localhost:1122");
    var response = await httpClient.GetAsync("http://localhost:1122/users");

    Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);        
}
  • Matchers.WildCard
builder
    .WhenGet(Matchers.Wildcard(@"/staffs/?"))
    .Respond(HttpStatusCode.Unauthorized);
  • Matchers.Is
builder
     .WhenGet(Matchers.Is("/staffs"))
     .Respond(HttpStatusCode.OK);

Downloads by Version

Depended On By

Find packages that declare this package as a dependency. NuGet does not provide a reverse-dependency endpoint, so candidates are checked on demand.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

Compatibility metadata lists 2 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.