Beispiel #1
0
        /// <summary>
        /// Sets the parameters to use as the form URL-encoded response content.
        /// </summary>
        /// <param name="builder">The <see cref="HttpRequestInterceptionBuilder"/> to use.</param>
        /// <param name="parameters">The parameters to use for the form URL-encoded content.</param>
        /// <returns>
        /// The value specified by <paramref name="builder"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="builder"/> or <paramref name="parameters"/> is <see langword="null"/>.
        /// </exception>
        public static HttpRequestInterceptionBuilder WithFormContent(
            this HttpRequestInterceptionBuilder builder,
            IEnumerable <KeyValuePair <string, string> > parameters)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

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

            Func <Task <byte[]> > contentFactory = async() =>
            {
                using (var content = new FormUrlEncodedContent(parameters))
                {
                    return(await content.ReadAsByteArrayAsync().ConfigureAwait(false));
                }
            };

            return(builder
                   .WithMediaType(HttpClientInterceptorOptions.FormMediaType)
                   .WithContent(contentFactory));
        }
Beispiel #2
0
        /// <summary>
        /// Sets the object to use as the response content.
        /// </summary>
        /// <param name="builder">The <see cref="HttpRequestInterceptionBuilder"/> to use.</param>
        /// <param name="content">The object to serialize as JSON as the content.</param>
        /// <param name="settings">The optional <see cref="JsonSerializerSettings"/> to use.</param>
        /// <returns>
        /// The value specified by <paramref name="builder"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="builder"/> or <paramref name="content"/> is <see langword="null"/>.
        /// </exception>
        public static HttpRequestInterceptionBuilder WithNewtonsoftJsonContent(
            this HttpRequestInterceptionBuilder builder,
            object content,
            JsonSerializerSettings?settings = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

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

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

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

            return(builder
                   .WithMediaType(HttpClientInterceptorOptions.JsonMediaType)
                   .WithContent(ContentFactory));
        }
Beispiel #3
0
        /// <summary>
        /// Sets the object to use as the response content.
        /// </summary>
        /// <param name="builder">The <see cref="HttpRequestInterceptionBuilder"/> to use.</param>
        /// <param name="content">The object to serialize as JSON as the content.</param>
        /// <param name="options">The optional <see cref="JsonSerializerOptions"/> to use.</param>
        /// <returns>
        /// The value specified by <paramref name="builder"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="builder"/> or <paramref name="content"/> is <see langword="null"/>.
        /// </exception>
        public static HttpRequestInterceptionBuilder WithSystemTextJsonContent(
            this HttpRequestInterceptionBuilder builder,
            object content,
            JsonSerializerOptions?options = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

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

            byte[] ContentFactory()
            {
                return(JsonSerializer.SerializeToUtf8Bytes(content, options));
            }

            return(builder
                   .WithMediaType(HttpClientInterceptorOptions.JsonMediaType)
                   .WithContent(ContentFactory));
        }