public static void RegisterStream_Throws_If_Options_Is_Null()
    {
        // Arrange
        var method = HttpMethod.Get;
        var uri    = new Uri("https://google.com");

        HttpClientInterceptorOptions options = null;
        IEnumerable <KeyValuePair <string, string> > headers = null;

        Should.Throw <ArgumentNullException>(() => options.RegisterStream(method, uri, contentStream: () => Stream.Null, responseHeaders: headers), "options");
    }
Ejemplo n.º 2
0
    public static void RegisterStream_Throws_If_Uri_Is_Null()
    {
        // Arrange
        var options = new HttpClientInterceptorOptions();

        var method = HttpMethod.Get;
        Uri uri    = null;

        // Act and Assert
        Should.Throw <ArgumentNullException>(() => options.RegisterStream(method, uri, contentStream: EmptyStream), "uri");
    }
Ejemplo n.º 3
0
    public static void RegisterStream_Throws_If_Method_Is_Null()
    {
        // Arrange
        var options = new HttpClientInterceptorOptions();

        HttpMethod method = null;
        var        uri    = new Uri("https://www.just-eat.co.uk");

        // Act and Assert
        Should.Throw <ArgumentNullException>(() => options.RegisterStream(method, uri, contentStream: EmptyStream), "method");
    }
Ejemplo n.º 4
0
    public static void RegisterStream_Throws_If_ContentStream_Is_Null_Asynchronous()
    {
        // Arrange
        var options = new HttpClientInterceptorOptions();

        HttpMethod            method        = HttpMethod.Get;
        Uri                   uri           = new Uri("https://www.just-eat.co.uk");
        Func <Task <Stream> > contentStream = null;

        // Act and Assert
        Should.Throw <ArgumentNullException>(() => options.RegisterStream(method, uri, contentStream), "contentStream");
    }
Ejemplo n.º 5
0
    /// <summary>
    /// Registers an HTTP request interception, replacing any existing registration.
    /// </summary>
    /// <param name="options">The <see cref="HttpClientInterceptorOptions"/> to set up.</param>
    /// <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.</param>
    /// <returns>
    /// The current <see cref="HttpClientInterceptorOptions"/>.
    /// </returns>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="options"/>, <paramref name="method"/>, <paramref name="uri"/> or
    /// <paramref name="contentStream"/> is <see langword="null"/>.
    /// </exception>
    public static HttpClientInterceptorOptions RegisterStream(
        this HttpClientInterceptorOptions options,
        HttpMethod method,
        Uri uri,
        Func <Stream> contentStream,
        HttpStatusCode statusCode = HttpStatusCode.OK,
        string mediaType          = HttpClientInterceptorOptions.JsonMediaType,
        IEnumerable <KeyValuePair <string, string> >?responseHeaders = null)
    {
        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }

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

        IDictionary <string, IEnumerable <string> >?multivalueHeaders = null;

        if (responseHeaders != null)
        {
            multivalueHeaders = new Dictionary <string, IEnumerable <string> >();

            foreach (var pair in responseHeaders)
            {
                multivalueHeaders[pair.Key] = new[] { pair.Value };
            }
        }

        return(options.RegisterStream(
                   method,
                   uri,
                   () => Task.FromResult(contentStream()),
                   statusCode,
                   mediaType,
                   multivalueHeaders));
    }