HttpServerMock 0.0.91

HTTP Server Mock

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.");
           }

There is a newer version of this package available.
See the version list below for details.
Install-Package HttpServerMock -Version 0.0.91
dotnet add package HttpServerMock --version 0.0.91
paket add HttpServerMock --version 0.0.91
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Release Notes

Classes renaming.

Version History

Version Downloads Last updated
0.0.92 1,535 7/29/2015
0.0.91 386 5/11/2015
0.0.9 245 5/6/2015