Skip to content
Martijn Bodeman edited this page Nov 26, 2025 · 13 revisions

Mock HTTP responses for HttpClient and verify request expectations, with an experience inspired by Moq.

Installation

Install MockHttp via the Nuget package manager or dotnet cli.

dotnet add package skwas.MockHttp

For JSON (either STJ or Newtonsoft) integration:

dotnet add package skwas.MockHttp.Json

Main workflow Quality Gate Status Coverage

skwas.MockHttp NuGet NuGet Documentation
skwas.MockHttp.Json NuGet NuGet Documentation
skwas.MockHttp.Server NuGet NuGet Documentation

Usage example

MockHttpHandler mockHttp = new MockHttpHandler();

// Configure setup(s).
mockHttp
    .When(matching => matching
        .Method("GET")
        .RequestUri("http://localhost/controller/*")
    )
    .Respond(with => with
        .StatusCode(200)
        .JsonBody(new { id = 123, firstName = "John", lastName = "Doe" })
    )
    .Verifiable();

var client = new HttpClient(mockHttp);

// Act
var response = await client.GetAsync("http://localhost/controller/action?test=1");

// Assert
mockHttp.Verify();
response.StatusCode.Should().Be(HttpStatusCode.OK);
// ... etc ...

Clone this wiki locally