| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text; |
| | | 5 | | using System.Net; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using WireMock.Util; |
| | | 8 | | using WireMock.Validation; |
| | | 9 | | |
| | | 10 | | namespace WireMock |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// The request. |
| | | 14 | | /// </summary> |
| | | 15 | | public class RequestMessage |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets the Client IP Address. |
| | | 19 | | /// </summary> |
| | 8 | 20 | | public string ClientIP { get; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the url. |
| | | 24 | | /// </summary> |
| | 26 | 25 | | public string Url { get; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the DateTime. |
| | | 29 | | /// </summary> |
| | 60 | 30 | | public DateTime DateTime { get; set; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the path. |
| | | 34 | | /// </summary> |
| | 183 | 35 | | public string Path { get; } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the method. |
| | | 39 | | /// </summary> |
| | 164 | 40 | | public string Method { get; } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the headers. |
| | | 44 | | /// </summary> |
| | 142 | 45 | | public IDictionary<string, WireMockList<string>> Headers { get; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the cookies. |
| | | 49 | | /// </summary> |
| | 14 | 50 | | public IDictionary<string, string> Cookies { get; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets the query. |
| | | 54 | | /// </summary> |
| | 48 | 55 | | public IDictionary<string, WireMockList<string>> Query { get; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the raw query. |
| | | 59 | | /// </summary> |
| | 143 | 60 | | public string RawQuery { get; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// The body as string. |
| | | 64 | | /// </summary> |
| | 57 | 65 | | public string Body { get; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// The body (as JSON object). |
| | | 69 | | /// </summary> |
| | 109 | 70 | | public object BodyAsJson { get; set; } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// The body (as bytearray). |
| | | 74 | | /// </summary> |
| | 166 | 75 | | public byte[] BodyAsBytes { get; set; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the Host |
| | | 79 | | /// </summary> |
| | 1 | 80 | | public string Host { get; } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the protocol |
| | | 84 | | /// </summary> |
| | 1 | 85 | | public string Protocol { get; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets the port |
| | | 89 | | /// </summary> |
| | 1 | 90 | | public int Port { get; } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets the origin |
| | | 94 | | /// </summary> |
| | 1 | 95 | | public string Origin { get; } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// The body encoding. |
| | | 99 | | /// </summary> |
| | 11 | 100 | | 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> |
| | 78 | 111 | | public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] BodyDat |
| | 78 | 112 | | { |
| | 78 | 113 | | Check.NotNull(url, nameof(url)); |
| | 78 | 114 | | Check.NotNull(method, nameof(method)); |
| | 78 | 115 | | Check.NotNull(clientIP, nameof(clientIP)); |
| | | 116 | | |
| | 78 | 117 | | Url = url.ToString(); |
| | 78 | 118 | | Protocol = url.Scheme; |
| | 78 | 119 | | Host = url.Host; |
| | 78 | 120 | | Port = url.Port; |
| | 78 | 121 | | Origin = $"{url.Scheme}://{url.Host}:{url.Port}"; |
| | 78 | 122 | | Path = WebUtility.UrlDecode(url.AbsolutePath); |
| | 78 | 123 | | Method = method.ToLower(); |
| | 78 | 124 | | ClientIP = clientIP; |
| | | 125 | | |
| | 78 | 126 | | Body = body?.BodyAsString; |
| | 78 | 127 | | BodyEncoding = body?.Encoding; |
| | 78 | 128 | | BodyAsJson = body?.BodyAsJson; |
| | 78 | 129 | | BodyAsBytes = body?.BodyAsBytes; |
| | | 130 | | |
| | 374 | 131 | | Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value)); |
| | 78 | 132 | | Cookies = cookies; |
| | 78 | 133 | | RawQuery = WebUtility.UrlDecode(url.Query); |
| | 78 | 134 | | Query = ParseQuery(RawQuery); |
| | 78 | 135 | | } |
| | | 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> |
| | 65 | 148 | | public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] byte[] |
| | 65 | 149 | | { |
| | 65 | 150 | | Check.NotNull(url, nameof(url)); |
| | 65 | 151 | | Check.NotNull(method, nameof(method)); |
| | 65 | 152 | | Check.NotNull(clientIP, nameof(clientIP)); |
| | | 153 | | |
| | 65 | 154 | | Url = url.ToString(); |
| | 65 | 155 | | Protocol = url.Scheme; |
| | 65 | 156 | | Host = url.Host; |
| | 65 | 157 | | Port = url.Port; |
| | 65 | 158 | | Origin = $"{url.Scheme}://{url.Host}:{url.Port}"; |
| | 65 | 159 | | Path = WebUtility.UrlDecode(url.AbsolutePath); |
| | 65 | 160 | | Method = method.ToLower(); |
| | 65 | 161 | | ClientIP = clientIP; |
| | 65 | 162 | | BodyAsBytes = bodyAsBytes; |
| | 65 | 163 | | Body = body; |
| | 65 | 164 | | BodyEncoding = bodyEncoding; |
| | 81 | 165 | | Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value)); |
| | 65 | 166 | | Cookies = cookies; |
| | 65 | 167 | | RawQuery = WebUtility.UrlDecode(url.Query); |
| | 65 | 168 | | Query = ParseQuery(RawQuery); |
| | 65 | 169 | | } |
| | | 170 | | |
| | | 171 | | private static IDictionary<string, WireMockList<string>> ParseQuery(string queryString) |
| | 143 | 172 | | { |
| | 143 | 173 | | if (string.IsNullOrEmpty(queryString)) |
| | 131 | 174 | | { |
| | 131 | 175 | | return null; |
| | | 176 | | } |
| | | 177 | | |
| | 12 | 178 | | if (queryString.StartsWith("?")) |
| | 12 | 179 | | { |
| | 12 | 180 | | queryString = queryString.Substring(1); |
| | 12 | 181 | | } |
| | | 182 | | |
| | 12 | 183 | | return queryString.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries) |
| | 12 | 184 | | .Aggregate(new Dictionary<string, WireMockList<string>>(), |
| | 12 | 185 | | (dict, term) => |
| | 31 | 186 | | { |
| | 31 | 187 | | string[] parts = term.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries); |
| | 31 | 188 | | string key = parts[0]; |
| | 31 | 189 | | if (!dict.ContainsKey(key)) |
| | 26 | 190 | | { |
| | 26 | 191 | | dict.Add(key, new WireMockList<string>()); |
| | 26 | 192 | | } |
| | 12 | 193 | | |
| | 31 | 194 | | if (parts.Length == 2) |
| | 28 | 195 | | { |
| | 28 | 196 | | string[] values = parts[1].Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); |
| | 28 | 197 | | dict[key].AddRange(values); |
| | 28 | 198 | | } |
| | 12 | 199 | | |
| | 31 | 200 | | return dict; |
| | 31 | 201 | | }); |
| | 143 | 202 | | } |
| | | 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) |
| | 14 | 210 | | { |
| | 14 | 211 | | if (Query == null) |
| | 2 | 212 | | { |
| | 2 | 213 | | return null; |
| | | 214 | | } |
| | | 215 | | |
| | 12 | 216 | | return Query.ContainsKey(key) ? Query[key] : null; |
| | 14 | 217 | | } |
| | | 218 | | } |
| | | 219 | | } |