A really simple http mock using self host service.
var builder = new MockedHttpServerBuilder();
builder
.WhenGet("/Test")
.Respond(HttpStatusCode.OK);
using (builder.Build("http://localhost:1122"))
using (var httpClient = new HttpClient())
{
var response = await httpClient.GetAsync("http://localhost:1122/test");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
var builder = new MockedHttpServerBuilder();
builder
.WhenGet("/Test")
.Respond(HttpStatusCode.OK, new People { Name = "John Doe"});
using (builder.Build("http://localhost:1122"))
using (var httpClient = new HttpClient())
{
var response = await httpClient.GetAsync("http://localhost:1122/test");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var people = await response.ReadAsAsync<People>()
Assert.Equal("John Doe", people.Name);
}
using (var server = new MockedHttpServer("http://localhost:1122"))
{
var builder = new MockedHttpServerBuilder();
builder.WhenGet("/test").Respond(HttpStatusCode.OK);
builder.Build(server);
using (var httpClient = new HttpClient())
{
var response = httpClient.GetAsync("http://localhost:1122/test");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
var builder = new MockedHttpServerBuilder();
var requestRetriever = builder.WhenGet("/test").Respond(HttpStatusCode.OK).Retrieve();
using (builder.Build("http://localhost:1122"))
{
using (var httpClient = new HttpClient())
{
// no request being sent yet
Assert.Null(requestRetriever());
// when send the request
var response = await httpClient.GetAsync("http://localhost:1122/test1");
// should get the request by retriever
var actualRequest = requestRetriever();
Assert.NotNull(actualRequest);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("GET", actualRequest.Method);
Assert.Equal("http://localhost:1122/test", actualRequest.RequestUri.ToString());
}
}
- Matchers.Regex
var serverBuilder = new MockedHttpServerBuilder();
serverBuilder
.WhenGet(Matchers.Regex(@"/(staff)|(user)s"))
.Respond(HttpStatusCode.InternalServerError);
using (serverBuilder.Build(BaseAddress))
{
var response = Get("http://localhost/users"));
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}
- Matchers.WildCard
serverBuilder
.WhenGet(Matchers.Wildcard(@"/staffs/?"))
.Respond(HttpStatusCode.Unauthorized);
- Matchers.Is
serverBuilder
.WhenGet(Matchers.Is("/staffs"))
.Respond(HttpStatusCode.OK);
serverBuilder
.When(Matchers.Wildcard(@"/staffs/?"), HttpMethod.Patch)
.Respond(HttpStatusCode.Unauthorized);