Skip to content

StubHttp class

Defined in

Namespace: Refit.Testing Assembly: Refit.Testing.dll Full name: Refit.Testing.StubHttp Modifiers: public sealed

Summary

View source

A declarative test HttpMessageHandler for Refit clients, written as a route table: each entry pairs a RouteMatcher (built with Route) with a StubResponse (built with Reply). Populate it with a collection initializer whose templates mirror the routes declared on the Refit interface.

Applies to

net10.0, net10.0-browserwasm1.0, net10.0-desktop1.0, net9.0, net9.0-browserwasm1.0, net9.0-desktop1.0, net8.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.2, net8.0-macos14.5, net8.0-tvos17.2, netstandard2.1, net462, net481, net471

Class hierarchy
classDiagram
class StubHttp
class HttpMessageHandler
HttpMessageHandler <|-- StubHttp
class IEnumerable~RouteMatcher~ {
    <>
}
IEnumerable~RouteMatcher~ <|.. StubHttp
class IEnumerable {
    <>
}
IEnumerable <|.. StubHttp

Inherits from: HttpMessageHandler

Implements: IEnumerable, IEnumerable

Remarks

var http = new StubHttp
 {
 { Route.Get("/users/{id}"), Reply.With(new User("octocat")) },
 { Route.Post("/users"), Reply.Status(HttpStatusCode.Created) },
 };
 var api = http.CreateClient<IGitHubApi>(baseUrl);

Non-reusable routes (the common case) are one-shot: each satisfies exactly one request and is then consumed, and VerifyAllCalled asserts every one was hit. Set Reusable for a background stub that may match any number of requests. An unmatched request throws rather than returning a canned 404, and every request is recorded in Requests for inspection — including typed inspection of the sent body via LastRequestBodyAsync.

Constructors

NameSummary
.ctorInitializes a new instance of the [StubHttp](# class with an empty route table.

Properties

NameSummary
RequestsGets a snapshot of the requests this handler has received, in order.
BehaviorGets or sets the network behavior simulated for each matched request; null disables simulation.

Methods

NameSummary
AddAdds a route and its reply to the table. Called by the collection initializer.
ToSettingsWraps this handler in a fresh [RefitSettings](# that routes requests through it.
CreateClientCreates a Refit implementation of T whose HTTP requests are routed through this handler. A one-line replacement for RestService.For<T>(hostUrl, handler.ToSettings()).
CreateGeneratedClientCreates a source-generated Refit implementation of T whose HTTP requests are routed through this handler, without falling back to reflection. Use it in trim- or AOT-compiled test hosts where...
LastRequestBodyAsyncDeserializes the body of the most recent request using the client's content serializer.
RequestBodyAsyncDeserializes the body of the request at index using the client's content serializer.
VerifyAllCalledAsserts every non-reusable route was matched by a request.
VerifyAllCalledAsyncAsynchronously waits (up to a default 1 second) for every non-reusable route to be hit, then asserts. Use this when the requests under test are fired-and-forget (e.g. an observable...
SendAsync
Inherited members