コード例 #1
0
        public void Reconfigure(Action <MockedHttpServerBuilder> configure)
        {
            var builder = new MockedHttpServerBuilder();

            configure(builder);
            builder.Reconfigure(_server, true);
        }
コード例 #2
0
        public void should_be_able_to_valid_request()
        {
            var          builder = new MockedHttpServerBuilder();
            const string result  = " a \"c\" b ";

            var actualRequest = ActualRequest.ToRequest();

            builder.WhenPost(string.Format("/test"))
            .WithRequest(r => actualRequest = r)
            .RespondContent(HttpStatusCode.OK, new StringContent(result));

            using (builder.Build("http://localhost:1122"))
            {
                using (var httpClient = new HttpClient())
                {
                    var requestBody = new { Field = "a", Field2 = "b" };
                    var request     = new HttpRequestMessage(HttpMethod.Post, "http://localhost:1122/test")
                    {
                        Content = new StringContent(JsonConvert.SerializeObject(requestBody))
                    };

                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    var response = httpClient.SendAsync(request).Result;
                    Assert.Equal(result, response.Content.ReadAsStringAsync().Result); // raw string
                    Assert.Equal("a", actualRequest.RequestBody.Field);
                    Assert.Equal("b", actualRequest.RequestBody.Field2);
                }
            }
        }
コード例 #3
0
ファイル: Facts.cs プロジェクト: hit-services/SimpleHttpMock
        public void should_read_string_as_request_body_for_unknow_content_type()
        {
            var request = default(object).ToRequest();
            var builder = new MockedHttpServerBuilder();

            builder
            .WhenPost("/streams/test")
            .WithRequest <object>(r => request = r)
            .Respond(HttpStatusCode.OK);
            using (builder.Build("http://*****:*****@"[
                      {
                        ""eventId"": ""e1fdf1f0-a66d-4f42-95e6-d6588cc22e9b"",
                        ""id"": 0
                      }
                    ]";
                    var          content = new StringContent(result);
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.eventstore.events+json");

                    var response = httpClient.PostAsync("http://localhost:1122/streams/test", content).Result;
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.NotNull(request.RequestBody);
                }
            }
        }
コード例 #4
0
ファイル: Facts.cs プロジェクト: hit-services/SimpleHttpMock
        public void should_be_able_to_retrive_request()
        {
            var builder          = new MockedHttpServerBuilder();
            var requestRetriever = builder.WhenGet("/test1").Respond(HttpStatusCode.OK).Retrieve();

            using (builder.Build("http://localhost:1122"))
            {
                using (var httpClient = new HttpClient())
                {
                    Assert.Null(requestRetriever());
                    var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test0")).Result;
                    Assert.Null(requestRetriever());
                    Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);

                    response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test1")).Result;
                    var actualRequest = requestRetriever();
                    Assert.NotNull(actualRequest);
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.Equal("GET", actualRequest.Method);
                    Assert.Equal("http://localhost:1122/test1", actualRequest.RequestUri.ToString());

                    response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test2")).Result;
                    var retriever = requestRetriever();
                    Assert.NotNull(retriever);
                    Assert.Equal("http://localhost:1122/test1", retriever.RequestUri.ToString());
                    Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
                }
            }
        }
コード例 #5
0
ファイル: Facts.cs プロジェクト: vishalishere/SimpleHttpMock
        public void should_read_as_model_wen_media_type_is_json()
        {
            var request = default(List <StreamEntity>).ToRequest();
            var builder = new MockedHttpServerBuilder();

            builder
            .WhenPost(string.Format("/streams/test"))
            .WithRequest <List <StreamEntity> >(r => request = r)
            .Respond(HttpStatusCode.OK);
            using (builder.Build("http://*****:*****@"[
                      {
                        ""eventId"": ""e1fdf1f0-a66d-4f42-95e6-d6588cc22e9b"",
                        ""id"": 0
                      }
                    ]";

                    var content = new StringContent(result);
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    var response = httpClient.PostAsync("http://localhost:1122/streams/test", content).Result;
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.NotNull(request.RequestBody);
                }
            }
        }
コード例 #6
0
        public void SetupStatusResponse(HttpStatusCode code)
        {
            var builder = new MockedHttpServerBuilder();

            builder.WhenGet("/status").Respond(code);
            builder.Reconfigure(_server, true);
        }
コード例 #7
0
        public void SetupStatusResponse(HttpStatusCode code, object model)
        {
            var builder = new MockedHttpServerBuilder();

            builder.WhenGet("/status").RespondContent(code, r => new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"));
            builder.Reconfigure(_server, true);
        }
コード例 #8
0
        public void SetupStatusPlainResponse(HttpStatusCode code, string text)
        {
            var builder = new MockedHttpServerBuilder();

            builder.WhenGet("/status").RespondContent(code, r => new StringContent(text, Encoding.UTF8, "text/plain"));
            builder.Reconfigure(_server, true);
        }
コード例 #9
0
ファイル: Facts.cs プロジェクト: hit-services/SimpleHttpMock
        public void should_be_able_to_match_the_last_mocked_request()
        {
            var builder = new MockedHttpServerBuilder();
            var firstRequestRetriever = builder.WhenGet("/multi-time-to-mock")
                                        .RespondContent(HttpStatusCode.OK, request => new StringContent("mock uri for first time"))
                                        .MatchRequest(r => true)
                                        .Retrieve();
            var secondRequestRetriever = builder.WhenGet("/multi-time-to-mock")
                                         .RespondContent(HttpStatusCode.BadGateway, request => new StringContent("mock uri for second time"))
                                         .MatchRequest(r => true)
                                         .Retrieve();

            using (builder.Build("http://localhost:1122"))
            {
                using (var httpClient = new HttpClient())
                {
                    var actualRequest = secondRequestRetriever();
                    Assert.Null(actualRequest);
                    var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/multi-time-to-mock")).Result;
                    actualRequest = secondRequestRetriever();
                    Assert.NotNull(actualRequest);
                    Assert.NotNull(response);
                }
            }
        }
コード例 #10
0
        private Func <ActualRequest> CreateAllPostOkRequestBehavior()
        {
            var builder         = new MockedHttpServerBuilder();
            var retrieveRequest = builder
                                  .WhenPost(Matchers.Regex(".*")).Respond(HttpStatusCode.OK)
                                  .Retrieve();

            builder.Build(_mockedPiwikServer);
            return(retrieveRequest);
        }
コード例 #11
0
        public void should_support_it_is_star_wildcard()
        {
            var serverBuilder = new MockedHttpServerBuilder();

            serverBuilder
            .WhenGet(Matchers.Wildcard(@"/staff*"))
            .Respond(HttpStatusCode.InternalServerError);

            using (serverBuilder.Build(BaseAddress))
            {
                var response = Get(RequestUri);
                Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            }
        }
コード例 #12
0
        public void should_support_is_regex(string url, HttpStatusCode expectedStatusCode)
        {
            var serverBuilder = new MockedHttpServerBuilder();

            serverBuilder
            .WhenGet(Matchers.Regex(@"/(staff)|(user)s"))
            .Respond(HttpStatusCode.InternalServerError);

            using (serverBuilder.Build(BaseAddress))
            {
                var response = Get($"{BaseAddress}{url}");
                Assert.Equal(expectedStatusCode, response.StatusCode);
            }
        }
コード例 #13
0
        public void should_support_it_is_question_mark_wildcard()
        {
            var serverBuilder = new MockedHttpServerBuilder();

            serverBuilder
            .WhenGet(Matchers.Wildcard(@"/staffs/?"))
            .Respond(HttpStatusCode.InternalServerError);

            using (serverBuilder.Build(BaseAddress))
            {
                var response = Get(string.Format("{0}/staffs/1", BaseAddress));
                Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            }
        }
コード例 #14
0
ファイル: Facts.cs プロジェクト: hit-services/SimpleHttpMock
 public void should_be_able_create_server_and_build()
 {
     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.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result;
             Assert.Equal(HttpStatusCode.OK, response.StatusCode);
         }
     }
 }
コード例 #15
0
        public void should_support_it_is()
        {
            var serverBuilder = new MockedHttpServerBuilder();

            serverBuilder
            .WhenGet(Matchers.Is("/staff?employeeId=Staff0001"))
            .Respond(HttpStatusCode.InternalServerError);

            using (serverBuilder.Build(BaseAddress))
            {
                var response = Get(RequestUri);
                Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            }
        }
コード例 #16
0
        public void should_support_head_request()
        {
            var serverBuilder = new MockedHttpServerBuilder();

            serverBuilder
            .When(Matchers.Regex(@"/staffs"), HttpMethod.Head)
            .Respond(HttpStatusCode.InternalServerError);

            using (serverBuilder.Build(BaseAddress))
            {
                var response = SendHttpRequest($"{BaseAddress}/staffs", HttpMethod.Head);
                Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            }
        }
コード例 #17
0
        public void should_support_is_regex_for_post()
        {
            var serverBuilder = new MockedHttpServerBuilder();

            serverBuilder
            .WhenPost(Matchers.Regex(@"/staffs"))
            .Respond(HttpStatusCode.InternalServerError);

            using (serverBuilder.Build(BaseAddress))
            {
                var data     = new { Name = "Staff", Email = "*****@*****.**" };
                var response = Post($"{BaseAddress}/staffs", data);
                Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            }
        }
コード例 #18
0
ファイル: Facts.cs プロジェクト: hit-services/SimpleHttpMock
        public void should_be_able_to_process_string_as_json()
        {
            var builder  = new MockedHttpServerBuilder();
            var retrieve = builder.WhenPut("/te$st").Respond(HttpStatusCode.OK, new {}).Retrieve();

            using (builder.Build("http://localhost:1122"))
            {
                using (var httpClient = new HttpClient())
                {
                    var response = httpClient.PutAsJsonAsync("http://localhost:1122/te$st", "abc").Result;
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.Equal("abc", retrieve().RequestBody);
                }
            }
        }
コード例 #19
0
ファイル: Facts.cs プロジェクト: vishalishere/SimpleHttpMock
        public void should_be_not_case_sensitive()
        {
            var builder = new MockedHttpServerBuilder();

            builder
            .WhenGet("/Test")
            .Respond(HttpStatusCode.InternalServerError);
            using (builder.Build("http://localhost:1122"))
            {
                using (var httpClient = new HttpClient())
                {
                    var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result;
                    Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
                }
            }
        }
コード例 #20
0
        public void should_be_able_to_accept_string_content()
        {
            var          builder = new MockedHttpServerBuilder();
            const string result  = " a \"c\" b ";

            builder.WhenGet(string.Format("/test"))
            .RespondContent(HttpStatusCode.OK, new StringContent(result));
            using (builder.Build("http://localhost:1122"))
            {
                using (var httpClient = new HttpClient())
                {
                    var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result;
                    Assert.Equal(result, response.Content.ReadAsStringAsync().Result); // raw string
                }
            }
        }
コード例 #21
0
        public void should_matches_url_when_it_is_absolute_uri()
        {
            var builder = new MockedHttpServerBuilder();

            builder
            .WhenGet("http://localhost:1122/test")
            .Respond(HttpStatusCode.InternalServerError);
            using (builder.Build("http://localhost:1122"))
            {
                using (var httpClient = new HttpClient())
                {
                    var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result;
                    Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
                }
            }
        }
コード例 #22
0
ファイル: Facts.cs プロジェクト: hit-services/SimpleHttpMock
        public void should_be_able_to_accept_raw_object()
        {
            var builder = new MockedHttpServerBuilder();
            int result  = 56;

            builder.WhenGet("/test")
            .Respond(HttpStatusCode.OK, result);
            using (builder.Build("http://localhost:1122"))
            {
                using (var httpClient = new HttpClient())
                {
                    var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result;
                    var actual   = response.Content.ReadAsStringAsync().Result;
                    Assert.Equal(result.ToString(), actual);
                }
            }
        }
コード例 #23
0
        public void should_match_url_contains_question_mark()
        {
            var serverBuilder = new MockedHttpServerBuilder();

            serverBuilder
            .WhenGet("/staff?employeeId=Staff0001")
            .Respond(HttpStatusCode.InternalServerError);

            const string baseAddress = "http://localhost:1122";

            using (serverBuilder.Build(baseAddress))
            {
                const string requestUri = "http://localhost:1122/staff?employeeId=Staff0001";
                var          response   = Get(requestUri);
                Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            }
        }
コード例 #24
0
ファイル: Facts.cs プロジェクト: hit-services/SimpleHttpMock
        public void should_be_able_to_match_and_retrive_request()
        {
            var builder          = new MockedHttpServerBuilder();
            var requestRetriever = builder.WhenGet("/te$st").Respond(HttpStatusCode.OK)
                                   .MatchRequest(r => true)
                                   .Retrieve();

            using (builder.Build("http://localhost:1122"))
            {
                using (var httpClient = new HttpClient())
                {
                    var actualRequest = requestRetriever();
                    Assert.Null(actualRequest);
                    var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/te$st")).Result;
                    actualRequest = requestRetriever();
                    Assert.NotNull(actualRequest);
                }
            }
        }
コード例 #25
0
        public void should_be_able_to_accept_custom_header()
        {
            var          builder     = new MockedHttpServerBuilder();
            const string content     = "dummy";
            const string headerValue = "testHeaderValue";

            builder.WhenGet(string.Format("/test"))
            .RespondContent(HttpStatusCode.OK, new StringContent(content))
            .RespondHeaders(new { headerKey = headerValue });
            using (builder.Build("http://localhost:1122"))
            {
                using (var httpClient = new HttpClient())
                {
                    var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result;
                    Assert.Equal(content, response.Content.ReadAsStringAsync().Result);
                    Assert.Equal(headerValue, response.Headers.GetValues("headerKey").First());
                }
            }
        }
コード例 #26
0
        /// <summary>
        /// Create Stub target at a given <paramref name="location"/>.
        /// </summary>
        /// <param name="location">The HTTP Location.</param>
        /// <returns></returns>
        public static SpyHttpDeliverTarget AtLocation(string location)
        {
            var builder = new MockedHttpServerBuilder();
            var target  = new SpyHttpDeliverTarget();

            builder.WhenPost(location).RespondContent(
                httpStatusCode: HttpStatusCode.OK,
                contentFn: request =>
            {
                Task <string> task      = request.Content.ReadAsStringAsync();
                target.DeliveredMessage = task.Result;

                target._waitHandle.Set();

                return(null);
            });

            target._httpServer = builder.Build(location);
            return(target);
        }
コード例 #27
0
ファイル: Facts.cs プロジェクト: hit-services/SimpleHttpMock
        public void should_be_able_to_reconfigure_server_on_the_fly_with_preserving_existing_mocks()
        {
            var builder = new MockedHttpServerBuilder();

            builder.WhenGet("/test").Respond(HttpStatusCode.BadRequest);
            builder.WhenPut("/test").Respond(HttpStatusCode.Accepted);

            using (var server = builder.Build("http://localhost:1122"))
                using (var httpClient = new HttpClient())
                {
                    Assert.Equal(HttpStatusCode.BadRequest, httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result.StatusCode);
                    Assert.Equal(HttpStatusCode.Accepted, httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Put, "http://localhost:1122/test")).Result.StatusCode);

                    var newBuilder = new MockedHttpServerBuilder();
                    newBuilder.WhenGet("/test").Respond(HttpStatusCode.OK);
                    newBuilder.Reconfigure(server, false);

                    Assert.Equal(HttpStatusCode.OK, httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result.StatusCode);
                    Assert.Equal(HttpStatusCode.Accepted, httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Put, "http://localhost:1122/test")).Result.StatusCode);
                }
        }
コード例 #28
0
ファイル: Facts.cs プロジェクト: hit-services/SimpleHttpMock
        public void should_be_able_to_reconfigure_server_on_the_fly()
        {
            var          builder = new MockedHttpServerBuilder();
            const string content = " a \"c\" b ";

            builder.WhenGet("/test")
            .RespondContent(HttpStatusCode.OK, request => new StringContent(content));

            using (var server = builder.Build("http://localhost:1122"))
                using (var httpClient = new HttpClient())
                {
                    var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result;
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    Assert.Equal(content, response.Content.ReadAsStringAsync().Result);

                    var newBuilder = new MockedHttpServerBuilder();
                    newBuilder.WhenGet("/test").Respond(HttpStatusCode.BadRequest);

                    newBuilder.Reconfigure(server, true);

                    Assert.Equal(HttpStatusCode.BadRequest, httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result.StatusCode);
                }
        }
コード例 #29
0
ファイル: Facts.cs プロジェクト: hit-services/SimpleHttpMock
        public void should_be_able_to_reconfigure_server_on_the_fly_with_removing_existing_mocks()
        {
            var builder = new MockedHttpServerBuilder();

            builder.WhenGet("/test").Respond(HttpStatusCode.BadRequest);
            builder.WhenPut("/test").Respond(HttpStatusCode.Accepted);

            using (var server = builder.Build("http://localhost:1122"))
                using (var httpClient = new HttpClient())
                {
                    Assert.Equal(HttpStatusCode.BadRequest, httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result.StatusCode);
                    Assert.Equal(HttpStatusCode.Accepted, httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Put, "http://localhost:1122/test")).Result.StatusCode);

                    var newBuilder = new MockedHttpServerBuilder();
                    newBuilder.WhenGet("/test").Respond(HttpStatusCode.InternalServerError);
                    newBuilder.Reconfigure(server, true);

                    //altered behavior
                    Assert.Equal(HttpStatusCode.InternalServerError, httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost:1122/test")).Result.StatusCode);
                    //the default response
                    Assert.Equal(HttpStatusCode.NotFound, httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Put, "http://localhost:1122/test")).Result.StatusCode);
                }
        }
コード例 #30
0
 public void SetupStatusResponse(HttpStatusCode code, object model)
 {
     var builder = new MockedHttpServerBuilder();
     builder.WhenGet("/status").RespondContent(code, r => new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"));
     builder.Reconfigure(_server, true);
 }
コード例 #31
0
 public void SetupStatusPlainResponse(HttpStatusCode code, string text)
 {
     var builder = new MockedHttpServerBuilder();
     builder.WhenGet("/status").RespondContent(code, r => new StringContent(text, Encoding.UTF8, "text/plain"));
     builder.Reconfigure(_server, true);
 }
コード例 #32
0
 public void SetupStatusResponse(HttpStatusCode code)
 {
     var builder = new MockedHttpServerBuilder();
     builder.WhenGet("/status").Respond(code);
     builder.Reconfigure(_server, true);
 }
コード例 #33
0
 public void Reconfigure(Action<MockedHttpServerBuilder> configure)
 {
     var builder = new MockedHttpServerBuilder();
     configure(builder);
     builder.Reconfigure(_server, true);
 }