using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using WireMock.Matchers; using WireMock.Matchers.Request; using WireMock.Util; namespace WireMock.RequestBuilders { /// /// The requests. /// public class Request : RequestMessageCompositeMatcher, IRequestBuilder { private readonly IList _requestMatchers; /// /// Creates this instance. /// /// The . public static IRequestBuilder Create() { return new Request(new List()); } /// /// Initializes a new instance of the class. /// /// The request matchers. private Request(IList requestMatchers) : base(requestMatchers) { _requestMatchers = requestMatchers; } /// /// Gets the request message matchers. /// /// Type of IRequestMatcher /// A List{T} public IList GetRequestMessageMatchers() where T : IRequestMatcher { return new ReadOnlyCollection(_requestMatchers.Where(rm => rm is T).Cast().ToList()); } /// /// Gets the request message matcher. /// /// Type of IRequestMatcher /// A RequestMatcher public T GetRequestMessageMatcher() where T : IRequestMatcher { return _requestMatchers.Where(rm => rm is T).Cast().FirstOrDefault(); } /// /// The with url. /// /// The matchers. /// The . public IUrlAndPathRequestBuilder WithUrl(params IMatcher[] matchers) { _requestMatchers.Add(new RequestMessageUrlMatcher(matchers)); return this; } /// /// The with url. /// /// The urls. /// The . public IUrlAndPathRequestBuilder WithUrl(params string[] urls) { _requestMatchers.Add(new RequestMessageUrlMatcher(urls)); return this; } /// /// The with url. /// /// The url func. /// The . public IUrlAndPathRequestBuilder WithUrl(params Func[] funcs) { _requestMatchers.Add(new RequestMessageUrlMatcher(funcs)); return this; } /// /// The with url. /// /// The matcher. /// The . public IUrlAndPathRequestBuilder WithPath(params IMatcher[] matcher) { _requestMatchers.Add(new RequestMessagePathMatcher(matcher)); return this; } /// /// The with path. /// /// The path. /// The . public IUrlAndPathRequestBuilder WithPath(params string[] paths) { _requestMatchers.Add(new RequestMessagePathMatcher(paths)); return this; } /// /// The with path. /// /// The path func. /// The . public IUrlAndPathRequestBuilder WithPath(params Func[] funcs) { _requestMatchers.Add(new RequestMessagePathMatcher(funcs)); return this; } /// /// The using get. /// /// /// The . /// public IHeadersAndCookiesRequestBuilder UsingGet() { _requestMatchers.Add(new RequestMessageVerbMatcher("get")); return this; } /// /// The using post. /// /// /// The . /// public IHeadersAndCookiesRequestBuilder UsingPost() { _requestMatchers.Add(new RequestMessageVerbMatcher("post")); return this; } /// /// The using put. /// /// /// The . /// public IHeadersAndCookiesRequestBuilder UsingPut() { _requestMatchers.Add(new RequestMessageVerbMatcher("put")); return this; } /// /// The using delete. /// /// /// The . /// public IHeadersAndCookiesRequestBuilder UsingDelete() { _requestMatchers.Add(new RequestMessageVerbMatcher("delete")); return this; } /// /// The using head. /// /// /// The . /// public IHeadersAndCookiesRequestBuilder UsingHead() { _requestMatchers.Add(new RequestMessageVerbMatcher("head")); return this; } /// /// The using any verb. /// /// /// The . /// public IHeadersAndCookiesRequestBuilder UsingAnyVerb() { var matchers = _requestMatchers.Where(m => m is RequestMessageVerbMatcher).ToList(); foreach (var matcher in matchers) { _requestMatchers.Remove(matcher); } return this; } /// /// The using verb. /// /// The verbs. /// The . public IHeadersAndCookiesRequestBuilder UsingVerb(params string[] verbs) { _requestMatchers.Add(new RequestMessageVerbMatcher(verbs)); return this; } /// /// The with body. /// /// /// The body. /// /// /// The . /// public IRequestMatcher WithBody(string body) { _requestMatchers.Add(new RequestMessageBodyMatcher(body)); return this; } /// /// The with body byte[]. /// /// /// The body as byte[]. /// /// /// The . /// public IRequestMatcher WithBody(byte[] body) { _requestMatchers.Add(new RequestMessageBodyMatcher(body)); return this; } /// /// The with body. /// /// /// The body function. /// /// /// The . /// public IRequestMatcher WithBody(Func func) { _requestMatchers.Add(new RequestMessageBodyMatcher(func)); return this; } /// /// The with body. /// /// /// The body function. /// /// /// The . /// public IRequestMatcher WithBody(Func func) { _requestMatchers.Add(new RequestMessageBodyMatcher(func)); return this; } /// /// The with body. /// /// The matcher. /// /// The . /// public IRequestMatcher WithBody(IMatcher matcher) { _requestMatchers.Add(new RequestMessageBodyMatcher(matcher)); return this; } /// /// The with parameters. /// /// /// The key. /// /// /// The values. /// /// /// The . /// public IRequestMatcher WithParam(string key, params string[] values) { _requestMatchers.Add(new RequestMessageParamMatcher(key, values.ToList())); return this; } /// /// The with parameters. /// /// The funcs. /// The . public IRequestMatcher WithParam(params Func>, bool>[] funcs) { _requestMatchers.Add(new RequestMessageParamMatcher(funcs)); return this; } /// /// With header. /// /// The name. /// The pattern. /// if set to true [ignore case]. /// public IHeadersAndCookiesRequestBuilder WithHeader(string name, string pattern, bool ignoreCase = true) { _requestMatchers.Add(new RequestMessageHeaderMatcher(name, pattern, ignoreCase)); return this; } /// /// With header. /// /// The funcs. /// public IHeadersAndCookiesRequestBuilder WithHeader(params Func, bool>[] funcs) { _requestMatchers.Add(new RequestMessageHeaderMatcher(funcs)); return this; } /// /// With cookie. /// /// The name. /// The pattern. /// if set to true [ignore case]. /// public IHeadersAndCookiesRequestBuilder WithCookie(string name, string pattern, bool ignoreCase = true) { _requestMatchers.Add(new RequestMessageCookieMatcher(name, pattern, ignoreCase)); return this; } /// /// With header. /// /// The funcs. /// public IHeadersAndCookiesRequestBuilder WithCookie(params Func, bool>[] funcs) { _requestMatchers.Add(new RequestMessageCookieMatcher(funcs)); return this; } } }