Beispiel #1
0
        /// <summary>
        /// Configures a condition for an expectation. If the condition evaluates to <see langword="false"/>, the expectation is not matched.
        /// </summary>
        /// <param name="matching">The request match builder.</param>
        /// <returns>The configured request.</returns>
        public IConfiguredRequest When(Action <RequestMatching> matching)
        {
            if (matching is null)
            {
                throw new ArgumentNullException(nameof(matching));
            }

            var b = new RequestMatching();

            matching(b);

            var newSetup = new HttpCallSequence();

            newSetup.SetMatchers(b.Build());
            _setups.Add(newSetup);
            return(new HttpRequestSetupPhrase(newSetup));
        }
Beispiel #2
0
        /// <summary>
        /// Verifies that a request matching the specified match conditions has been sent.
        /// </summary>
        /// <param name="matching">The conditions to match.</param>
        /// <param name="times">The number of times a request is allowed to be sent.</param>
        /// <param name="because">The reasoning for this expectation.</param>
        public async Task VerifyAsync(Action <RequestMatching> matching, IsSent times, string because = null)
        {
            if (matching is null)
            {
                throw new ArgumentNullException(nameof(matching));
            }

            if (times is null)
            {
                times = IsSent.AtLeastOnce();
            }

            var rm = new RequestMatching();

            matching(rm);

            IReadOnlyCollection <IAsyncHttpRequestMatcher> matchers        = rm.Build();
            IReadOnlyList <IInvokedHttpRequest>            matchedRequests = InvokedRequests;

            if (matchers.Count > 0)
            {
                var list = new List <IInvokedHttpRequest>();
                foreach (IInvokedHttpRequest invokedHttpRequest in InvokedRequests)
                {
                    var requestContext = new MockHttpRequestContext(invokedHttpRequest.Request);
                    if (await matchers.AllAsync(requestContext).ConfigureAwait(false))
                    {
                        list.Add(invokedHttpRequest);
                    }
                }

                matchedRequests = list;
            }

            if (!times.Verify(matchedRequests.Count))
            {
                throw new HttpMockException(times.GetErrorMessage(matchedRequests.Count, BecauseMessage(because)));
            }

            foreach (InvokedHttpRequest r in matchedRequests.Cast <InvokedHttpRequest>())
            {
                r.MarkAsVerified();
            }
        }