예제 #1
0
        public void FindNockedWebResponseCorrectlyReturnsRelevantResponseWhenMethodDiffers()
        {
            var getResponse  = "<somexml />";
            var postResponse = "{ a:\"\"}";

            new nock("http://www.nock-fake-domain.co.uk")
            .Get("/path/")
            .Reply(HttpStatusCode.OK, getResponse);

            new nock("http://www.nock-fake-domain.co.uk")
            .Post("/path/")
            .Reply(HttpStatusCode.OK, postResponse);

            Assert.That(nock.NockedRequests.Count, Is.EqualTo(2));

            var request = new NockHttpWebRequest();

            request.RequestUri  = "http://www.nock-fake-domain.co.uk/path/";
            request.InputStream = new MemoryStream();
            request.Method      = "POST";

            var nockMatch = RequestMatcher.FindNockedWebResponse(request);
            var response  = nockMatch.NockedRequest;

            Assert.That(response.Response, Is.EqualTo(postResponse));
            Assert.That(nock.NockedRequests.Count, Is.EqualTo(1));

            request.Method = "GET";

            nockMatch = RequestMatcher.FindNockedWebResponse(request);
            response  = nockMatch.NockedRequest;

            Assert.That(response.Response, Is.EqualTo(getResponse));
            Assert.That(nock.NockedRequests.Count, Is.EqualTo(0));
        }
예제 #2
0
        public void FindNockedWebResponseCorrectlyFiltersBasedOnQueryNameValueExact()
        {
            var nvc = new NameValueCollection();

            nvc.Add("test", "1");

            new nock("http://www.nock-fake-domain.co.uk")
            .Get("/path/")
            .Query(nvc, true)
            .Reply(HttpStatusCode.OK, "I am a test response");

            var request = new NockHttpWebRequest();

            request.RequestUri  = "http://www.nock-fake-domain.co.uk/path/?test=1&test2=2";
            request.Method      = "GET";
            request.InputStream = new MemoryStream();
            request.Headers     = new NameValueCollection();
            request.Query       = new NameValueCollection {
                { "test", "1" }, { "test2", "2" }
            };

            var nockMatch = RequestMatcher.FindNockedWebResponse(request);
            var response  = nockMatch.NockedRequest;

            Assert.That(response, Is.Null);
            Assert.That(nock.NockedRequests.Count, Is.EqualTo(1));
        }
예제 #3
0
        /// <summary>
        /// Gets the RequestMatchResult based on the RequestMessage.
        /// </summary>
        /// <param name="requestMessage">The request message.</param>
        /// <param name="nextState">The Next State.</param>
        /// <returns>The <see cref="RequestMatchResult"/>.</returns>
        public RequestMatchResult GetRequestMatchResult(RequestMessage requestMessage, [CanBeNull] object nextState)
        {
            var result = new RequestMatchResult();

            RequestMatcher.GetMatchingScore(requestMessage, result);

            // Only check state if Scenario is defined
            if (Scenario != null)
            {
                var matcher = new RequestMessageScenarioAndStateMatcher(nextState, ExecutionConditionState);
                matcher.GetMatchingScore(requestMessage, result);
                //// If ExecutionConditionState is null, this means that request is the start from a scenario. So just return.
                //if (ExecutionConditionState != null)
                //{
                //    // ExecutionConditionState is not null, so get score for matching with the nextState.
                //    var matcher = new RequestMessageScenarioAndStateMatcher(nextState, ExecutionConditionState);
                //    matcher.GetMatchingScore(requestMessage, result);
                //}
            }

            if (LogOnMatchFailure)
            {
                if (!result.IsPerfectMatch)
                {
                    result.LogThis = true;
                }
            }

            return(result);
        }
예제 #4
0
        public void FindNockedWebResponseReturnsAValidStandardTestHttpWebResponseObject()
        {
            var headers = new NameValueCollection {
                { "x-custom", "custom-value" }
            };

            new nock("http://www.nock-fake-domain.co.uk")
            .Get("/path/")
            .Reply(HttpStatusCode.Created, "The response", headers);

            var request = new NockHttpWebRequest();

            request.RequestUri  = "http://www.nock-fake-domain.co.uk/path/";
            request.InputStream = new MemoryStream();
            request.Method      = "GET";

            var nockMatch = RequestMatcher.FindNockedWebResponse(request);

            var response = nockMatch.NockedRequest;

            Assert.That(response, Is.Not.Null);
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created));
            Assert.That(response.ResponseHeaders.Count, Is.EqualTo(1));
            Assert.That(response.ResponseHeaders["x-custom"], Is.EqualTo("custom-value"));

            Assert.That(response.Response, Is.EqualTo("The response"));

            Assert.That(nock.NockedRequests.Count, Is.EqualTo(0));
        }
예제 #5
0
        public void FindNockedWebResponseReturnsAValidStandardWebResponseObjectIfRequestAndNockRequestHeadersMatch()
        {
            var requestHeaders = new NameValueCollection
            {
                { "x-custom", "val" }
            };

            new nock("http://www.nock-fake-domain.co.uk")
            .ContentType("application/xml")
            .Post("/path/")
            .MatchHeaders(requestHeaders)
            .Reply(HttpStatusCode.OK, "I am a test response");

            var request = new NockHttpWebRequest();

            request.RequestUri = "http://www.nock-fake-domain.co.uk/path/";
            request.Method     = "POST";
            //request.ContentType = "application/xml";
            request.InputStream = new MemoryStream();
            request.Headers     = new NameValueCollection();
            request.Headers.Add("x-custom", "val");
            request.Headers.Add("Content-Type", "application/xml");

            var nockMatch = RequestMatcher.FindNockedWebResponse(request);
            var response  = nockMatch.NockedRequest;

            Assert.That(response, Is.Not.Null);
            Assert.That(nock.NockedRequests.Count, Is.EqualTo(0));
        }
예제 #6
0
        public void FindNockedWebResponseReturnsAValidStandardTestHttpWebResponseObjectIfBodyInRequestAndNockAreEqual()
        {
            new nock("http://www.nock-fake-domain.co.uk")
            .Post("/path/", "Request body")
            .Reply(HttpStatusCode.OK, "I am a test response");

            var postData = "Request body";
            var bytes    = Encoding.UTF8.GetBytes(postData);

            var request = new NockHttpWebRequest();

            request.RequestUri = "http://www.nock-fake-domain.co.uk/path/";
            request.Method     = "POST";

            MemoryStream ms = new MemoryStream();

            ms.Write(bytes, 0, bytes.Length);
            ms.Position         = 0;
            request.InputStream = ms;

            var nockMatch = RequestMatcher.FindNockedWebResponse(request);
            var response  = nockMatch.NockedRequest;

            Assert.That(response, Is.Not.Null);
            Assert.That(nock.NockedRequests.Count, Is.EqualTo(0));
        }
        public void Should_match_a_specific_handler()
        {
            var expectedRequest = MockRepository.GenerateStub <IRequestHandler>();

            expectedRequest.Method      = "GET";
            expectedRequest.Path        = "/path/specific";
            expectedRequest.QueryParams = new Dictionary <string, string>();
            expectedRequest.Stub(s => s.CanVerifyConstraintsFor("", "")).IgnoreArguments().Return(true);


            var otherRequest = MockRepository.GenerateStub <IRequestHandler>();

            otherRequest.Method      = "GET";
            otherRequest.Path        = "/path/";
            otherRequest.QueryParams = new Dictionary <string, string>();
            otherRequest.Stub(s => s.CanVerifyConstraintsFor("", "")).IgnoreArguments().Return(true);

            var requestMatcher = new RequestMatcher(new EndpointMatchingRule());

            var requestHandlerList = new List <IRequestHandler> {
                otherRequest, expectedRequest
            };


            var httpRequestHead = new HttpRequestHead {
                Method = "GET", Path = "/path/specific", Uri = "/path/specific"
            };

            var matchedRequest = requestMatcher.Match(httpRequestHead, null, requestHandlerList);


            Assert.That(matchedRequest.Path, Is.EqualTo(expectedRequest.Path));
        }
예제 #8
0
        /// <summary>
        /// Determines whether the RequestMessage is handled.
        /// </summary>
        /// <param name="requestMessage">The request message.</param>
        /// <returns>The <see cref="RequestMatchResult"/>.</returns>
        public RequestMatchResult IsRequestHandled(RequestMessage requestMessage)
        {
            var result = new RequestMatchResult();

            RequestMatcher.GetMatchingScore(requestMessage, result);

            return(result);
        }
예제 #9
0
        public void FindNockedWebResponseReturnsANullObjectIfThereAreNoResponseDetailsDefined()
        {
            var request = new NockHttpWebRequest();

            request.RequestUri = "http://www.nock-fake-domain.co.uk/path";

            var match = RequestMatcher.FindNockedWebResponse(request);

            Assert.That(match.NockedRequest, Is.Null);
        }
예제 #10
0
        public void FindNockedWebResponseCorrectlyFiltersBasedOnQueryFunc()
        {
            new nock("http://www.nock-fake-domain.co.uk")
            .Get("/path/")
            .Query((queryDetails) =>
            {
                return(queryDetails.Query["test"] == "2");
            })
            .Reply(HttpStatusCode.OK, "I am a test response");

            var request = new NockHttpWebRequest();

            request.RequestUri  = "http://www.nock-fake-domain.co.uk/path/?test=1";
            request.Method      = "GET";
            request.InputStream = new MemoryStream();
            request.Headers     = new NameValueCollection();
            request.Query       = new NameValueCollection {
                { "test", "1" }
            };

            var nockMatch = RequestMatcher.FindNockedWebResponse(request);
            var response  = nockMatch.NockedRequest;

            Assert.That(response, Is.Null);
            Assert.That(nock.NockedRequests.Count, Is.EqualTo(1));

            nock.ClearAll();

            new nock("http://www.nock-fake-domain.co.uk")
            .Get("/path/")
            .Query((queryDetails) =>
            {
                return(queryDetails.Query["test"] == "2");
            })
            .Reply(HttpStatusCode.OK, "I am a test response");

            request             = new NockHttpWebRequest();
            request.RequestUri  = "http://www.nock-fake-domain.co.uk/path/?test=2";
            request.Method      = "GET";
            request.InputStream = new MemoryStream();
            request.Headers     = new NameValueCollection();
            request.Query       = new NameValueCollection {
                { "test", "2" }
            };

            nockMatch = RequestMatcher.FindNockedWebResponse(request);
            response  = nockMatch.NockedRequest;

            Assert.That(response, Is.Not.Null);
            Assert.That(nock.NockedRequests.Count, Is.EqualTo(0));
        }
 public IViewComponentResult Invoke(RequestMatcher requestMatcher)
 {
     if (requestMatcher is AnyMatcher)
     {
         return(View("AnyMatcher", requestMatcher));
     }
     else if (requestMatcher is XPathMatcher)
     {
         return(View("XPathMatcher", requestMatcher as XPathMatcher));
     }
     else if (requestMatcher is RegexMatcher)
     {
         return(View("RegexMatcher", requestMatcher as RegexMatcher));
     }
     else
     {
         return(View(requestMatcher));
     }
 }
예제 #12
0
        public void FindNockedWebResponseCorrectlyReturnsRelevantResponseWhenContentTypeDiffers()
        {
            var xmlResponse  = "<somexml />";
            var jsonResponse = "{ a:\"\"}";

            new nock("http://www.nock-fake-domain.co.uk")
            .ContentType("application/xml")
            .Get("/path/")
            .Reply(HttpStatusCode.OK, xmlResponse);

            new nock("http://www.nock-fake-domain.co.uk")
            .ContentType("application/json")
            .Get("/path/")
            .Reply(HttpStatusCode.OK, jsonResponse);

            Assert.That(nock.NockedRequests.Count, Is.EqualTo(2));

            var request = new NockHttpWebRequest();

            request.RequestUri  = "http://www.nock-fake-domain.co.uk/path/";
            request.InputStream = new MemoryStream();
            request.Method      = "GET";
            //request.ContentType = "application/json";
            request.Headers.Add("Content-Type", "application/json");

            var nockMatch = RequestMatcher.FindNockedWebResponse(request);
            var response  = nockMatch.NockedRequest;

            Assert.That(response.Response, Is.EqualTo(jsonResponse));
            Assert.That(nock.NockedRequests.Count, Is.EqualTo(1));

            //request.ContentType = "application/xml";
            request.Headers["Content-Type"] = "application/xml";

            nockMatch = RequestMatcher.FindNockedWebResponse(request);
            response  = nockMatch.NockedRequest;

            Assert.That(response.Response, Is.EqualTo(xmlResponse));
            Assert.That(nock.NockedRequests.Count, Is.EqualTo(0));
        }
예제 #13
0
        /// <inheritdoc cref="IMapping.GetRequestMatchResult" />
        public RequestMatchResult GetRequestMatchResult(RequestMessage requestMessage, string nextState)
        {
            var result = new RequestMatchResult();

            RequestMatcher.GetMatchingScore(requestMessage, result);

            // Only check state if Scenario is defined
            if (Scenario != null)
            {
                var matcher = new RequestMessageScenarioAndStateMatcher(nextState, ExecutionConditionState);
                matcher.GetMatchingScore(requestMessage, result);
                //// If ExecutionConditionState is null, this means that request is the start from a scenario. So just return.
                //if (ExecutionConditionState != null)
                //{
                //    // ExecutionConditionState is not null, so get score for matching with the nextState.
                //    var matcher = new RequestMessageScenarioAndStateMatcher(nextState, ExecutionConditionState);
                //    matcher.GetMatchingScore(requestMessage, result);
                //}
            }

            return(result);
        }
        public void Should_match_a_specific_handler()
        {
            var expectedRequest = MockRepository.GenerateStub<IRequestHandler>();
            expectedRequest.Method = "GET";
            expectedRequest.Path = "/path/specific";
            expectedRequest.QueryParams = new Dictionary<string, string>();
            expectedRequest.Stub(s => s.CanVerifyConstraintsFor("")).IgnoreArguments().Return(true);

            var otherRequest = MockRepository.GenerateStub<IRequestHandler>();
            otherRequest.Method = "GET";
            otherRequest.Path = "/path/";
            otherRequest.QueryParams = new Dictionary<string, string>();
            otherRequest.Stub(s => s.CanVerifyConstraintsFor("")).IgnoreArguments().Return(true);

            var requestMatcher = new RequestMatcher(new EndpointMatchingRule());

            var requestHandlerList = new List<IRequestHandler> {  otherRequest, expectedRequest };

            var httpRequestHead = new HttpRequestHead { Method = "GET", Path = "/path/specific", Uri = "/path/specific" };

            var matchedRequest = requestMatcher.Match(httpRequestHead, requestHandlerList);

            Assert.That(matchedRequest.Path, Is.EqualTo(expectedRequest.Path));
        }
 protected override ICollector <RequestEntity> GetTrigger(IContext <RequestEntity> context)
 {
     return(context.CreateCollector(RequestMatcher
                                    .AllOf(RequestMatcher.RequestCreateMapEditor)
                                    .NoneOf(RequestMatcher.Consumed, RequestMatcher.Delay)));
 }
 /// <summary>
 /// Allows for further expectations to be set on the request.
 /// </summary>
 /// <param name="requestMatcher">The request expectations.</param>
 /// <returns>The request expectations.</returns>
 public IRequestActions AndExpect(RequestMatcher requestMatcher)
 {
     ArgumentUtils.AssertNotNull(requestMatcher, "requestMatcher");
     this.requestMatchers.Add(requestMatcher);
     return(this);
 }
예제 #17
0
 public bool Match(IHttpRequest request)
 {
     return(RequestMatcher.Match((request)));
 }
 /// <summary>
 /// Allows for further expectations to be set on the request.
 /// </summary>
 /// <param name="requestMatcher">The request expectations.</param>
 /// <returns>The request expectations.</returns>
 public IRequestActions AndExpect(RequestMatcher requestMatcher)
 {
     ArgumentUtils.AssertNotNull(requestMatcher, "requestMatcher");
     this.requestMatchers.Add(requestMatcher);
     return this;
 }