コード例 #1
0
        private static RequestMockResponse ConfigureRequestMockResponse(
            this RequestExpectation requestExpectation,
            HttpStatusCode responStatusCode,
            HttpRequestContentType responseContentType,
            object responseContent,
            IDictionary <string, string> responseHeaders,
            Func <HttpRequestMessage, HttpResponseMessage> responseBuilder)
        {
            requestExpectation.Response.ResponseStatusCode  = responStatusCode;
            requestExpectation.Response.ResponseContentType = Helper.ParseResponseContentType(responseContentType);
            requestExpectation.Response.ResponseContent     = responseContent;
            requestExpectation.Response.ResponseBuilder     = responseBuilder;

            if (responseHeaders == null)
            {
                return(requestExpectation.Response);
            }

            foreach (var responseHeader in responseHeaders)
            {
                requestExpectation.Response.ResponseHeaders.Add(responseHeader.Key, responseHeader.Value);
            }

            return(requestExpectation.Response);
        }
コード例 #2
0
        public void HashCodeIgnoringUriCasing()
        {
            var comparer = new RequestExpectationEqualityComparer(new HttpClientTestingFactorySettings {
                IgnoreUriCasing = true
            });

            // Because GetHashCode may result in different values across different platforms or even processes
            // we can't actually do much validation here beyond asserting general contracts.
            var expectation = new RequestExpectation(HttpMethod.Get, new Uri("https://www.foo.com"));

            // Consistency
            Assert.AreEqual(comparer.GetHashCode(expectation), comparer.GetHashCode(expectation));

            // Equivalence
            var expectationCopy = new RequestExpectation(HttpMethod.Get, new Uri("https://www.foo.com"));

            Assert.AreEqual(comparer.GetHashCode(expectation), comparer.GetHashCode(expectationCopy));

            // Case-insensitivity
            var expectationCaseDifferent = new RequestExpectation(HttpMethod.Get, new Uri("https://www.FOO.com"));

            Assert.AreEqual(comparer.GetHashCode(expectation), comparer.GetHashCode(expectationCopy));

            // Non-colliding
            var otherExpectation = new RequestExpectation(HttpMethod.Post, new Uri("https://www.bar.com"));

            Assert.AreNotEqual(comparer.GetHashCode(expectation), comparer.GetHashCode(otherExpectation));
        }
コード例 #3
0
        private static RequestExpectation CreateHttpServerExpectation(HttpServerMock serverMock,
                                                                      HttpMethod requestMethod,
                                                                      string requetsUri,
                                                                      string name,
                                                                      uint times,
                                                                      HttpRequestContentType expectedContentType,
                                                                      object expectedRequestContent,
                                                                      IDictionary <string, string> expectedRequestHeaders,
                                                                      Func <HttpRequestMessage, bool> requestValidator)
        {
            var expectation = new RequestExpectation(requetsUri)
            {
                RequestHttpMethod          = requestMethod,
                ExpectedRequestContent     = expectedRequestContent,
                ExpectedRequestContentType = Helper.ParseRequestContentType(expectedContentType),
                Repeats          = times,
                RequestValidator = requestValidator,
                Name             = name
            };

            if (expectedRequestHeaders != null)
            {
                foreach (var expectedRequestHeader in expectedRequestHeaders)
                {
                    expectation.ExpectedRequestHeaders.Add(expectedRequestHeader.Key, expectedRequestHeader.Value);
                }
            }

            serverMock.ServerRequestsState.RequestExpectations.Add(expectation);

            return(expectation);
        }
コード例 #4
0
        /// <summary>
        /// Validators the specified expectation.
        /// </summary>
        /// <param name="expectation">The expectation.</param>
        /// <param name="requestValidator">The request validator.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">expectation</exception>
        public static RequestExpectation Validator(this RequestExpectation expectation, Func <HttpRequestMessage, bool> requestValidator)
        {
            if (expectation == null)
            {
                throw new ArgumentNullException("expectation");
            }

            expectation.RequestValidator = requestValidator;

            return(expectation);
        }
コード例 #5
0
        /// <summary>
        /// Expecteds the number of calls.
        /// </summary>
        /// <param name="expectation">The expectation.</param>
        /// <param name="repetitions">The repetitions.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">expectation</exception>
        public static RequestExpectation ExpectedNumberOfCalls(this RequestExpectation expectation, uint repetitions)
        {
            if (expectation == null)
            {
                throw new ArgumentNullException("expectation");
            }

            expectation.Repeats = repetitions;

            return(expectation);
        }
コード例 #6
0
        public void Constructor_ValidAbsoluteUrl_Ok()
        {
            const string AbsoluteUrl = "http://192.168.1.1/users/23/data";
            var          requestMock = new RequestExpectation(AbsoluteUrl);

            Assert.IsNull(requestMock.RequestRegex, "The request Regex should be null.");
            Assert.IsFalse(requestMock.IsRequestUriARegex, "The request URI should not be a Regex.");
            Assert.IsNotNull(requestMock.RequestUri, "The request uri should not be null.");
            Assert.AreEqual(AbsoluteUrl, requestMock.RequestUrl, "The request Url is not the expected.");
            Assert.AreEqual(AbsoluteUrl, requestMock.RequestUri.AbsoluteUri, "The request Url is not the expected.");
        }
コード例 #7
0
        public void Constructor_ValidRegex_Ok()
        {
            const string RegexUrl    = "^/users/[0-9]{2}/data$";
            var          requestMock = new RequestExpectation(RegexUrl);

            Assert.IsNotNull(requestMock.RequestRegex, "The request Regex should not be null.");
            Assert.IsTrue(requestMock.IsRequestUriARegex, "The request URI should be a Regex.");
            Assert.IsNull(requestMock.RequestUri, "The request uri should be null.");
            Assert.AreEqual(RegexUrl, requestMock.RequestUrl, "The request Url is not the expected.");
            Assert.AreEqual(RegexUrl, requestMock.RequestRegex.ToString(), "The request regex is not the expected.");
        }
コード例 #8
0
        public void Constructor()
        {
            // Validation
            Assert.ThrowsException <ArgumentNullException>(() => new RequestExpectation(null, this.uri));
            Assert.ThrowsException <ArgumentNullException>(() => new RequestExpectation(this.method, null));

            // Public members
            var expectation = new RequestExpectation(this.method, this.uri);

            Assert.AreEqual(this.method, expectation.HttpMethod);
            Assert.AreEqual(this.uri, expectation.Uri);
        }
コード例 #9
0
        /// <summary>
        /// Expecteds the content.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expectation">The expectation.</param>
        /// <param name="content">The content.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">expectation</exception>
        public static RequestExpectation ExpectedContent <T>(this RequestExpectation expectation, T content, HttpRequestContentType contentType) where T : class
        {
            if (expectation == null)
            {
                throw new ArgumentNullException("expectation");
            }

            expectation.ExpectedRequestContent     = content;
            expectation.ExpectedRequestContentType = Helper.ParseRequestContentType(contentType);

            return(expectation);
        }
コード例 #10
0
        public void EqualsStrictUriCasing()
        {
            var comparer = new RequestExpectationEqualityComparer(new HttpClientTestingFactorySettings {
                IgnoreUriCasing = false
            });
            var expectation = new RequestExpectation(HttpMethod.Get, new Uri("https://www.foo.com/bar"));

            Assert.IsTrue(comparer.Equals(null, null));
            Assert.IsFalse(comparer.Equals(expectation, null));
            Assert.IsFalse(comparer.Equals(null, expectation));
            Assert.IsTrue(comparer.Equals(expectation, expectation));
            Assert.IsTrue(comparer.Equals(expectation, new RequestExpectation(HttpMethod.Get, new Uri("https://www.foo.com/bar"))));
            Assert.IsFalse(comparer.Equals(expectation, new RequestExpectation(HttpMethod.Get, new Uri("https://www.foo.com/BAR"))));
            Assert.IsFalse(comparer.Equals(expectation, new RequestExpectation(HttpMethod.Get, new Uri("https://www.bar.com/bar"))));
            Assert.IsFalse(comparer.Equals(expectation, new RequestExpectation(HttpMethod.Post, new Uri("https://www.foo.com/bar"))));
        }
コード例 #11
0
        /// <summary>
        /// Add a header to the HTTP request expectation.
        /// </summary>
        /// <param name="expectation">The HTTP request expectation.</param>
        /// <param name="headerName">Name of the header.</param>
        /// <param name="headerValue">The header value.</param>
        /// <returns>The HTTP request expectation.</returns>
        /// <exception cref="System.ArgumentNullException">expectation</exception>
        public static RequestExpectation ExpectedRequestHeader(this RequestExpectation expectation, string headerName, string headerValue)
        {
            if (expectation == null)
            {
                throw new ArgumentNullException("expectation");
            }

            if (expectation.ExpectedRequestHeaders.ContainsKey(headerName))
            {
                expectation.ExpectedRequestHeaders[headerName] = headerValue;
                return(expectation);
            }

            expectation.ExpectedRequestHeaders.Add(headerName, headerValue);
            return(expectation);
        }
コード例 #12
0
        /// <summary>
        /// Responses the specified expectation.
        /// </summary>
        /// <param name="expectation">The expectation.</param>
        /// <param name="responseBuilder">The response builder.</param>
        /// <exception cref="System.ArgumentNullException">expectation</exception>
        public static void Response(
            this RequestExpectation expectation,
            Func <HttpRequestMessage, HttpResponseMessage> responseBuilder)
        {
            if (expectation == null)
            {
                throw new ArgumentNullException("expectation");
            }

            ConfigureRequestMockResponse(
                expectation,
                HttpStatusCode.NotImplemented,
                HttpRequestContentType.None,
                null,
                null,
                responseBuilder);
        }
コード例 #13
0
        /// <summary>
        /// Responses the specified expectation.
        /// </summary>
        /// <param name="expectation">The expectation.</param>
        /// <param name="responseStatusCode">The response status code.</param>
        /// <param name="responseHeaders">The response headers.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">expectation</exception>
        public static RequestMockResponse Response(
            this RequestExpectation expectation,
            HttpStatusCode responseStatusCode,
            IDictionary <string, string> responseHeaders = null)
        {
            if (expectation == null)
            {
                throw new ArgumentNullException("expectation");
            }

            return(ConfigureRequestMockResponse(
                       expectation,
                       responseStatusCode,
                       HttpRequestContentType.None,
                       null,
                       responseHeaders,
                       null));
        }
コード例 #14
0
        /// <summary>
        /// Add a collection of headers to the HTTP request expectation.
        /// </summary>
        /// <param name="expectation">The HTTP request expectation.</param>
        /// <param name="headers">The new headers.</param>
        /// <returns>The HTTP request expectation.</returns>
        /// <exception cref="System.ArgumentNullException">expectation</exception>
        public static RequestExpectation ExpectedRequestHeaders(this RequestExpectation expectation, IDictionary <string, string> headers)
        {
            if (expectation == null)
            {
                throw new ArgumentNullException("expectation");
            }

            if (headers == null)
            {
                return(expectation);
            }

            foreach (var expectedHeader in headers)
            {
                expectation.ExpectedRequestHeader(expectedHeader.Key, expectedHeader.Value);
            }

            return(expectation);
        }
コード例 #15
0
        public void HashCodeStrictUriCasing()
        {
            var comparer = new RequestExpectationEqualityComparer(new HttpClientTestingFactorySettings {
                IgnoreUriCasing = false
            });

            // Because GetHashCode may result in different values across different platforms or even processes
            // we can't actually do much validation here beyond asserting general contracts.
            var expectation = new RequestExpectation(HttpMethod.Get, new Uri("https://www.foo.com/bar"));

            // Consistency
            Assert.AreEqual(comparer.GetHashCode(expectation), comparer.GetHashCode(expectation));

            // Equivalence
            Assert.AreEqual(comparer.GetHashCode(expectation), comparer.GetHashCode(new RequestExpectation(HttpMethod.Get, new Uri("https://www.foo.com/bar"))));

            // Case-sensitivity
            Assert.AreNotEqual(comparer.GetHashCode(expectation), comparer.GetHashCode(new RequestExpectation(HttpMethod.Get, new Uri("https://www.foo.com/BAR"))));

            // Non-colliding
            Assert.AreNotEqual(comparer.GetHashCode(expectation), comparer.GetHashCode(new RequestExpectation(HttpMethod.Post, new Uri("https://www.baz.com/bar"))));
        }
コード例 #16
0
        /// <summary>
        /// Expecteds the type of the content.
        /// </summary>
        /// <param name="expectation">The expectation.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <returns></returns>
        public static RequestExpectation ExpectedContentType(this RequestExpectation expectation, HttpRequestContentType contentType)
        {
            expectation.ExpectedRequestContentType = Helper.ParseRequestContentType(contentType);

            return(expectation);
        }
コード例 #17
0
 /// <summary>
 /// Timeds the out.
 /// </summary>
 /// <param name="expectation">The expectation.</param>
 public static void TimedOut(this RequestExpectation expectation)
 {
     expectation.IsRequestTimedOut = true;
 }