| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using WireMock.Util; |
| | | 6 | | using WireMock.Validation; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace WireMock |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// The request. |
| | | 13 | | /// </summary> |
| | | 14 | | public class RequestMessage |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets the url. |
| | | 18 | | /// </summary> |
| | 3 | 19 | | public string Url { get; } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets the DateTime. |
| | | 23 | | /// </summary> |
| | 13 | 24 | | public DateTime DateTime { get; set; } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the path. |
| | | 28 | | /// </summary> |
| | 32 | 29 | | public string Path { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the method. |
| | | 33 | | /// </summary> |
| | 17 | 34 | | public string Method { get; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the headers. |
| | | 38 | | /// </summary> |
| | 13 | 39 | | public IDictionary<string, string> Headers { get; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the cookies. |
| | | 43 | | /// </summary> |
| | 3 | 44 | | public IDictionary<string, string> Cookies { get; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the query. |
| | | 48 | | /// </summary> |
| | 74 | 49 | | public IDictionary<string, WireMockList<string>> Query { get; } = new Dictionary<string, WireMockList<string>>() |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the bodyAsBytes. |
| | | 53 | | /// </summary> |
| | 1 | 54 | | public byte[] BodyAsBytes { get; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the body. |
| | | 58 | | /// </summary> |
| | 13 | 59 | | public string Body { get; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets the body encoding. |
| | | 63 | | /// </summary> |
| | 0 | 64 | | public Encoding BodyEncoding { get; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Initializes a new instance of the <see cref="RequestMessage"/> class. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="url">The original url.</param> |
| | | 70 | | /// <param name="verb">The verb.</param> |
| | | 71 | | /// <param name="bodyAsBytes">The bodyAsBytes byte[].</param> |
| | | 72 | | /// <param name="body">The body string.</param> |
| | | 73 | | /// <param name="bodyEncoding">The body encoding</param> |
| | | 74 | | /// <param name="headers">The headers.</param> |
| | | 75 | | /// <param name="cookies">The cookies.</param> |
| | 56 | 76 | | public RequestMessage([NotNull] Uri url, [NotNull] string verb, [CanBeNull] byte[] bodyAsBytes = null, [CanBeNul |
| | 56 | 77 | | { |
| | 56 | 78 | | Check.NotNull(url, nameof(url)); |
| | 56 | 79 | | Check.NotNull(verb, nameof(verb)); |
| | | 80 | | |
| | 56 | 81 | | Url = url.ToString(); |
| | 56 | 82 | | Path = url.AbsolutePath; |
| | 56 | 83 | | Method = verb.ToLower(); |
| | 56 | 84 | | BodyAsBytes = bodyAsBytes; |
| | 56 | 85 | | Body = body; |
| | 56 | 86 | | BodyEncoding = bodyEncoding; |
| | 56 | 87 | | Headers = headers; |
| | 56 | 88 | | Cookies = cookies; |
| | | 89 | | |
| | 56 | 90 | | string query = url.Query; |
| | 56 | 91 | | if (!string.IsNullOrEmpty(query)) |
| | 5 | 92 | | { |
| | 5 | 93 | | if (query.StartsWith("?")) |
| | 5 | 94 | | { |
| | 5 | 95 | | query = query.Substring(1); |
| | 5 | 96 | | } |
| | | 97 | | |
| | 5 | 98 | | Query = query.Split('&').Aggregate( |
| | 5 | 99 | | new Dictionary<string, WireMockList<string>>(), |
| | 5 | 100 | | (dict, term) => |
| | 16 | 101 | | { |
| | 16 | 102 | | var parts = term.Split('='); |
| | 16 | 103 | | var key = parts[0]; |
| | 16 | 104 | | if (!dict.ContainsKey(key)) |
| | 12 | 105 | | { |
| | 12 | 106 | | dict.Add(key, new WireMockList<string>()); |
| | 12 | 107 | | } |
| | 5 | 108 | | |
| | 16 | 109 | | if (parts.Length == 2) |
| | 15 | 110 | | dict[key].Add(parts[1]); |
| | 5 | 111 | | |
| | 16 | 112 | | return dict; |
| | 16 | 113 | | }); |
| | 5 | 114 | | } |
| | 56 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// The get a query parameter. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="key">The key.</param> |
| | | 121 | | /// <returns>The query parameter.</returns> |
| | | 122 | | public List<string> GetParameter(string key) |
| | 7 | 123 | | { |
| | 7 | 124 | | return Query.ContainsKey(key) ? Query[key] : null; |
| | 7 | 125 | | } |
| | | 126 | | } |
| | | 127 | | } |