Summary

Class:WireMock.RequestMessage
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\RequestMessage.cs
Covered lines:81
Uncovered lines:0
Coverable lines:81
Total lines:212
Line coverage:100%
Branch coverage:95.8%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
ParseQuery(...)0011
GetParameter(...)0010.75
.ctor(...)0011

File(s)

C:\Users\StefHeyenrath\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.Models;
 8using WireMock.Util;
 9using WireMock.Validation;
 10
 11namespace WireMock
 12{
 13    /// <summary>
 14    /// The RequestMessage.
 15    /// </summary>
 16    public class RequestMessage
 17    {
 18        /// <summary>
 19        /// Gets the Client IP Address.
 20        /// </summary>
 6021        public string ClientIP { get; }
 22
 23        /// <summary>
 24        /// Gets the url (relative).
 25        /// </summary>
 6026        public string Url { get; }
 27
 28        /// <summary>
 29        /// Gets the AbsoluteUrl.
 30        /// </summary>
 5431        public string AbsoluteUrl { get; }
 32
 33        /// <summary>
 34        /// Gets the DateTime.
 35        /// </summary>
 10136        public DateTime DateTime { get; set; }
 37
 38        /// <summary>
 39        /// Gets the path (relative).
 40        /// </summary>
 61541        public string Path { get; }
 42
 43        /// <summary>
 44        /// Gets the AbsolutePath.
 45        /// </summary>
 25546        public string AbsolutePath { get; }
 47
 48        /// <summary>
 49        /// Gets the path segments.
 50        /// </summary>
 351        public string[] PathSegments { get; }
 52
 53        /// <summary>
 54        /// Gets the absolute path segments.
 55        /// </summary>
 156        public string[] AbsolutePathSegments { get; }
 57
 58        /// <summary>
 59        /// Gets the method.
 60        /// </summary>
 35061        public string Method { get; }
 62
 63        /// <summary>
 64        /// Gets the headers.
 65        /// </summary>
 18866        public IDictionary<string, WireMockList<string>> Headers { get; }
 67
 68        /// <summary>
 69        /// Gets the cookies.
 70        /// </summary>
 7471        public IDictionary<string, string> Cookies { get; }
 72
 73        /// <summary>
 74        /// Gets the query.
 75        /// </summary>
 10376        public IDictionary<string, WireMockList<string>> Query { get; }
 77
 78        /// <summary>
 79        /// Gets the raw query.
 80        /// </summary>
 20081        public string RawQuery { get; }
 82
 83        /// <summary>
 84        /// The original body as string, this is defined when Body or BodyAsJson are not null.
 85        /// </summary>
 10786        public string Body { get; }
 87
 88        /// <summary>
 89        /// The body (as JSON object).
 90        /// </summary>
 30091        public object BodyAsJson { get; set; }
 92
 93        /// <summary>
 94        /// The body (as bytearray).
 95        /// </summary>
 27096        public byte[] BodyAsBytes { get; set; }
 97
 98        /// <summary>
 99        /// Gets the Host
 100        /// </summary>
 201101        public string Host { get; }
 102
 103        /// <summary>
 104        /// Gets the protocol
 105        /// </summary>
 201106        public string Protocol { get; }
 107
 108        /// <summary>
 109        /// Gets the port
 110        /// </summary>
 201111        public int Port { get; }
 112
 113        /// <summary>
 114        /// Gets the origin
 115        /// </summary>
 1116        public string Origin { get; }
 117
 118        /// <summary>
 119        /// The body encoding.
 120        /// </summary>
 87121        public Encoding BodyEncoding { get; }
 122
 123        /// <summary>
 124        /// Initializes a new instance of the <see cref="RequestMessage"/> class.
 125        /// </summary>
 126        /// <param name="urlDetails">The original url details.</param>
 127        /// <param name="method">The HTTP method.</param>
 128        /// <param name="clientIP">The client IP Address.</param>
 129        /// <param name="body">The body.</param>
 130        /// <param name="headers">The headers.</param>
 131        /// <param name="cookies">The cookies.</param>
 200132        public RequestMessage([NotNull] UrlDetails urlDetails, [NotNull] string method, [NotNull] string clientIP, [CanB
 200133        {
 200134            Check.NotNull(urlDetails, nameof(urlDetails));
 200135            Check.NotNull(method, nameof(method));
 200136            Check.NotNull(clientIP, nameof(clientIP));
 137
 200138            AbsoluteUrl = urlDetails.AbsoluteUrl.ToString();
 200139            Url = urlDetails.Url.ToString();
 200140            Protocol = urlDetails.Url.Scheme;
 200141            Host = urlDetails.Url.Host;
 200142            Port = urlDetails.Url.Port;
 200143            Origin = $"{Protocol}://{Host}:{Port}";
 144
 200145            AbsolutePath = WebUtility.UrlDecode(urlDetails.AbsoluteUrl.AbsolutePath);
 200146            Path = WebUtility.UrlDecode(urlDetails.Url.AbsolutePath);
 200147            PathSegments = Path.Split('/').Skip(1).ToArray();
 200148            AbsolutePathSegments = AbsolutePath.Split('/').Skip(1).ToArray();
 149
 200150            Method = method;
 200151            ClientIP = clientIP;
 152
 200153            Body = body?.BodyAsString;
 200154            BodyEncoding = body?.Encoding;
 200155            BodyAsJson = body?.BodyAsJson;
 200156            BodyAsBytes = body?.BodyAsBytes;
 157
 366158            Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value));
 200159            Cookies = cookies;
 200160            RawQuery = WebUtility.UrlDecode(urlDetails.Url.Query);
 200161            Query = ParseQuery(RawQuery);
 200162        }
 163
 164        private static IDictionary<string, WireMockList<string>> ParseQuery(string queryString)
 200165        {
 200166            if (string.IsNullOrEmpty(queryString))
 187167            {
 187168                return null;
 169            }
 170
 13171            if (queryString.StartsWith("?"))
 13172            {
 13173                queryString = queryString.Substring(1);
 13174            }
 175
 13176            return queryString.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries)
 13177                .Aggregate(new Dictionary<string, WireMockList<string>>(),
 13178                (dict, term) =>
 33179                {
 33180                    string[] parts = term.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
 33181                    string key = parts[0];
 33182                    if (!dict.ContainsKey(key))
 28183                    {
 28184                        dict.Add(key, new WireMockList<string>());
 28185                    }
 13186
 33187                    if (parts.Length == 2)
 30188                    {
 30189                        string[] values = parts[1].Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
 30190                        dict[key].AddRange(values);
 30191                    }
 13192
 33193                    return dict;
 33194                });
 200195        }
 196
 197        /// <summary>
 198        /// Get a query parameter.
 199        /// </summary>
 200        /// <param name="key">The key.</param>
 201        /// <returns>The query parameter.</returns>
 202        public WireMockList<string> GetParameter(string key)
 16203        {
 16204            if (Query == null)
 2205            {
 2206                return null;
 207            }
 208
 14209            return Query.ContainsKey(key) ? Query[key] : null;
 16210        }
 211    }
 212}