コード例 #1
0
        public async void GivenRequestDoesNotMatchVerification_WhenVerificationIsAttempted_ThenVerificationSuceeds()
        {
            var path        = "/bodytest456";
            var requestBody = "This is the request body 456";

            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri("http://localhost:1080/");

                var expectedRequest = RequestBuilder.Request()
                                      .WithMethod(HttpMethod.Post)
                                      .WithPath(path)
                                      .WithBody(requestBody);
                var mockServerClient = new MockServerClient(httpClient);
                mockServerClient.When(expectedRequest)
                .Respond(ResponseBuilder.Respond().WithStatusCode(200));

                var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "/bodytestfourfivesix");
                httpRequestMessage.Content = new StringContent(requestBody, System.Text.Encoding.UTF8);
                var response = await httpClient.SendAsync(httpRequestMessage);

                var result = await mockServerClient.Verify(expectedRequest.Create(), new VerificationTimes(1, 1));

                Assert.False(result);
            }
        }
コード例 #2
0
        public async Task Should_Verify_Expecation_Was_Met_On_MockServer()
        {
            // Arrange
            using var client = new MockServerClient("http://localhost:5000");

            var response = await client.SetupExpectationAsync(new Expectation
            {
                HttpRequest  = HttpRequest.Get("/test"),
                HttpResponse = new HttpResponse(201)
            });

            response.EnsureSuccessStatusCode();

            var request = new HttpRequestMessage(HttpMethod.Get, new Uri("test", UriKind.Relative));

            response = await client.SendAsync(request);

            response.EnsureSuccessStatusCode();

            // Act
            var verification = VerificaionRequest.Once(HttpRequest.Get("/test"));

            response = await client.Verify(verification);

            // Assert
            response.StatusCode.Should().Be(HttpStatusCode.Accepted);
        }
コード例 #3
0
        public void ShouldVerifyAtMostSuccess()
        {
            SendHello(1);

            Assert.NotNull(MockServerClient.Verify(Request()
                                                   .WithMethod(HttpMethod.Get)
                                                   .WithPath("/hello"), VerificationTimes.AtMost(2)));
        }
コード例 #4
0
        public void ShouldVerifyMultipleRequestsFailed()
        {
            var ex = Assert.Throws <AssertionException>(() =>
            {
                MockServerClient.Verify(Request().WithPath("/hello"), Request().WithPath("/world"));
            });

            Assert.StartsWith("Request sequence not found", ex.Message);
        }
コード例 #5
0
        public void ShouldVerifyAtLeastFailed()
        {
            var ex = Assert.Throws <AssertionException>(() =>
            {
                MockServerClient.Verify(Request()
                                        .WithMethod(HttpMethod.Get)
                                        .WithPath("/hello"), VerificationTimes.AtLeast(2));
            });

            Assert.StartsWith("Request not found at least 2 times", ex.Message);
        }
コード例 #6
0
        public void ShouldVerifyOnceFailed()
        {
            var ex = Assert.Throws <AssertionException>(() =>
            {
                MockServerClient.Verify(Request()
                                        .WithMethod(HttpMethod.Get)
                                        .WithPath("/hello"), VerificationTimes.Once());
            });

            Assert.StartsWith("Request not found exactly once", ex.Message);
        }
コード例 #7
0
        public void ShouldVerifyBetweenFailed()
        {
            var ex = Assert.Throws <AssertionException>(() =>
            {
                MockServerClient.Verify(Request()
                                        .WithMethod(HttpMethod.Get)
                                        .WithPath("/hello"), VerificationTimes.Between(1, 2));
            });

            Assert.StartsWith("Request not found between 1 and 2", ex.Message);
        }
コード例 #8
0
        public void ShouldVerify()
        {
            // arrange
            HttpRequest request = Request().WithMethod("GET").WithPath("/hello");

            mockServerClient
            .When(request, Times.Unlimited())
            .Respond(Response().WithStatusCode(200).WithBody("hello").WithDelay(TimeSpan.FromSeconds(0)));

            SendRequest(BuildGetRequest("/hello"), out _, out _);
            SendRequest(BuildGetRequest("/hello"), out _, out _);

            // act
            var result = mockServerClient.Verify(Request()
                                                 .WithMethod("GET")
                                                 .WithPath("/hello"), VerificationTimes.Exactly(2));

            // assert
            Assert.NotNull(result);
        }
コード例 #9
0
        public void ShouldVerifyMultipleRequests()
        {
            MockServerClient
            .When(Request().WithPath("/hello"), Times.Exactly(1))
            .Respond(Response().WithStatusCode(200).WithBody("hello").WithDelay(TimeSpan.FromSeconds(0)));

            MockServerClient
            .When(Request().WithPath("/world"), Times.Exactly(1))
            .Respond(Response().WithStatusCode(200).WithBody("world").WithDelay(TimeSpan.FromSeconds(0)));

            SendRequest(BuildGetRequest("/hello"), out var helloResponse, out _);
            SendRequest(BuildGetRequest("/world"), out var worldResponse, out _);

            Assert.Equal("hello", helloResponse);
            Assert.Equal("world", worldResponse);

            Assert.NotNull(MockServerClient.Verify(Request().WithPath("/hello"), Request().WithPath("/world")));
        }
コード例 #10
0
        public void ShouldVerifyExactlyFailed()
        {
            // arrange
            var request = Request().WithMethod(HttpMethod.Get).WithPath("/hello");

            MockServerClient
            .When(request, Times.Unlimited())
            .Respond(Response().WithStatusCode(200).WithBody("hello").WithDelay(TimeSpan.FromSeconds(0)));

            // act
            SendRequest(BuildGetRequest("/hello"), out _, out _);

            // assert
            var ex = Assert.Throws <AssertionException>(() =>
            {
                MockServerClient.Verify(Request()
                                        .WithMethod(HttpMethod.Get)
                                        .WithPath("/hello"), VerificationTimes.Exactly(2));
            });

            Assert.StartsWith("Request not found exactly 2 times", ex.Message);
        }