Easy http stubs for integration testing
var fakeService = new FakeService()
fakeService.AddResponse
requires a path(as a regex), a method, and a Response
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)
Calling fakeService.Start()
to host on a random available port. The address is returned as a string
.
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()