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