| | | 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) |
| | 7 | 14 | | { |
| | 7 | 15 | | var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url); |
| | | 16 | | |
| | 7 | 17 | | ContentType contentType = null; |
| | 7 | 18 | | if (requestMessage.Headers != null && requestMessage.Headers.ContainsKey(HttpKnownHeaderNames.ContentType)) |
| | 4 | 19 | | { |
| | 4 | 20 | | var value = requestMessage.Headers[HttpKnownHeaderNames.ContentType].FirstOrDefault(); |
| | 4 | 21 | | ContentType.TryParse(value, out contentType); |
| | 4 | 22 | | } |
| | | 23 | | |
| | | 24 | | // Set Body if present |
| | 7 | 25 | | if (requestMessage.BodyAsBytes != null) |
| | 1 | 26 | | { |
| | 1 | 27 | | httpRequestMessage.Content = new ByteArrayContent(requestMessage.BodyAsBytes); |
| | 1 | 28 | | } |
| | 6 | 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 | | } |
| | 4 | 41 | | else if (requestMessage.Body != null) |
| | 4 | 42 | | { |
| | 4 | 43 | | if (contentType != null) |
| | 3 | 44 | | { |
| | 3 | 45 | | var encoding = requestMessage.BodyEncoding ?? Encoding.GetEncoding(contentType.Charset ?? "UTF-8"); |
| | 3 | 46 | | httpRequestMessage.Content = new StringContent(requestMessage.Body, encoding, contentType.MimeType); |
| | 3 | 47 | | } |
| | | 48 | | else |
| | 1 | 49 | | { |
| | 1 | 50 | | httpRequestMessage.Content = new StringContent(requestMessage.Body, requestMessage.BodyEncoding); |
| | 1 | 51 | | } |
| | 4 | 52 | | } |
| | | 53 | | |
| | | 54 | | // Overwrite the host header |
| | 7 | 55 | | httpRequestMessage.Headers.Host = new Uri(url).Authority; |
| | | 56 | | |
| | | 57 | | // Set other headers if present and if not excluded |
| | 7 | 58 | | if (requestMessage.Headers == null || requestMessage.Headers.Count == 0) |
| | 2 | 59 | | { |
| | 2 | 60 | | return httpRequestMessage; |
| | | 61 | | } |
| | | 62 | | |
| | 5 | 63 | | var excludeHeaders = new List<string> { HttpKnownHeaderNames.Host, HttpKnownHeaderNames.ContentLength }; |
| | 5 | 64 | | if (contentType != null) |
| | 4 | 65 | | { |
| | 4 | 66 | | excludeHeaders.Add(HttpKnownHeaderNames.ContentType); |
| | 4 | 67 | | } |
| | | 68 | | |
| | 22 | 69 | | foreach (var header in requestMessage.Headers.Where(h => !excludeHeaders.Contains(h.Key, StringComparer.Ordi |
| | 1 | 70 | | { |
| | | 71 | | // Try to add to request headers. If failed - try to add to content headers |
| | 1 | 72 | | if (httpRequestMessage.Headers.Contains(header.Key)) |
| | 0 | 73 | | { |
| | 0 | 74 | | continue; |
| | | 75 | | } |
| | | 76 | | |
| | 1 | 77 | | if (!httpRequestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value)) |
| | 0 | 78 | | { |
| | 0 | 79 | | httpRequestMessage.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); |
| | 0 | 80 | | } |
| | 1 | 81 | | } |
| | | 82 | | |
| | 5 | 83 | | return httpRequestMessage; |
| | 7 | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |