예제 #1
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));
        }
예제 #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
        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));
        }
예제 #4
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));
        }
예제 #5
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));
        }
예제 #6
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);
        }
예제 #7
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));
        }
예제 #8
0
        public void TestUrlMatching(bool expectation, string requestUrl, string nockedRequestUrl, string nockedRequestPath)
        {
            var webRequest = new NockHttpWebRequest()
            {
                RequestUri = requestUrl
            };
            var nockedRequest = new NockedRequest(nockedRequestUrl)
            {
                Path = nockedRequestPath
            };

            var result = UrlMatcher.IsMatch(webRequest, nockedRequest);

            Assert.That(result, Is.EqualTo(expectation));
        }
예제 #9
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));
        }