/// <summary>
        /// Registers an HTTP request interception, replacing any existing registration.
        /// </summary>
        /// <param name="method">The HTTP method to register an interception for.</param>
        /// <param name="uri">The request URI to register an interception for.</param>
        /// <param name="contentStream">A delegate to a method that returns the response stream.</param>
        /// <param name="statusCode">The optional HTTP status code to return.</param>
        /// <param name="mediaType">The optional media type for the content-type.</param>
        /// <param name="responseHeaders">The optional HTTP response headers for the response.</param>
        /// <param name="contentHeaders">The optional HTTP response headers for the content.</param>
        /// <param name="onIntercepted">An optional delegate to invoke when the HTTP message is intercepted.</param>
        /// <returns>
        /// The current <see cref="HttpClientInterceptorOptions"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="method"/>, <paramref name="uri"/> or <paramref name="contentStream"/> is <see langword="null"/>.
        /// </exception>
        public HttpClientInterceptorOptions Register(
            HttpMethod method,
            Uri uri,
            Func <Task <Stream> > contentStream,
            HttpStatusCode statusCode = HttpStatusCode.OK,
            string mediaType          = JsonMediaType,
            IEnumerable <KeyValuePair <string, IEnumerable <string> > > responseHeaders = null,
            IEnumerable <KeyValuePair <string, IEnumerable <string> > > contentHeaders  = null,
            Func <HttpRequestMessage, Task> onIntercepted = null)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

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

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

            var interceptor = new HttpInterceptionResponse()
            {
                ContentStream    = contentStream,
                ContentHeaders   = contentHeaders,
                ContentMediaType = mediaType,
                Method           = method,
                OnIntercepted    = DelegateHelpers.ConvertToBooleanTask(onIntercepted),
                RequestUri       = uri,
                ResponseHeaders  = responseHeaders,
                StatusCode       = statusCode
            };

            string key = BuildKey(method, uri);

            _mappings[key] = interceptor;

            return(this);
        }
Beispiel #2
0
        /// <summary>
        /// Registers an HTTP request interception for an array of bytes, replacing any existing registration.
        /// </summary>
        /// <param name="method">The HTTP method to register an interception for.</param>
        /// <param name="uri">The request URI to register an interception for.</param>
        /// <param name="contentFactory">A delegate to a method that returns the raw response content.</param>
        /// <param name="statusCode">The optional HTTP status code to return.</param>
        /// <param name="mediaType">The optional media type for the content-type.</param>
        /// <param name="responseHeaders">The optional HTTP response headers for the response.</param>
        /// <param name="contentHeaders">The optional HTTP response headers for the content.</param>
        /// <param name="onIntercepted">An optional delegate to invoke when the HTTP message is intercepted.</param>
        /// <returns>
        /// The current <see cref="HttpClientInterceptorOptions"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="method"/>, <paramref name="uri"/> or <paramref name="contentFactory"/> is <see langword="null"/>.
        /// </exception>
        public HttpClientInterceptorOptions RegisterByteArray(
            HttpMethod method,
            Uri uri,
            Func <Task <byte[]> > contentFactory,
            HttpStatusCode statusCode = HttpStatusCode.OK,
            string mediaType          = JsonMediaType,
            IEnumerable <KeyValuePair <string, IEnumerable <string> > >?responseHeaders = null,
            IEnumerable <KeyValuePair <string, IEnumerable <string> > >?contentHeaders  = null,
            Func <HttpRequestMessage, Task>?onIntercepted = null)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

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

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

            var interceptor = new HttpInterceptionResponse()
            {
                ContentFactory   = contentFactory,
                ContentHeaders   = contentHeaders,
                ContentMediaType = mediaType,
                Method           = method,
                OnIntercepted    = DelegateHelpers.ConvertToBooleanTask(onIntercepted),
                RequestUri       = uri,
                ResponseHeaders  = responseHeaders,
                StatusCode       = statusCode,
            };

            ConfigureMatcherAndRegister(interceptor);

            return(this);
        }
Beispiel #3
0
 /// <summary>
 /// Configures the builder to match any request that meets the criteria defined by the specified predicate.
 /// </summary>
 /// <param name="predicate">
 /// A delegate to a method which returns <see langword="true"/> if the
 /// request is considered a match; otherwise <see langword="false"/>.
 /// </param>
 /// <returns>
 /// The current <see cref="HttpRequestInterceptionBuilder"/>.
 /// </returns>
 /// <remarks>
 /// Pass a value of <see langword="null"/> to remove a previously-registered custom request matching predicate.
 /// </remarks>
 public HttpRequestInterceptionBuilder For(Predicate <HttpRequestMessage> predicate)
 {
     _requestMatcher = predicate == null ? null : DelegateHelpers.ConvertToBooleanTask(predicate);
     return(this);
 }
Beispiel #4
0
 /// <summary>
 /// Sets an asynchronous callback to use to use when a request is intercepted.
 /// </summary>
 /// <param name="onIntercepted">A delegate to a method to await when a request is intercepted.</param>
 /// <returns>
 /// The current <see cref="HttpRequestInterceptionBuilder"/>.
 /// </returns>
 public HttpRequestInterceptionBuilder WithInterceptionCallback(Func <HttpRequestMessage, Task> onIntercepted)
 {
     _onIntercepted = DelegateHelpers.ConvertToBooleanTask(onIntercepted);
     return(this);
 }