# WireMock.Util.BodyParser - Coverage Report

# Summary

Class: | WireMock.Util.BodyParser
Assembly: | WireMock.Net
File(s): | C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Util\BodyParser.cs
Covered lines: | 42
Uncovered lines: | 8
Coverable lines: | 50
Total lines: | 81
Line coverage: | 84%
Branch coverage: | 83.3%

# Metrics

Method | Cyclomatic complexity | NPath complexity | Sequence coverage | Branch coverage
--- | --- | --- | --- | ---
.cctor() | 1 | 0 | 100 | 100
ReadStringAsync() | 6 | 2 | 100 | 100
ReadBytesAsync() | 6 | 2 | 100 | 66.67
Parse() | 16 | 16 | 79.31 | 88.89

# File(s)

## C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Util\BodyParser.cs

| # | Line |  | Line coverage
--- | --- | --- | --- | ---
|  | `1` |  | `using System;`
|  | `2` |  | `using System.IO;`
|  | `3` |  | `using System.Linq;`
|  | `4` |  | `using System.Text;`
|  | `5` |  | `using System.Threading.Tasks;`
|  | `6` |  | `using JetBrains.Annotations;`
|  | `7` |  | `using Newtonsoft.Json;`
|  | `8` |  | ``
|  | `9` |  | `namespace WireMock.Util`
|  | `10` |  | `{`
|  | `11` |  | `internal static class BodyParser`
|  | `12` |  | `{`
| 1 | `13` |  | `private static readonly string[] TextContentTypes =`
| 1 | `14` |  | `{`
| 1 | `15` |  | `"text/",`
| 1 | `16` |  | `"application/javascript", "application/typescript",`
| 1 | `17` |  | `"application/xml", "application/xhtml+xml",`
| 1 | `18` |  | `"application/x-www-form-urlencoded"`
| 1 | `19` |  | `};`
|  | `20` |  | ``
|  | `21` |  | `private static async Task<Tuple<string, Encoding>> ReadStringAsync(Stream stream)`
| 14 | `22` |  | `{`
| 14 | `23` |  | `using (var streamReader = new StreamReader(stream))`
| 14 | `24` |  | `{`
| 14 | `25` |  | `string content = await streamReader.ReadToEndAsync();`
|  | `26` |  | ``
| 14 | `27` |  | `return new Tuple<string, Encoding>(content, streamReader.CurrentEncoding);`
|  | `28` |  | `}`
| 14 | `29` |  | `}`
|  | `30` |  | ``
|  | `31` |  | `private static async Task<byte[]> ReadBytesAsync(Stream stream)`
| 5 | `32` |  | `{`
| 5 | `33` |  | `using (var memoryStream = new MemoryStream())`
| 5 | `34` |  | `{`
| 5 | `35` |  | `await stream.CopyToAsync(memoryStream);`
| 5 | `36` |  | `return memoryStream.ToArray();`
|  | `37` |  | `}`
| 5 | `38` |  | `}`
|  | `39` |  | ``
|  | `40` |  | `public static async Task<BodyData> Parse([NotNull] Stream stream, [CanBeNull] string contentTypeHeaderValue)`
| 19 | `41` |  | `{`
| 19 | `42` |  | `var data = new BodyData();`
|  | `43` |  | ``
| 51 | `44` |  | `if (contentTypeHeaderValue != null && TextContentTypes.Any(t => contentTypeHeaderValue.StartsWith(t, StringC`
| 11 | `45` |  | `{`
|  | `46` |  | `try`
| 11 | `47` |  | `{`
| 11 | `48` |  | `var stringData = await ReadStringAsync(stream);`
| 11 | `49` |  | `data.BodyAsString = stringData.Item1;`
| 11 | `50` |  | `data.Encoding = stringData.Item2;`
| 11 | `51` |  | `}`
| 0 | `52` |  | `catch`
| 0 | `53` |  | `{`
|  | `54` |  | `// Reading as string failed, just get the ByteArray.`
| 0 | `55` |  | `data.BodyAsBytes = await ReadBytesAsync(stream);`
| 0 | `56` |  | `}`
| 11 | `57` |  | `}`
| 8 | `58` |  | `else if (contentTypeHeaderValue != null && contentTypeHeaderValue.StartsWith("application/json", StringCompa`
| 3 | `59` |  | `{`
| 3 | `60` |  | `var stringData = await ReadStringAsync(stream);`
| 3 | `61` |  | `data.Encoding = stringData.Item2;`
|  | `62` |  | ``
|  | `63` |  | `try`
| 3 | `64` |  | `{`
| 3 | `65` |  | `data.BodyAsJson = JsonConvert.DeserializeObject(stringData.Item1, new JsonSerializerSettings { Forma`
| 3 | `66` |  | `}`
| 0 | `67` |  | `catch`
| 0 | `68` |  | `{`
|  | `69` |  | `// JsonConvert failed, just set the Body as string.`
| 0 | `70` |  | `data.BodyAsString = stringData.Item1;`
| 0 | `71` |  | `}`
| 3 | `72` |  | `}`
|  | `73` |  | `else`
| 5 | `74` |  | `{`
| 5 | `75` |  | `data.BodyAsBytes = await ReadBytesAsync(stream);`
| 5 | `76` |  | `}`
|  | `77` |  | ``
| 19 | `78` |  | `return data;`
| 19 | `79` |  | `}`
|  | `80` |  | `}`
|  | `81` |  | `}`

# Methods/Properties

.cctor()
ReadStringAsync()
ReadBytesAsync()
Parse()