GennadyGS.WireMock.Net.Routing
1.0.1
dotnet add package GennadyGS.WireMock.Net.Routing --version 1.0.1
NuGet\Install-Package GennadyGS.WireMock.Net.Routing -Version 1.0.1
<PackageReference Include="GennadyGS.WireMock.Net.Routing" Version="1.0.1" />
<PackageVersion Include="GennadyGS.WireMock.Net.Routing" Version="1.0.1" />
<PackageReference Include="GennadyGS.WireMock.Net.Routing" />
paket add GennadyGS.WireMock.Net.Routing --version 1.0.1
#r "nuget: GennadyGS.WireMock.Net.Routing, 1.0.1"
#:package GennadyGS.WireMock.Net.Routing@1.0.1
#addin nuget:?package=GennadyGS.WireMock.Net.Routing&version=1.0.1
#tool nuget:?package=GennadyGS.WireMock.Net.Routing&version=1.0.1
WireMock.Net.Routing
WireMock.Net.Routing extends WireMock.Net with modern, minimal-API-style routing for .NET. It provides extension methods for expressive, maintainable, and testable HTTP routing, inspired by ASP.NET Core Minimal APIs.
Motivation
While WireMock.Net is a powerful tool for HTTP mocking in .NET, its native API for defining routes and request handlers can be verbose and require significant boilerplate. Setting up even simple endpoints often involves multiple chained method calls, manual parsing of request data, and repetitive configuration, which can make tests harder to read and maintain.
WireMock.Net.Routing addresses these pain points by introducing a concise, fluent, and minimal-API-inspired approach to routing. This makes your test code:
- More readable: Route definitions are clear and expressive, closely resembling production minimal APIs.
- Easier to maintain: Less boilerplate means fewer places for errors and easier refactoring.
- Faster to write: Define routes and handlers in a single line, with strong typing and async support.
Example: Native WireMock.Net vs. WireMock.Net.Routing
Native WireMock.Net
server.Given(
Request.Create().WithPath("/hello").UsingGet()
)
.RespondWith(
Response.Create().WithBody("Hello, world!")
);
server.Given(
Request.Create().WithPath("/user/*").UsingGet()
)
.RespondWith(
Response.Create().WithCallback(request =>
{
var id = request.PathSegments[1];
// ...fetch user by id...
return new ResponseMessage { Body = $"User: {id}" };
})
);
With WireMock.Net.Routing
router.MapGet("/hello", _ => "Hello, world!");
router.MapGet("/user/{id:int}", requestInfo =>
{
var id = requestInfo.RouteArgs["id"];
// ...fetch user by id...
return $"User: {id}";
});
With WireMock.Net.Routing, you get:
- Minimal, one-line route definitions
- Typed route parameters (e.g.,
{id:int}
) - Direct access to parsed route arguments and request bodies
- Async handler support
This leads to more maintainable, scalable, and production-like test code.
Features
- Minimal API-style route definitions for WireMock.Net
- Strongly-typed request handling
- Routing parameters with constraints (
int
andstring
are currently supported) - Asynchronous handlers
- Fluent, composable routing extensions
- Easy integration with existing WireMock.Net servers
- .NET 8+ support
Installation
Install from NuGet:
dotnet add package WireMock.Net.Routing
Quick Start
using System.Net.Http.Json;
using WireMock.Net.Routing;
using WireMock.Net.Routing.Extensions;
using WireMock.Server;
var server = WireMockServer.Start();
var router = new WireMockRouter(server);
router.MapGet("/hello", _ => "Hello, world!");
using var client = server.CreateClient();
var result = await client.GetFromJsonAsync<string>("/hello");
// Hello, world!
Usage
Routing with route parameters
router.MapGet("/user/{id:int}", async requestInfo => {
var userId = requestInfo.RouteArgs["id"];
// var user = await ...
return user;
});
Strongly-Typed Request Info
router.MapPost<Item>("/api/items", requestInfo => {
var item = requestInfo.Body!;
// process item
return Results.Json(new { success = true });
});
Supported Methods
MapGet
,MapPost
,MapPut
,MapDelete
Documentation
Contributing
Contributions are welcome! Please open issues or pull requests. See CONTRIBUTING.md if available.
License
This project is licensed under the MIT License. See LICENSE for details.
Support & Feedback
For questions, suggestions, or issues, please use the GitHub Issues page.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- WireMock.Net (>= 1.8.17)
-
net9.0
- WireMock.Net (>= 1.8.17)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.