コード例 #1
0
        public async Task <MockServerClient> VerifyAsync(HttpRequest httpRequest, VerificationTimes times)
        {
            if (httpRequest == null)
            {
                throw new ArgumentException("Required: Non-Null request");
            }

            if (times == null)
            {
                throw new ArgumentException("Required: Non-Null verification times");
            }

            var verification = new Verification().WithRequest(httpRequest).WithTimes(times);

            var response = await SendRequestAsync(new HttpRequestMessage()
                                                  .WithMethod(HttpMethod.Put)
                                                  .WithUri(ServerAddress(FullPath(VerifyEndpoint)))
                                                  .WithBody(_verificationSerializer.Serialize(verification), Encoding.UTF8));

            var body = await response.Content.ReadAsStringAsync();

            if (!body.IsNullOrEmpty())
            {
                throw new AssertionException(body);
            }

            return(this);
        }
コード例 #2
0
        public T Verify(HttpRequest httpRequest, VerificationTimes times)
        {
            if (httpRequest == null)
            {
                throw new ArgumentException("Required: Non-Null request");
            }

            if (times == null)
            {
                throw new ArgumentException("Required: Non-Null verification times");
            }

            var verification = new Verification().WithRequest(httpRequest).WithTimes(times);

            var res = SendRequest(new HttpRequestMessage()
                                  .WithMethod("PUT")
                                  .WithPath(CalculatePath("verify"))
                                  .WithBody(VerificationSerializer.Serialize(verification), Encoding.UTF8));

            var body = res?.Content.ReadAsStringAsync().Result;

            if (!string.IsNullOrEmpty(body))
            {
                throw new AssertionException(body);
            }

            return((T)this);
        }
コード例 #3
0
        public void ShouldVerifyAtMostSuccess()
        {
            SendHello(1);

            Assert.NotNull(MockServerClient.Verify(Request()
                                                   .WithMethod(HttpMethod.Get)
                                                   .WithPath("/hello"), VerificationTimes.AtMost(2)));
        }
コード例 #4
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);
        }
コード例 #5
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);
        }
コード例 #6
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);
        }
コード例 #7
0
        public async Task <bool> Verify(HttpRequest request, VerificationTimes verificationTimes)
        {
            var verification       = new Verification(request, verificationTimes);
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, "/verify");

            httpRequestMessage.Content = new StringContent(
                JsonConvert.SerializeObject(verification,
                                            Formatting.Indented,
                                            new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            }),
                Encoding.UTF8,
                "application/json"
                );

            var result = await _httpClient.SendAsync(httpRequestMessage);

            return(result.StatusCode == System.Net.HttpStatusCode.Accepted);
        }
コード例 #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 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);
        }
コード例 #10
0
 /// <inheritdoc />
 public void AtLeast(int value)
 {
     _verificationTimes = VerificationTimes.MoreThan(value);
 }
コード例 #11
0
        public T VerifyZeroInteractions()
        {
            var verification = new Verification().WithRequest(new HttpRequest()).WithTimes(VerificationTimes.Exactly(0));

            var res = SendRequest(new HttpRequestMessage()
                                  .WithMethod("PUT")
                                  .WithPath(CalculatePath("verify"))
                                  .WithBody(VerificationSerializer.Serialize(verification), Encoding.UTF8));

            var body = res?.Content.ReadAsStringAsync().Result;

            if (!string.IsNullOrEmpty(body))
            {
                throw new AssertionException(body);
            }

            return((T)this);
        }
コード例 #12
0
 public Verify(HttpRequest httpRequest, VerificationTimes times)
 {
     Times       = times;
     HttpRequest = httpRequest;
 }
コード例 #13
0
 public async Task <MockServerClient> VerifyZeroInteractionsAsync()
 {
     return(await VerifyAsync(new HttpRequest(), VerificationTimes.Exactly(0)));
 }
コード例 #14
0
 /// <inheritdoc />
 public void Between(int min, int max)
 {
     _verificationTimes = VerificationTimes.Between(min, max);
 }
コード例 #15
0
 public MockServerClient Verify(HttpRequest httpRequest, VerificationTimes times)
 {
     return(VerifyAsync(httpRequest, times).AwaitResult());
 }
コード例 #16
0
 /// <inheritdoc />
 public void Once()
 {
     _verificationTimes = VerificationTimes.Once;
 }
コード例 #17
0
 /// <inheritdoc />
 public void AtMost(int value)
 {
     _verificationTimes = VerificationTimes.LessThan(value);
 }
コード例 #18
0
 /// <inheritdoc />
 public void Twice()
 {
     _verificationTimes = VerificationTimes.Twice;
 }