Easy http stubs for integration testing
var fakeService = new FakeService() // Randomly allocates a port
var fakeService = new FakeService(port) // Or supply a specific port
fakeService.AddResponse requires a path, a method, and a Response
N.B. The path is a regex, so if you want to match /test?foo=bar then your path should be /test?foo=bar
fakeService.AddResponse("/foo", Method.GET, Response.WithStatusCode(200))
fakeService.AddResponse("/foo", Method.GET, Response.WithBody(200, "body"))
fakeService.AddResponse("/foo", Method.GET, Response.WithHeaders(200, new Dictionary<string, string> {["foo"] = "bar" }))
fakeService.AddResponse("/foo", Method.GET, Response.WithHeaders(200, "foo:bar", "bing:bong")
fakeService.AddResponse("/foo", Method.GET, Response.WithBodyAndHeaders(200, "body", new Dictionary<string, string> {["foo"] = "bar" }))
fakeService.AddResponse("/foo", Method.GET, Response.WithBodyAndHeaders(200, "body", "foo:bar", "bing:bong"))
fakeService.AddResponse("/foo", Method.GET, Response.WithResponses(new [] { Response.WithStatusCode(200), Response.WithStatusCode(500)}))
fakeService.AddResponse("/foo", Method.GET, Response.WithDelegate(x => x.Query["foo"].Contains("bar") ? Response.WithStatusCode(200) : Response.WithStatusCode(404)))
Requires a string in this format
GET path
200
foo: bar
Body
POST path
200
fakeService.AddResponsesFromText(text)
- Headers and body are optional
- Separate the body with a single line break
- Separate each response with a double line break
The same as above, but takes a file name instead of a string
fakeService.AddResponsesFromFile(fileName)
Call fakeService.Start() to host on a random available port. The address is returned as a string.
Call fakeService.StartTestHost() to host using Microsoft.AspNetCore.TestHost. The HttpClient is returned.
Call fakeService.StartApp(app) to override the middleware entirely with your own.
You can examine the requests sent to your service via fakeService.Requests
You can execute an Action<Request> when a request is received by using fakeService.OnRequestReceived(request => ...)
The address is returned from fakeService.Start() and is also available as a string fakeService.Url or Uri fakeService.Uri
FakeService implements IDisposable but you can also call fakeService.Stop()
