/// <summary>
        /// Validates that requests have been made, throws an exception when no requests were made.
        /// </summary>
        /// <param name="handler">The <see cref="TestableHttpMessageHandler"/> that should be asserted.</param>
        /// <param name="expectedNumberOfRequests">The expected number of requests.</param>
        /// <returns>An <see cref="IHttpRequestMessagesCheck"/> that can be used for additional assertions.</returns>
        /// <exception cref="ArgumentNullException">handler is `null`</exception>
        /// <exception cref="HttpRequestMessageAssertionException">When no requests are made</exception>
        public static IHttpRequestMessagesCheck ShouldHaveMadeRequests(this TestableHttpMessageHandler handler, int expectedNumberOfRequests)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            return(new HttpRequestMessageAsserter(handler.Requests).WithRequestUri("*", expectedNumberOfRequests));
        }
        /// <summary>
        /// Simulate a timeout on the request by throwing a TaskCanceledException when a request is received.
        /// </summary>
        /// <param name="handler">The <see cref="TestableHttpMessageHandler"/> that should be configured.</param>
        public static void SimulateTimeout(this TestableHttpMessageHandler handler)
        {
            if (handler is null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            handler.RespondWith(_ => new TimeoutHttpResponseMessage());
        }
        /// <summary>
        /// Configure the <see cref="HttpResponseMessage"/> that should be returned for each request.
        /// </summary>
        /// <param name="handler">The <see cref="TestableHttpMessageHandler"/> that should be configured.</param>
        /// <param name="httpResponseMessage">The response message to return.</param>
        /// <remarks>The response is actually the exact same response for every single request and will not be modified by TestableHttpMessageHandler.</remarks>
        public static void RespondWith(this TestableHttpMessageHandler handler, HttpResponseMessage httpResponseMessage)
        {
            if (handler is null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            if (httpResponseMessage is null)
            {
                throw new ArgumentNullException(nameof(httpResponseMessage));
            }

            handler.RespondWith(_ => httpResponseMessage);
        }
        /// <summary>
        /// Validates that requests to a specific uri have been made, throws an exception when no requests were made.
        /// </summary>
        /// <param name="handler">The <see cref="TestableHttpMessageHandler"/> that should be asserted.</param>
        /// <param name="pattern">The uri pattern to validate against, the pattern supports *.</param>
        /// <returns>An <see cref="IHttpRequestMessagesCheck"/> that can be used for additional assertions.</returns>
        /// <exception cref="ArgumentNullException">handler is `null` or pattern is `null`</exception>
        /// <exception cref="HttpRequestMessageAssertionException">When no requests are made</exception>
        public static IHttpRequestMessagesCheck ShouldHaveMadeRequestsTo(this TestableHttpMessageHandler handler, string pattern)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            return(new HttpRequestMessageAsserter(handler.Requests).WithRequestUri(pattern));
        }
Пример #5
0
        /// <summary>
        /// Create and configure an <seealso cref="HttpClient"/> configured with the TestableHttpMessageHandler.
        /// </summary>
        /// <param name="handler">The TestableHttpMessageHandler to set on the client.</param>
        /// <param name="configureClient">A delegate that is used to configure an HttpClient</param>
        /// <returns>An HttpClient configure with the TestableHttpMessageHandler.</returns>
        /// <exception cref="ArgumentNullException">The `handler` or `configureClient` is `null`</exception>
        public static HttpClient CreateClient(this TestableHttpMessageHandler handler, Action <HttpClient> configureClient)
        {
            if (handler is null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            if (configureClient is null)
            {
                throw new ArgumentNullException(nameof(configureClient));
            }

            var httpClient = new HttpClient(handler);

            configureClient(httpClient);

            return(httpClient);
        }
        /// <summary>
        /// Configure the <see cref="HttpResponseMessage"/> that should be returned using a <see cref="HttpResponseMessageBuilder"/>.
        /// </summary>
        /// <param name="handler">The <see cref="TestableHttpMessageHandler"/> that should be configured.</param>
        /// <param name="httpResponseMessageBuilderAction">An action that calls methods on the <see cref="HttpResponseMessageBuilder"/>.</param>
        public static void RespondWith(this TestableHttpMessageHandler handler, Action <HttpResponseMessageBuilder> httpResponseMessageBuilderAction)
        {
            if (handler is null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            if (httpResponseMessageBuilderAction is null)
            {
                throw new ArgumentNullException(nameof(httpResponseMessageBuilderAction));
            }

            handler.RespondWith(request =>
            {
                var builder = new HttpResponseMessageBuilder().WithRequestMessage(request);
                httpResponseMessageBuilderAction(builder);
                return(builder.Build());
            });
        }
 public static void ShouldNotHaveMadeRequests(this TestableHttpMessageHandler handler)
 {
     _ = handler.ShouldHaveMadeRequests(0);
 }
 public static void ShouldNotHaveMadeRequestsTo(this TestableHttpMessageHandler handler, string pattern)
 {
     _ = handler.ShouldHaveMadeRequestsTo(pattern, 0);
 }
Пример #9
0
 /// <summary>
 /// Create an <seealso cref="HttpClient"/> configured with the TestableHttpMessageHandler.
 /// </summary>
 /// <param name="handler">The TestableHttpMessageHandler to set on the client.</param>
 /// <returns>An HttpClient configure with the TestableHttpMessageHandler.</returns>
 /// <exception cref="ArgumentNullException">The `handler` is `null`</exception>
 /// <remarks>Using this method is equivalent to `new HttClient(handler)`.</remarks>
 public static HttpClient CreateClient(this TestableHttpMessageHandler handler)
 {
     return(CreateClient(handler, _ => { }));
 }