public async Task Given_request_has_non_matching_method_when_sending_with_negated_setup_it_should_match_mock(string notMethod, string sendMethod)
        {
            _mockHttp
            .When(m => m.Not.Method(notMethod))
            .Respond(HttpStatusCode.OK)
            .Verifiable();

            // Act
            HttpResponseMessage response = await _httpClient.SendAsync(new HttpRequestMessage(new HttpMethod(sendMethod), ""));

            // Assert
            response.Should().HaveStatusCode(HttpStatusCode.OK);
            _mockHttp.Verify();
            _mockHttp.VerifyNoOtherRequests();
        }
示例#2
0
        public async Task Given_request_is_setup_twice_when_sending_request_last_setup_wins()
        {
            using var httpMock   = new MockHttpHandler();
            using var httpClient = new HttpClient(httpMock)
                  {
                      BaseAddress = new Uri("http://0.0.0.0")
                  };
            httpMock.When(matching => matching.Method("POST")).Respond(HttpStatusCode.OK);
            httpMock.When(matching => matching.Method("POST")).Respond(HttpStatusCode.Accepted);
            httpMock.When(matching => matching.Method("PUT")).Respond(HttpStatusCode.BadRequest);

            // Act
            var response1 = await httpClient.PostAsync("", new StringContent("data 1"));

            var response2 = await httpClient.PostAsync("", new StringContent("data 2"));

            var response3 = await httpClient.PutAsync("", new StringContent("data 3"));

            // Assert
            response1.Should().HaveStatusCode(HttpStatusCode.Accepted, "the second setup wins on first request");
            response2.Should().HaveStatusCode(HttpStatusCode.Accepted, "the second setup wins on second request");
            response3.Should().HaveStatusCode(HttpStatusCode.BadRequest, "the request was sent with different HTTP method matching third setup");
        }
示例#3
0
        public void Given_request_is_configured_to_throw_when_sending_request_should_throw_exception()
        {
            _sut.When(matching => { })
            .Throws <TestableException>()
            .Verifiable();

            // Act
            Func <Task <HttpResponseMessage> > act = () => _httpClient.GetAsync("");

            // Assert
            act.Should().ThrowExactly <TestableException>();
            _sut.Verify(m => { }, IsSent.Once);
            _sut.VerifyNoOtherRequests();
        }