Beispiel #1
0
        /// <summary>
        /// Determines whether the content contains the specified json object.
        /// <paramref name="objectValue"/> is compared by using <c>Equals</c> with the deserialized content of the request.
        /// </summary>
        /// <typeparam name="T">The type of the object the content is compared against.</typeparam>
        /// <param name="content">The http content.</param>
        /// <param name="objectValue">The object the content is compared against.</param>
        /// <param name="jsonSerializerSettings">The settings to use when deserializing.</param>
        /// <returns>
        ///   <c>true</c> if the request deserializes and <c>Equals</c> the objectValue; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        ///   objectValue
        /// </exception>
        public static bool IsJson <T>(this HttpContent content, T objectValue, JsonSerializerSettings jsonSerializerSettings = null)
        {
            if (objectValue == null)
            {
                throw new ArgumentNullException(nameof(objectValue));
            }

            return(content.IsJson <T>(x => objectValue.Equals(x), jsonSerializerSettings));
        }
Beispiel #2
0
        /// <summary>
        /// Read content as a Json object <typeparamref name="TResult"/>.
        /// </summary>
        /// <typeparam name="TResult">
        /// The type which will be used to parse Json string.
        /// </typeparam>
        /// <param name="this">
        /// The http content.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="@this"/> is null.
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// If <paramref name="@this"/> is not "text/plain" or "application/json" content.
        /// </exception>
        public static async Task <TResult> ReadAsJsonObject <TResult>(this HttpContent @this)
        {
            if ([email protected]() && [email protected]())
            {
                throw new NotSupportedException("ReadAsJsonObject supports only text/plain or application/json content.");
            }

            return(JsonConvert.DeserializeObject <TResult>(await @this.ReadAsStringAsync()));
        }
        public void IsJson_HandlesNullContent()
        {
            // Arrange
            HttpContent content = null;

            // Act
            bool actual = content.IsJson();

            // Assert
            Assert.False(actual);
        }
        private static async Task LogContentAsync(StringBuilder log, HttpContent httpContent)
        {
            if (httpContent != null)
            {
                log.AppendLine("    CONTENT HEADERS: ");

                LogHeaders(log, httpContent.Headers);

                if (httpContent.IsPlainText() ||
                    httpContent.IsHtmlText() ||
                    httpContent.IsJson() ||
                    httpContent.IsFormUrlEncoded())
                {
                    var content = await httpContent.ReadAsStringAsync();

                    StorageFolder folder = null;

                    try
                    {
                        folder = (await ApplicationData.Current.LocalFolder.GetFoldersAsync())
                                 .FirstOrDefault(x => string.Equals(x.Name, WebResponseLogs, StringComparison.OrdinalIgnoreCase));
                    }
                    catch (InvalidOperationException)
                    {
                        // Unit tests does not have package identity. We just ignore them.
                    }

                    if (folder != null)
                    {
                        var fileName = string.Format("{0}.log", Guid.NewGuid());
                        var file     = await folder.CreateFileAsync(fileName);

                        await FileIO.WriteTextAsync(file, content);

                        log.AppendFormat("    CONTENT FILE: {0}", file.Path);
                    }
                    else
                    {
                        log.AppendFormat("    CONTENT:{0}{1}", Environment.NewLine, content.Substring(0, Math.Min(4096, content.Length)));
                        log.AppendLine();
                        log.AppendFormat("    ENDCONTENT.");
                        log.AppendLine();
                    }
                }
            }
            else
            {
                log.AppendLine("    CONTENT IS NULL.");
            }
        }
        public static object ToObject(this HttpContent content)
        {
            object contentObject = "Content not human readable.";

            if (content.IsJson())
            {
                contentObject = ThirdParty
                                .LitJson
                                .JsonMapper
                                .ToObject(AsyncHelper.RunSync(content.ReadAsStringAsync));
            }
            else if (content.IsHumanReadable())
            {
                contentObject = AsyncHelper.RunSync(content.ReadAsStringAsync);
            }

            return(contentObject);
        }