Skip to content

GennadyGS/WireMock.Net.Routing

Repository files navigation

WireMock.Net.Routing

NuGet Build .NET License

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 and string 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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published