Summary

Class:WireMock.RequestMessage
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\RequestMessage.cs
Covered lines:94
Uncovered lines:0
Coverable lines:94
Total lines:219
Line coverage:100%
Branch coverage:91.6%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)1332100100
.ctor(...)52100100
ParseQuery(...)4410080
GetParameter(...)4410080

File(s)

C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\RequestMessage.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using System.Net;
 6using JetBrains.Annotations;
 7using WireMock.Util;
 8using WireMock.Validation;
 9
 10namespace WireMock
 11{
 12    /// <summary>
 13    /// The request.
 14    /// </summary>
 15    public class RequestMessage
 16    {
 17        /// <summary>
 18        /// Gets the Client IP Address.
 19        /// </summary>
 820        public string ClientIP { get; }
 21
 22        /// <summary>
 23        /// Gets the url.
 24        /// </summary>
 2625        public string Url { get; }
 26
 27        /// <summary>
 28        /// Gets the DateTime.
 29        /// </summary>
 6030        public DateTime DateTime { get; set; }
 31
 32        /// <summary>
 33        /// Gets the path.
 34        /// </summary>
 18335        public string Path { get; }
 36
 37        /// <summary>
 38        /// Gets the method.
 39        /// </summary>
 16440        public string Method { get; }
 41
 42        /// <summary>
 43        /// Gets the headers.
 44        /// </summary>
 14245        public IDictionary<string, WireMockList<string>> Headers { get; }
 46
 47        /// <summary>
 48        /// Gets the cookies.
 49        /// </summary>
 1450        public IDictionary<string, string> Cookies { get; }
 51
 52        /// <summary>
 53        /// Gets the query.
 54        /// </summary>
 4855        public IDictionary<string, WireMockList<string>> Query { get; }
 56
 57        /// <summary>
 58        /// Gets the raw query.
 59        /// </summary>
 14360        public string RawQuery { get; }
 61
 62        /// <summary>
 63        /// The body as string.
 64        /// </summary>
 5765        public string Body { get; }
 66
 67        /// <summary>
 68        /// The body (as JSON object).
 69        /// </summary>
 10970        public object BodyAsJson { get; set; }
 71
 72        /// <summary>
 73        /// The body (as bytearray).
 74        /// </summary>
 16675        public byte[] BodyAsBytes { get; set; }
 76
 77        /// <summary>
 78        /// Gets the Host
 79        /// </summary>
 180        public string Host { get; }
 81
 82        /// <summary>
 83        /// Gets the protocol
 84        /// </summary>
 185        public string Protocol { get; }
 86
 87        /// <summary>
 88        /// Gets the port
 89        /// </summary>
 190        public int Port { get; }
 91
 92        /// <summary>
 93        /// Gets the origin
 94        /// </summary>
 195        public string Origin { get; }
 96
 97        /// <summary>
 98        /// The body encoding.
 99        /// </summary>
 11100        public Encoding BodyEncoding { get; }
 101
 102        /// <summary>
 103        /// Initializes a new instance of the <see cref="RequestMessage"/> class.
 104        /// </summary>
 105        /// <param name="url">The original url.</param>
 106        /// <param name="method">The HTTP method.</param>
 107        /// <param name="clientIP">The client IP Address.</param>
 108        /// <param name="body">The body.</param>
 109        /// <param name="headers">The headers.</param>
 110        /// <param name="cookies">The cookies.</param>
 78111        public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] BodyDat
 78112        {
 78113            Check.NotNull(url, nameof(url));
 78114            Check.NotNull(method, nameof(method));
 78115            Check.NotNull(clientIP, nameof(clientIP));
 116
 78117            Url = url.ToString();
 78118            Protocol = url.Scheme;
 78119            Host = url.Host;
 78120            Port = url.Port;
 78121            Origin = $"{url.Scheme}://{url.Host}:{url.Port}";
 78122            Path = WebUtility.UrlDecode(url.AbsolutePath);
 78123            Method = method.ToLower();
 78124            ClientIP = clientIP;
 125
 78126             Body = body?.BodyAsString;
 78127             BodyEncoding = body?.Encoding;
 78128             BodyAsJson = body?.BodyAsJson;
 78129             BodyAsBytes = body?.BodyAsBytes;
 130
 374131             Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value));
 78132            Cookies = cookies;
 78133            RawQuery = WebUtility.UrlDecode(url.Query);
 78134            Query = ParseQuery(RawQuery);
 78135        }
 136
 137        /// <summary>
 138        /// Initializes a new instance of the <see cref="RequestMessage"/> class.
 139        /// </summary>
 140        /// <param name="url">The original url.</param>
 141        /// <param name="method">The HTTP method.</param>
 142        /// <param name="clientIP">The client IP Address.</param>
 143        /// <param name="bodyAsBytes">The bodyAsBytes byte[].</param>
 144        /// <param name="body">The body string.</param>
 145        /// <param name="bodyEncoding">The body encoding</param>
 146        /// <param name="headers">The headers.</param>
 147        /// <param name="cookies">The cookies.</param>
 65148        public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] byte[] 
 65149        {
 65150            Check.NotNull(url, nameof(url));
 65151            Check.NotNull(method, nameof(method));
 65152            Check.NotNull(clientIP, nameof(clientIP));
 153
 65154            Url = url.ToString();
 65155            Protocol = url.Scheme;
 65156            Host = url.Host;
 65157            Port = url.Port;
 65158            Origin = $"{url.Scheme}://{url.Host}:{url.Port}";
 65159            Path = WebUtility.UrlDecode(url.AbsolutePath);
 65160            Method = method.ToLower();
 65161            ClientIP = clientIP;
 65162            BodyAsBytes = bodyAsBytes;
 65163            Body = body;
 65164            BodyEncoding = bodyEncoding;
 81165             Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value));
 65166            Cookies = cookies;
 65167            RawQuery = WebUtility.UrlDecode(url.Query);
 65168            Query = ParseQuery(RawQuery);
 65169        }
 170
 171        private static IDictionary<string, WireMockList<string>> ParseQuery(string queryString)
 143172        {
 143173             if (string.IsNullOrEmpty(queryString))
 131174            {
 131175                return null;
 176            }
 177
 12178             if (queryString.StartsWith("?"))
 12179            {
 12180                queryString = queryString.Substring(1);
 12181            }
 182
 12183            return queryString.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries)
 12184                .Aggregate(new Dictionary<string, WireMockList<string>>(),
 12185                (dict, term) =>
 31186                {
 31187                    string[] parts = term.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
 31188                    string key = parts[0];
 31189                     if (!dict.ContainsKey(key))
 26190                    {
 26191                        dict.Add(key, new WireMockList<string>());
 26192                    }
 12193
 31194                     if (parts.Length == 2)
 28195                    {
 28196                        string[] values = parts[1].Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
 28197                        dict[key].AddRange(values);
 28198                    }
 12199
 31200                    return dict;
 31201                });
 143202        }
 203
 204        /// <summary>
 205        /// Get a query parameter.
 206        /// </summary>
 207        /// <param name="key">The key.</param>
 208        /// <returns>The query parameter.</returns>
 209        public WireMockList<string> GetParameter(string key)
 14210        {
 14211             if (Query == null)
 2212            {
 2213                return null;
 214            }
 215
 12216             return Query.ContainsKey(key) ? Query[key] : null;
 14217        }
 218    }
 219}