| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Collections.ObjectModel; |
| | | 4 | | using System.Linq; |
| | | 5 | | using WireMock.Matchers; |
| | | 6 | | using WireMock.Matchers.Request; |
| | | 7 | | using WireMock.Util; |
| | | 8 | | using WireMock.Validation; |
| | | 9 | | |
| | | 10 | | namespace WireMock.RequestBuilders |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// The requests. |
| | | 14 | | /// </summary> |
| | | 15 | | public class Request : RequestMessageCompositeMatcher, IRequestBuilder |
| | | 16 | | { |
| | | 17 | | private readonly IList<IRequestMatcher> _requestMatchers; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Creates this instance. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 23 | | public static IRequestBuilder Create() |
| | 53 | 24 | | { |
| | 53 | 25 | | return new Request(new List<IRequestMatcher>()); |
| | 53 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="Request"/> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="requestMatchers">The request matchers.</param> |
| | 53 | 32 | | private Request(IList<IRequestMatcher> requestMatchers) : base(requestMatchers) |
| | 53 | 33 | | { |
| | 53 | 34 | | _requestMatchers = requestMatchers; |
| | 53 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the request message matchers. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <typeparam name="T">Type of IRequestMatcher</typeparam> |
| | | 41 | | /// <returns>A List{T}</returns> |
| | | 42 | | public IList<T> GetRequestMessageMatchers<T>() where T : IRequestMatcher |
| | 0 | 43 | | { |
| | 0 | 44 | | return new ReadOnlyCollection<T>(_requestMatchers.Where(rm => rm is T).Cast<T>().ToList()); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the request message matcher. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <typeparam name="T">Type of IRequestMatcher</typeparam> |
| | | 51 | | /// <returns>A RequestMatcher</returns> |
| | | 52 | | public T GetRequestMessageMatcher<T>() where T : IRequestMatcher |
| | 0 | 53 | | { |
| | 0 | 54 | | return _requestMatchers.Where(rm => rm is T).Cast<T>().FirstOrDefault(); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// The with path. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="matchers">The matchers.</param> |
| | | 61 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 62 | | public IRequestBuilder WithPath(params IMatcher[] matchers) |
| | 6 | 63 | | { |
| | 6 | 64 | | Check.NotEmpty(matchers, nameof(matchers)); |
| | | 65 | | |
| | 6 | 66 | | _requestMatchers.Add(new RequestMessagePathMatcher(matchers)); |
| | 6 | 67 | | return this; |
| | 6 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// The with path. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="paths">The paths.</param> |
| | | 74 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 75 | | public IRequestBuilder WithPath(params string[] paths) |
| | 25 | 76 | | { |
| | 25 | 77 | | Check.NotEmpty(paths, nameof(paths)); |
| | | 78 | | |
| | 25 | 79 | | _requestMatchers.Add(new RequestMessagePathMatcher(paths)); |
| | 25 | 80 | | return this; |
| | 25 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// The with path. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <param name="funcs">The path func.</param> |
| | | 87 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 88 | | public IRequestBuilder WithPath(params Func<string, bool>[] funcs) |
| | 1 | 89 | | { |
| | 1 | 90 | | Check.NotEmpty(funcs, nameof(funcs)); |
| | | 91 | | |
| | 1 | 92 | | _requestMatchers.Add(new RequestMessagePathMatcher(funcs)); |
| | 1 | 93 | | return this; |
| | 1 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// The with url. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="matchers">The matchers.</param> |
| | | 100 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 101 | | public IRequestBuilder WithUrl(params IMatcher[] matchers) |
| | 0 | 102 | | { |
| | 0 | 103 | | Check.NotEmpty(matchers, nameof(matchers)); |
| | | 104 | | |
| | 0 | 105 | | _requestMatchers.Add(new RequestMessageUrlMatcher(matchers)); |
| | 0 | 106 | | return this; |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// The with url. |
| | | 111 | | /// </summary> |
| | | 112 | | /// <param name="urls">The urls.</param> |
| | | 113 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 114 | | public IRequestBuilder WithUrl(params string[] urls) |
| | 1 | 115 | | { |
| | 1 | 116 | | Check.NotEmpty(urls, nameof(urls)); |
| | | 117 | | |
| | 1 | 118 | | _requestMatchers.Add(new RequestMessageUrlMatcher(urls)); |
| | 1 | 119 | | return this; |
| | 1 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// The with url. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="funcs">The url func.</param> |
| | | 126 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 127 | | public IRequestBuilder WithUrl(params Func<string, bool>[] funcs) |
| | 0 | 128 | | { |
| | 0 | 129 | | Check.NotEmpty(funcs, nameof(funcs)); |
| | | 130 | | |
| | 0 | 131 | | _requestMatchers.Add(new RequestMessageUrlMatcher(funcs)); |
| | 0 | 132 | | return this; |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// The using get. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <returns> |
| | | 139 | | /// The <see cref="IRequestBuilder"/>. |
| | | 140 | | /// </returns> |
| | | 141 | | public IRequestBuilder UsingGet() |
| | 12 | 142 | | { |
| | 12 | 143 | | _requestMatchers.Add(new RequestMessageMethodMatcher("get")); |
| | 12 | 144 | | return this; |
| | 12 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// The using post. |
| | | 149 | | /// </summary> |
| | | 150 | | /// <returns> |
| | | 151 | | /// The <see cref="IRequestBuilder"/>. |
| | | 152 | | /// </returns> |
| | | 153 | | public IRequestBuilder UsingPost() |
| | 1 | 154 | | { |
| | 1 | 155 | | _requestMatchers.Add(new RequestMessageMethodMatcher("post")); |
| | 1 | 156 | | return this; |
| | 1 | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// The using put. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <returns> |
| | | 163 | | /// The <see cref="IRequestBuilder"/>. |
| | | 164 | | /// </returns> |
| | | 165 | | public IRequestBuilder UsingPut() |
| | 3 | 166 | | { |
| | 3 | 167 | | _requestMatchers.Add(new RequestMessageMethodMatcher("put")); |
| | 3 | 168 | | return this; |
| | 3 | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// The using delete. |
| | | 173 | | /// </summary> |
| | | 174 | | /// <returns> |
| | | 175 | | /// The <see cref="IRequestBuilder"/>. |
| | | 176 | | /// </returns> |
| | | 177 | | public IRequestBuilder UsingDelete() |
| | 1 | 178 | | { |
| | 1 | 179 | | _requestMatchers.Add(new RequestMessageMethodMatcher("delete")); |
| | 1 | 180 | | return this; |
| | 1 | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// The using head. |
| | | 185 | | /// </summary> |
| | | 186 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 187 | | public IRequestBuilder UsingHead() |
| | 1 | 188 | | { |
| | 1 | 189 | | _requestMatchers.Add(new RequestMessageMethodMatcher("head")); |
| | 1 | 190 | | return this; |
| | 1 | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// The using any verb. |
| | | 195 | | /// </summary> |
| | | 196 | | /// <returns> |
| | | 197 | | /// The <see cref="IRequestBuilder"/>. |
| | | 198 | | /// </returns> |
| | | 199 | | public IRequestBuilder UsingAnyVerb() |
| | 19 | 200 | | { |
| | 21 | 201 | | var matchers = _requestMatchers.Where(m => m is RequestMessageMethodMatcher).ToList(); |
| | 57 | 202 | | foreach (var matcher in matchers) |
| | 0 | 203 | | { |
| | 0 | 204 | | _requestMatchers.Remove(matcher); |
| | 0 | 205 | | } |
| | | 206 | | |
| | 19 | 207 | | return this; |
| | 19 | 208 | | } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// The using verb. |
| | | 212 | | /// </summary> |
| | | 213 | | /// <param name="verbs">The verbs.</param> |
| | | 214 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 215 | | public IRequestBuilder UsingVerb(params string[] verbs) |
| | 4 | 216 | | { |
| | 4 | 217 | | Check.NotEmpty(verbs, nameof(verbs)); |
| | | 218 | | |
| | 4 | 219 | | _requestMatchers.Add(new RequestMessageMethodMatcher(verbs)); |
| | 4 | 220 | | return this; |
| | 4 | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// The with body. |
| | | 225 | | /// </summary> |
| | | 226 | | /// <param name="body"> |
| | | 227 | | /// The body. |
| | | 228 | | /// </param> |
| | | 229 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 230 | | public IRequestBuilder WithBody(string body) |
| | 2 | 231 | | { |
| | 2 | 232 | | _requestMatchers.Add(new RequestMessageBodyMatcher(body)); |
| | 2 | 233 | | return this; |
| | 2 | 234 | | } |
| | | 235 | | |
| | | 236 | | /// <summary> |
| | | 237 | | /// The with body byte[]. |
| | | 238 | | /// </summary> |
| | | 239 | | /// <param name="body"> |
| | | 240 | | /// The body as byte[]. |
| | | 241 | | /// </param> |
| | | 242 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 243 | | public IRequestBuilder WithBody(byte[] body) |
| | 0 | 244 | | { |
| | 0 | 245 | | _requestMatchers.Add(new RequestMessageBodyMatcher(body)); |
| | 0 | 246 | | return this; |
| | 0 | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// The with body. |
| | | 251 | | /// </summary> |
| | | 252 | | /// <param name="func"> |
| | | 253 | | /// The body function. |
| | | 254 | | /// </param> |
| | | 255 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 256 | | public IRequestBuilder WithBody(Func<string, bool> func) |
| | 0 | 257 | | { |
| | 0 | 258 | | Check.NotNull(func, nameof(func)); |
| | | 259 | | |
| | 0 | 260 | | _requestMatchers.Add(new RequestMessageBodyMatcher(func)); |
| | 0 | 261 | | return this; |
| | 0 | 262 | | } |
| | | 263 | | |
| | | 264 | | /// <summary> |
| | | 265 | | /// The with body. |
| | | 266 | | /// </summary> |
| | | 267 | | /// <param name="func"> |
| | | 268 | | /// The body function. |
| | | 269 | | /// </param> |
| | | 270 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 271 | | public IRequestBuilder WithBody(Func<byte[], bool> func) |
| | 0 | 272 | | { |
| | 0 | 273 | | Check.NotNull(func, nameof(func)); |
| | | 274 | | |
| | 0 | 275 | | _requestMatchers.Add(new RequestMessageBodyMatcher(func)); |
| | 0 | 276 | | return this; |
| | 0 | 277 | | } |
| | | 278 | | |
| | | 279 | | /// <summary> |
| | | 280 | | /// The with body. |
| | | 281 | | /// </summary> |
| | | 282 | | /// <param name="matcher">The matcher.</param> |
| | | 283 | | /// <returns>The <see cref="IRequestBuilder" />.</returns> |
| | | 284 | | public IRequestBuilder WithBody(IMatcher matcher) |
| | 13 | 285 | | { |
| | 13 | 286 | | Check.NotNull(matcher, nameof(matcher)); |
| | | 287 | | |
| | 13 | 288 | | _requestMatchers.Add(new RequestMessageBodyMatcher(matcher)); |
| | 13 | 289 | | return this; |
| | 13 | 290 | | } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// The with parameters. |
| | | 294 | | /// </summary> |
| | | 295 | | /// <param name="key"> |
| | | 296 | | /// The key. |
| | | 297 | | /// </param> |
| | | 298 | | /// <param name="values"> |
| | | 299 | | /// The values. |
| | | 300 | | /// </param> |
| | | 301 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 302 | | public IRequestBuilder WithParam(string key, params string[] values) |
| | 3 | 303 | | { |
| | 3 | 304 | | Check.NotNull(key, nameof(key)); |
| | | 305 | | |
| | 3 | 306 | | _requestMatchers.Add(new RequestMessageParamMatcher(key, values)); |
| | 3 | 307 | | return this; |
| | 3 | 308 | | } |
| | | 309 | | |
| | | 310 | | /// <summary> |
| | | 311 | | /// The with parameters. |
| | | 312 | | /// </summary> |
| | | 313 | | /// <param name="funcs">The funcs.</param> |
| | | 314 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 315 | | public IRequestBuilder WithParam(params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs) |
| | 1 | 316 | | { |
| | 1 | 317 | | Check.NotEmpty(funcs, nameof(funcs)); |
| | | 318 | | |
| | 1 | 319 | | _requestMatchers.Add(new RequestMessageParamMatcher(funcs)); |
| | 1 | 320 | | return this; |
| | 1 | 321 | | } |
| | | 322 | | |
| | | 323 | | /// <summary> |
| | | 324 | | /// With header. |
| | | 325 | | /// </summary> |
| | | 326 | | /// <param name="name">The name.</param> |
| | | 327 | | /// <param name="pattern">The pattern.</param> |
| | | 328 | | /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param> |
| | | 329 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 330 | | public IRequestBuilder WithHeader(string name, string pattern, bool ignoreCase = true) |
| | 4 | 331 | | { |
| | 4 | 332 | | Check.NotNull(name, nameof(name)); |
| | 4 | 333 | | Check.NotNull(pattern, nameof(pattern)); |
| | | 334 | | |
| | 4 | 335 | | _requestMatchers.Add(new RequestMessageHeaderMatcher(name, pattern, ignoreCase)); |
| | 4 | 336 | | return this; |
| | 4 | 337 | | } |
| | | 338 | | |
| | | 339 | | /// <summary> |
| | | 340 | | /// With header. |
| | | 341 | | /// </summary> |
| | | 342 | | /// <param name="name">The name.</param> |
| | | 343 | | /// <param name="matchers">The matchers.</param> |
| | | 344 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 345 | | public IRequestBuilder WithHeader(string name, params IMatcher[] matchers) |
| | 0 | 346 | | { |
| | 0 | 347 | | Check.NotNull(name, nameof(name)); |
| | 0 | 348 | | Check.NotEmpty(matchers, nameof(matchers)); |
| | | 349 | | |
| | 0 | 350 | | _requestMatchers.Add(new RequestMessageHeaderMatcher(name, matchers)); |
| | 0 | 351 | | return this; |
| | 0 | 352 | | } |
| | | 353 | | |
| | | 354 | | /// <summary> |
| | | 355 | | /// With header. |
| | | 356 | | /// </summary> |
| | | 357 | | /// <param name="funcs">The funcs.</param> |
| | | 358 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 359 | | public IRequestBuilder WithHeader(params Func<IDictionary<string, string>, bool>[] funcs) |
| | 0 | 360 | | { |
| | 0 | 361 | | Check.NotEmpty(funcs, nameof(funcs)); |
| | | 362 | | |
| | 0 | 363 | | _requestMatchers.Add(new RequestMessageHeaderMatcher(funcs)); |
| | 0 | 364 | | return this; |
| | 0 | 365 | | } |
| | | 366 | | |
| | | 367 | | /// <summary> |
| | | 368 | | /// With cookie. |
| | | 369 | | /// </summary> |
| | | 370 | | /// <param name="name">The name.</param> |
| | | 371 | | /// <param name="pattern">The pattern.</param> |
| | | 372 | | /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param> |
| | | 373 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 374 | | public IRequestBuilder WithCookie(string name, string pattern, bool ignoreCase = true) |
| | 1 | 375 | | { |
| | 1 | 376 | | _requestMatchers.Add(new RequestMessageCookieMatcher(name, pattern, ignoreCase)); |
| | 1 | 377 | | return this; |
| | 1 | 378 | | } |
| | | 379 | | |
| | | 380 | | /// <summary> |
| | | 381 | | /// With cookie. |
| | | 382 | | /// </summary> |
| | | 383 | | /// <param name="name">The name.</param> |
| | | 384 | | /// <param name="matchers">The matchers.</param> |
| | | 385 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 386 | | public IRequestBuilder WithCookie(string name, params IMatcher[] matchers) |
| | 0 | 387 | | { |
| | 0 | 388 | | Check.NotEmpty(matchers, nameof(matchers)); |
| | | 389 | | |
| | 0 | 390 | | _requestMatchers.Add(new RequestMessageCookieMatcher(name, matchers)); |
| | 0 | 391 | | return this; |
| | 0 | 392 | | } |
| | | 393 | | |
| | | 394 | | /// <summary> |
| | | 395 | | /// With header. |
| | | 396 | | /// </summary> |
| | | 397 | | /// <param name="funcs">The funcs.</param> |
| | | 398 | | /// <returns>The <see cref="IRequestBuilder"/>.</returns> |
| | | 399 | | public IRequestBuilder WithCookie(params Func<IDictionary<string, string>, bool>[] funcs) |
| | 0 | 400 | | { |
| | 0 | 401 | | Check.NotEmpty(funcs, nameof(funcs)); |
| | | 402 | | |
| | 0 | 403 | | _requestMatchers.Add(new RequestMessageCookieMatcher(funcs)); |
| | 0 | 404 | | return this; |
| | 0 | 405 | | } |
| | | 406 | | } |
| | | 407 | | } |