Exemplo n.º 1
0
    /// <summary>
    /// Registers an HTTP GET request for the specified JSON content.
    /// </summary>
    /// <param name="options">The <see cref="HttpClientInterceptorOptions"/> to set up.</param>
    /// <param name="uriString">The request URL.</param>
    /// <param name="content">The object to serialize as JSON as the content.</param>
    /// <param name="statusCode">The optional HTTP status code to return.</param>
    /// <returns>
    /// The value specified by <paramref name="options"/>.
    /// </returns>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="options"/> or <paramref name="content"/> is <see langword="null"/>.
    /// </exception>
    public static HttpClientInterceptorOptions RegisterGetJson(
        this HttpClientInterceptorOptions options,
        string uriString,
        object content,
        HttpStatusCode statusCode = HttpStatusCode.OK)
    {
        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }

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

        byte[] ContentFactory()
        {
            string json = JsonConvert.SerializeObject(content);

            return(Encoding.UTF8.GetBytes(json));
        }

        return(options.RegisterByteArray(HttpMethod.Get, new Uri(uriString), ContentFactory, statusCode));
    }
Exemplo n.º 2
0
    public static void RegisterByteArray_Throws_If_Uri_Is_Null()
    {
        // Arrange
        var options = new HttpClientInterceptorOptions();

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

        // Act and Assert
        Should.Throw <ArgumentNullException>(() => options.RegisterByteArray(method, uri, contentFactory: EmptyContent), "uri");
    }
Exemplo n.º 3
0
    public static void RegisterByteArray_Throws_If_Method_Is_Null()
    {
        // Arrange
        var options = new HttpClientInterceptorOptions();

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

        // Act and Assert
        Should.Throw <ArgumentNullException>(() => options.RegisterByteArray(method, uri, contentFactory: EmptyContent), "method");
    }
    public static void RegisterByteArray_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;

        // Act and Assert
        Should.Throw <ArgumentNullException>(() => options.RegisterByteArray(method, uri, contentFactory: Array.Empty <byte>, responseHeaders: headers), "options");
    }
Exemplo n.º 5
0
    public static void RegisterByteArray_Throws_If_ContentFactory_Is_Null_Asynchronous()
    {
        // Arrange
        var options = new HttpClientInterceptorOptions();

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

        // Act and Assert
        Should.Throw <ArgumentNullException>(() => options.RegisterByteArray(method, uri, contentFactory), "contentFactory");
    }
Exemplo n.º 6
0
    /// <summary>
    /// Registers an HTTP GET request for the specified string.
    /// </summary>
    /// <param name="options">The <see cref="HttpClientInterceptorOptions"/> to set up.</param>
    /// <param name="uriString">The request URL.</param>
    /// <param name="content">The UTF-8 encoded string to use as the 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>
    /// <returns>
    /// The value specified by <paramref name="options"/>.
    /// </returns>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="options"/> is <see langword="null"/>.
    /// </exception>
    public static HttpClientInterceptorOptions RegisterGet(
        this HttpClientInterceptorOptions options,
        string uriString,
        string content,
        HttpStatusCode statusCode = HttpStatusCode.OK,
        string mediaType          = HttpClientInterceptorOptions.JsonMediaType)
    {
        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }

        return(options.RegisterByteArray(HttpMethod.Get, new Uri(uriString), () => Encoding.UTF8.GetBytes(content ?? string.Empty), statusCode, mediaType));
    }
Exemplo n.º 7
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="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.</param>
    /// <returns>
    /// The current <see cref="HttpClientInterceptorOptions"/>.
    /// </returns>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="options"/>, <paramref name="method"/>, <paramref name="uri"/> or
    /// <paramref name="contentFactory"/> is <see langword="null"/>.
    /// </exception>
    public static HttpClientInterceptorOptions RegisterByteArray(
        this HttpClientInterceptorOptions options,
        HttpMethod method,
        Uri uri,
        Func <byte[]> contentFactory,
        HttpStatusCode statusCode = HttpStatusCode.OK,
        string mediaType          = HttpClientInterceptorOptions.JsonMediaType,
        IEnumerable <KeyValuePair <string, string> >?responseHeaders = null)
    {
        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }

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

        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.RegisterByteArray(
                   method,
                   uri,
                   () => Task.FromResult(contentFactory()),
                   statusCode,
                   mediaType,
                   multivalueHeaders));
    }