# WireMock.Http.HttpRequestMessageHelper - Coverage Report

# Summary

Class: | WireMock.Http.HttpRequestMessageHelper
Assembly: | WireMock.Net
File(s): | C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Http\HttpRequestMessageHelper.cs
Covered lines: | 50
Uncovered lines: | 5
Coverable lines: | 55
Total lines: | 86
Line coverage: | 90.9%
Branch coverage: | 87.5%

# Metrics

Method | Cyclomatic complexity | NPath complexity | Sequence coverage | Branch coverage
--- | --- | --- | --- | ---
Create(...) | 20 | 4096 | 91.38 | 88

# File(s)

## C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Http\HttpRequestMessageHelper.cs

| # | Line |  | Line coverage
--- | --- | --- | --- | ---
|  | `1` |  | `using System;`
|  | `2` |  | `using System.Collections.Generic;`
|  | `3` |  | `using System.Linq;`
|  | `4` |  | `using System.Net.Http;`
|  | `5` |  | `using System.Text;`
|  | `6` |  | `using MimeKit;`
|  | `7` |  | `using Newtonsoft.Json;`
|  | `8` |  | ``
|  | `9` |  | `namespace WireMock.Http`
|  | `10` |  | `{`
|  | `11` |  | `internal static class HttpRequestMessageHelper`
|  | `12` |  | `{`
|  | `13` |  | `public static HttpRequestMessage Create(RequestMessage requestMessage, string url)`
| 15 | `14` |  | `{`
| 15 | `15` |  | `var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url);`
|  | `16` |  | ``
| 15 | `17` |  | `ContentType contentType = null;`
| 15 | `18` |  | `if (requestMessage.Headers != null && requestMessage.Headers.ContainsKey(HttpKnownHeaderNames.ContentType))`
| 7 | `19` |  | `{`
| 7 | `20` |  | `var value = requestMessage.Headers[HttpKnownHeaderNames.ContentType].FirstOrDefault();`
| 7 | `21` |  | `ContentType.TryParse(value, out contentType);`
| 7 | `22` |  | `}`
|  | `23` |  | ``
|  | `24` |  | `// Set Body if present`
| 15 | `25` |  | `if (requestMessage.BodyAsBytes != null)`
| 1 | `26` |  | `{`
| 1 | `27` |  | `httpRequestMessage.Content = new ByteArrayContent(requestMessage.BodyAsBytes);`
| 1 | `28` |  | `}`
| 14 | `29` |  | `else if (requestMessage.BodyAsJson != null)`
| 2 | `30` |  | `{`
| 2 | `31` |  | `if (contentType != null)`
| 1 | `32` |  | `{`
| 1 | `33` |  | `var encoding = requestMessage.BodyEncoding ?? Encoding.GetEncoding(contentType.Charset ?? "UTF-8");`
| 1 | `34` |  | `httpRequestMessage.Content = new StringContent(JsonConvert.SerializeObject(requestMessage.BodyAsJson`
| 1 | `35` |  | `}`
|  | `36` |  | `else`
| 1 | `37` |  | `{`
| 1 | `38` |  | `httpRequestMessage.Content = new StringContent(JsonConvert.SerializeObject(requestMessage.BodyAsJson`
| 1 | `39` |  | `}`
| 2 | `40` |  | `}`
| 12 | `41` |  | `else if (requestMessage.Body != null)`
| 7 | `42` |  | `{`
| 7 | `43` |  | `if (contentType != null)`
| 6 | `44` |  | `{`
| 6 | `45` |  | `var encoding = requestMessage.BodyEncoding ?? Encoding.GetEncoding(contentType.Charset ?? "UTF-8");`
| 6 | `46` |  | `httpRequestMessage.Content = new StringContent(requestMessage.Body, encoding, contentType.MimeType);`
| 6 | `47` |  | `}`
|  | `48` |  | `else`
| 1 | `49` |  | `{`
| 1 | `50` |  | `httpRequestMessage.Content = new StringContent(requestMessage.Body, requestMessage.BodyEncoding);`
| 1 | `51` |  | `}`
| 7 | `52` |  | `}`
|  | `53` |  | ``
|  | `54` |  | `// Overwrite the host header`
| 15 | `55` |  | `httpRequestMessage.Headers.Host = new Uri(url).Authority;`
|  | `56` |  | ``
|  | `57` |  | `// Set other headers if present and if not excluded`
| 15 | `58` |  | `if (requestMessage.Headers == null || requestMessage.Headers.Count == 0)`
| 2 | `59` |  | `{`
| 2 | `60` |  | `return httpRequestMessage;`
|  | `61` |  | `}`
|  | `62` |  | ``
| 13 | `63` |  | `var excludeHeaders = new List<string> { HttpKnownHeaderNames.Host, HttpKnownHeaderNames.ContentLength };`
| 13 | `64` |  | `if (contentType != null)`
| 7 | `65` |  | `{`
| 7 | `66` |  | `excludeHeaders.Add(HttpKnownHeaderNames.ContentType);`
| 7 | `67` |  | `}`
|  | `68` |  | ``
| 102 | `69` |  | `foreach (var header in requestMessage.Headers.Where(h => !excludeHeaders.Contains(h.Key, StringComparer.Ordi`
| 15 | `70` |  | `{`
|  | `71` |  | `// Try to add to request headers. If failed - try to add to content headers`
| 15 | `72` |  | `if (httpRequestMessage.Headers.Contains(header.Key))`
| 0 | `73` |  | `{`
| 0 | `74` |  | `continue;`
|  | `75` |  | `}`
|  | `76` |  | ``
| 15 | `77` |  | `if (!httpRequestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value))`
| 0 | `78` |  | `{`
| 0 | `79` |  | `httpRequestMessage.Content.Headers.TryAddWithoutValidation(header.Key, header.Value);`
| 0 | `80` |  | `}`
| 15 | `81` |  | `}`
|  | `82` |  | ``
| 13 | `83` |  | `return httpRequestMessage;`
| 15 | `84` |  | `}`
|  | `85` |  | `}`
|  | `86` |  | `}`

# Methods/Properties

Create(WireMock.RequestMessage,System.String)