https://github.com/kaliumhexacyanoferrat/mockh
This library allows to mock HTTP responses for integration, component and acceptance tests of your projects written in C# / .NET by hosting a webserver returning configured responses.
https://github.com/kaliumhexacyanoferrat/mockh
c-sharp component-testing csharp dotnet dotnet9 http integration-testing integration-tests mock mocking mocking-framework mocking-server request response webserver
Last synced: 3 months ago
JSON representation
This library allows to mock HTTP responses for integration, component and acceptance tests of your projects written in C# / .NET by hosting a webserver returning configured responses.
- Host: GitHub
- URL: https://github.com/kaliumhexacyanoferrat/mockh
- Owner: Kaliumhexacyanoferrat
- License: mit
- Created: 2021-11-25T20:47:02.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-01-31T12:37:50.000Z (9 months ago)
- Last Synced: 2025-05-19T10:19:39.601Z (6 months ago)
- Topics: c-sharp, component-testing, csharp, dotnet, dotnet9, http, integration-testing, integration-tests, mock, mocking, mocking-framework, mocking-server, request, response, webserver
- Language: C#
- Homepage:
- Size: 70.3 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# MockH
[](https://github.com/Kaliumhexacyanoferrat/MockH/actions/workflows/ci.yml) [](https://sonarcloud.io/summary/new_code?id=Kaliumhexacyanoferrat_MockH) [](https://www.nuget.org/packages/MockH/)
This library allows to mock HTTP responses for integration, component and acceptance tests of your projects written in C# / .NET 8/9 by hosting a webserver returning configured responses.
- Fast and thread safe
- Only a few dependencies
- No configuration needed
- Does not interfer with Kestrel or ASP.NET
- Independent from the testing framework in place
## Usage
```csharp
using MockH;
[TestMethod]
public async Task TestSomething()
{
await using var server = await MockServer.RunAsync
(
On.Get("/users/1").Return(new User(...)),
On.Get("/users/2").Respond(ResponseStatus.NoContent)
);
// access the server in your code via HTTP
using var client = new HttpClient();
await client.GetStringAsync(server.Url("/users/1"));
}
```
## Basic Usage
```csharp
// return a specific status code
On.Get("/ifail").Respond(ResponseStatus.InternalServerError);
// redirect the client
On.Get().Redirect("https://github.com");
// execute logic and return some simple text value
On.Get().Run(() => "42");
// execute logic and return some JSON
private record MyClass(int IntValue, string StringValue);
On.Get().Run(() => new MyClass(42, "The answer"));
// execute logic asynchronously
On.Get().Run(async () => await ...);
// access query parameters (GET /increment?=1)
On.Get("/increment").Run((int i) => i + 1);
// access path parameters (GET /increment/1)
On.Get("/increment/:i").Run((int i) => i + 1);
// access request body
On.Post().Run((MyClass body) => body);
// access request body as stream
On.Post().Run((Stream body) => body.Length);
```
## Advanced Usage
```csharp
// directly access request and response
On.Get().Run((IRequest request) => request.Respond().Status(ResponseStatus.BadRequest));
// return a handler provided by the GenHTTP framework, e.g. a website
// see https://genhttp.org/documentation/content/
// can be useful if you want to test some kind of website crawler
On.Get().Run(() => Listing.From(ResourceTree.FromDirectory("/var/www")));
```