コード例 #1
0
ファイル: BodyParser.cs プロジェクト: srikanthku/WireMock.Net
        public static async Task <BodyData> Parse([NotNull] Stream stream, [CanBeNull] string contentType)
        {
            Check.NotNull(stream, nameof(stream));

            var data = new BodyData
            {
                BodyAsBytes      = await ReadBytesAsync(stream),
                DetectedBodyType = BodyType.Bytes,
                DetectedBodyTypeFromContentType = DetectBodyTypeFromContentType(contentType)
            };

            // In case of MultiPart: check if the BodyAsBytes is a valid UTF8 or ASCII string, in that case read as String else keep as-is
            if (data.DetectedBodyTypeFromContentType == BodyType.MultiPart)
            {
                if (BytesEncodingUtils.TryGetEncoding(data.BodyAsBytes, out Encoding encoding) &&
                    SupportedBodyAsStringEncodingForMultipart.Select(x => x.Equals(encoding)).Any())
                {
                    data.BodyAsString     = encoding.GetString(data.BodyAsBytes);
                    data.Encoding         = encoding;
                    data.DetectedBodyType = BodyType.String;

                    return(data);
                }

                return(data);
            }

            // Try to get the body as String
            try
            {
                data.BodyAsString     = DefaultEncoding.GetString(data.BodyAsBytes);
                data.Encoding         = DefaultEncoding;
                data.DetectedBodyType = BodyType.String;

                // If string is not null or empty, try to get as Json
                if (!string.IsNullOrEmpty(data.BodyAsString))
                {
                    try
                    {
                        data.BodyAsJson = JsonConvert.DeserializeObject(data.BodyAsString, new JsonSerializerSettings {
                            Formatting = Formatting.Indented
                        });
                        data.DetectedBodyType = BodyType.Json;
                    }
                    catch
                    {
                        // JsonConvert failed, just ignore.
                    }
                }
            }
            catch
            {
                // Reading as string failed, just ignore
            }

            return(data);
        }
コード例 #2
0
        public static async Task <BodyData> Parse([NotNull] BodyParserSettings settings)
        {
            Check.NotNull(settings, nameof(settings));

            var bodyWithContentEncoding = await ReadBytesAsync(settings.Stream, settings.ContentEncoding, settings.DecompressGZipAndDeflate);

            var data = new BodyData
            {
                BodyAsBytes                     = bodyWithContentEncoding.Value,
                DetectedCompression             = bodyWithContentEncoding.Key,
                DetectedBodyType                = BodyType.Bytes,
                DetectedBodyTypeFromContentType = DetectBodyTypeFromContentType(settings.ContentType)
            };

            // In case of MultiPart: check if the BodyAsBytes is a valid UTF8 or ASCII string, in that case read as String else keep as-is
            if (data.DetectedBodyTypeFromContentType == BodyType.MultiPart)
            {
                if (BytesEncodingUtils.TryGetEncoding(data.BodyAsBytes, out Encoding encoding) &&
                    SupportedBodyAsStringEncodingForMultipart.Select(x => x.Equals(encoding)).Any())
                {
                    data.BodyAsString     = encoding.GetString(data.BodyAsBytes);
                    data.Encoding         = encoding;
                    data.DetectedBodyType = BodyType.String;
                }

                return(data);
            }

            // Try to get the body as String
            try
            {
                data.BodyAsString     = DefaultEncoding.GetString(data.BodyAsBytes);
                data.Encoding         = DefaultEncoding;
                data.DetectedBodyType = BodyType.String;

                // If string is not null or empty, try to deserialize the string to a JObject
                if (settings.DeserializeJson && !string.IsNullOrEmpty(data.BodyAsString))
                {
                    try
                    {
                        data.BodyAsJson       = JsonUtils.DeserializeObject(data.BodyAsString);
                        data.DetectedBodyType = BodyType.Json;
                    }
                    catch
                    {
                        // JsonConvert failed, just ignore.
                    }
                }
            }
            catch
            {
                // Reading as string failed, just ignore
            }

            return(data);
        }