HttpServerMock 0.0.91
See the version list below for details.
dotnet add package HttpServerMock --version 0.0.91
NuGet\Install-Package HttpServerMock -Version 0.0.91
<PackageReference Include="HttpServerMock" Version="0.0.91" />
paket add HttpServerMock --version 0.0.91
#r "nuget: HttpServerMock, 0.0.91"
// Install HttpServerMock as a Cake Addin
#addin nuget:?package=HttpServerMock&version=0.0.91
// Install HttpServerMock as a Cake Tool
#tool nuget:?package=HttpServerMock&version=0.0.91
HTTP server mock is a really useful tool for testing, it will help you to mock HTTP requests.
SOME EXAMPLES
Delete
using (var hserver = new HttpServerMock(TestServerPort))
{
hserver.SetUpExpectation(HttpMethod.DELETE, "http://localhost:50000/user/23")
.ExpectedRequestHeader("test", "test1")
.Response(
HttpStatusCode.OK,
HttpRequestContentType.Json,
new ResponseTestClass { Name = "response", IsOld = true, Age = 12 });
var restClient = new RestClient(this.serverBaseUrl);
var request = new RestRequest("/user/23");
request.AddJsonBody(new { Name = "test", Id = 23 });
request.AddHeader("test", "test1");
request.Method = Method.DELETE;
var response = restClient.Execute<ResponseTestClass>(request);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "The respond status code is not the expected.");
hserver.VerifyAllRequestExpectationsAndUnexpectedRequests();
}
Post
using (var hserver = new HttpServerMock(TestServerPort))
{
hserver.SetUpPostExpectation("http://localhost:50000/user/23")
.ExpectedNumberOfCalls(1)
.ExpectedContent(new { Name = "test", Id = 23 }, HttpRequestContentType.Json)
.ExpectedRequestHeader("test", "test1")
.Response(HttpStatusCode.OK, HttpRequestContentType.Json, new ResponseTestClass { Name = "response", IsOld = true, Age = 12 });
var restClient = new RestClient(this.serverBaseUrl);
var request = new RestRequest("/user/23");
request.AddJsonBody(new { Name = "test", Id = 23 });
request.AddHeader("test", "test1");
request.Method = Method.POST;
var response = restClient.Execute<ResponseTestClass>(request);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "The respond status code is not the expected.");
Assert.IsNotNull(response.Data, "The response is empty.");
Assert.AreEqual("response", response.Data.Name, "The response data is not the expected.");
Assert.IsTrue(response.Data.IsOld, "The response data is not the expected.");
Assert.AreEqual(12, response.Data.Age, "The response data is not the expected.");
hserver.VerifyAllRequestExpectationsAndUnexpectedRequests();
}
Using Relatives Uris
using (var hserver = new HttpServerMock(TestServerPort))
{
hserver.SetUpGetExpectation("user/23")
.Response(HttpStatusCode.OK);
var restClient = new RestClient(this.serverBaseUrl);
var request = new RestRequest("/user/23") { Method = Method.GET };
var response = restClient.Execute(request);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "The respond status code is not the expected.");
Assert.IsNull(response.ErrorException, "The request contains an exception.");
}
Setting up a default status code
using (var hserver = new HttpServerMock(TestServerPort))
{
hserver.ServerRequestsState.DefaultRespondStatusCode = HttpStatusCode.NotModified;
var restClient = new RestClient(this.serverBaseUrl);
var request = new RestRequest("/user/23");
request.Method = Method.GET;
var response = restClient.Execute<ResponseTestClass>(request);
Assert.AreEqual(HttpStatusCode.NotModified, response.StatusCode, "The respond status code is not the expected.");
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net451 is compatible. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.5.1
- Microsoft.AspNet.WebApi.SelfHost (>= 5.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Classes renaming.