示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MockHttpServer"/> using specified <paramref name="mockHttpHandler"/> and configures it to listen on specified <paramref name="hostUrl"/>.
        /// </summary>
        /// <param name="mockHttpHandler">The mock http handler.</param>
        /// <param name="loggerFactory">The logger factory to use to log pipeline requests to.</param>
        /// <param name="hostUrl">The host URL the mock HTTP server will listen on.</param>
#pragma warning disable CA1054 // Uri parameters should not be strings
        public MockHttpServer(MockHttpHandler mockHttpHandler, ILoggerFactory loggerFactory, string hostUrl)
#pragma warning restore CA1054 // Uri parameters should not be strings
        {
            Handler         = mockHttpHandler ?? throw new ArgumentNullException(nameof(mockHttpHandler));
            _webHostBuilder = CreateWebHostBuilder(loggerFactory);
            SetHostUrl(hostUrl);
        }
 public InvertRequestMatchingTests()
 {
     _mockHttp   = new MockHttpHandler();
     _httpClient = new HttpClient(_mockHttp)
     {
         BaseAddress = new Uri("http://0.0.0.1")
     };
 }
示例#3
0
 public MockHttpHandlerTests()
 {
     _sut        = new MockHttpHandler();
     _httpClient = new HttpClient(_sut)
     {
         BaseAddress = new Uri("http://0.0.0.0")
     };
 }
示例#4
0
        public void When_creating_server_handler_it_should_set_property()
        {
            var handler = new MockHttpHandler();

            // Act
            var server = new MockHttpServer(handler, "http://127.0.0.1");

            // Assert
            server.Handler.Should().Be(handler);
        }
示例#5
0
        public void When_creating_server_with_null_handler_it_should_throw()
        {
            MockHttpHandler mockHttpHandler = null;

            // Act
            // ReSharper disable once ObjectCreationAsStatement
            // ReSharper disable once ExpressionIsAlwaysNull
            Action act = () => new MockHttpServer(mockHttpHandler, "");

            // Assert
            act.Should().Throw <ArgumentNullException>().WithParamName(nameof(mockHttpHandler));
        }
示例#6
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");
        }
示例#7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MockHttpServer"/> using specified <paramref name="mockHttpHandler"/> and configures it to listen on specified <paramref name="hostUrl"/>.
        /// </summary>
        /// <param name="mockHttpHandler">The mock http handler.</param>
        /// <param name="hostUrl">The host URL the mock HTTP server will listen on.</param>
#pragma warning disable CA1054 // Uri parameters should not be strings
        public MockHttpServer(MockHttpHandler mockHttpHandler, string hostUrl)
#pragma warning restore CA1054 // Uri parameters should not be strings
            : this(mockHttpHandler, null, hostUrl)
        {
        }
示例#8
0
 public InvokedHttpRequestCollection(MockHttpHandler owner)
 {
     _owner = owner ?? throw new ArgumentNullException(nameof(owner));
 }