コード例 #1
0
        public void ApplyPost200()
        {
            var connector = new BasicConnector(HttpTransportMock.Object, Digest, Secret);

            ResourceMock.SetupProperty(r => r.Location, Url);
            ResourceMock.SetupGet(r => r.ContentType).Returns(ContentType);
            var resourceData = new Dictionary<string, object>() { { "Year", 2012 } };
            ResourceMock.Setup(r => r.Marshal()).Returns(resourceData);

            var request = (HttpWebRequest)WebRequest.Create(Url);
            HttpTransportMock.Setup(t => t.CreateRequest(Url)).Returns(request);
            PayLoad = "{\"Year\":2012}";
            ResponseMock.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request, PayLoad)).Returns(ResponseMock.Object);

            connector.Apply(HttpMethod.Post, ResourceMock.Object, null);

            HttpTransportMock.Verify(t => t.CreateRequest(Url), Times.Once());
            HttpTransportMock.Verify(t => t.Send(request, PayLoad), Times.Once());

            Assert.That(request.Method, Is.EqualTo(HttpMethod.Post.ToString().ToUpper()));
            Assert.That(request.UserAgent, Is.EqualTo(connector.UserAgent.ToString()));
            var authorization = string.Format("Klarna {0}", Digest.Create(string.Concat(PayLoad, Secret)));
            Assert.That(request.Headers["Authorization"], Is.EqualTo(authorization));
            Assert.That(request.Accept, Is.EqualTo(ContentType));
            Assert.That(request.ContentType, Is.EqualTo(ContentType));
        }
コード例 #2
0
        public void ApplyGet200InvalidJson()
        {
            var connector = new BasicConnector(HttpTransportMock.Object, Digest, Secret);

            ResourceMock.SetupProperty(r => r.Location, Url);
            ResourceMock.SetupGet(r => r.ContentType).Returns(ContentType);

            var request = (HttpWebRequest)WebRequest.Create(Url);
            HttpTransportMock.Setup(t => t.CreateRequest(Url)).Returns(request);
            ResponseMock.SetupGet(r => r.Data).Returns("{{{{");
            HttpTransportMock.Setup(t => t.Send(request, PayLoad)).Returns(ResponseMock.Object);

            var ex = Assert.Throws<ConnectorException>(() => connector.Apply(HttpMethod.Get, ResourceMock.Object, null));

            Assert.That(ex.Message, Is.EqualTo("Bad format on response content."));
        }
コード例 #3
0
        public void ApplyDataInOptions()
        {
            var connector = new BasicConnector(HttpTransportMock.Object, Digest, Secret);

            ResourceMock.SetupProperty(r => r.Location, Url);

            var request = (HttpWebRequest)WebRequest.Create(Url);
            HttpTransportMock.Setup(t => t.CreateRequest(Url)).Returns(request);
            PayLoad = "{\"Year\":2012}";
            ResponseMock.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request, PayLoad)).Returns(ResponseMock.Object);

            ResourceMock.Setup(r => r.Marshal()).Returns(new Dictionary<string, object>());

            var resourceData = new Dictionary<string, object>() { { "Year", 2012 } };
            var options = new Dictionary<string, object>() { { "data", resourceData } };
            connector.Apply(HttpMethod.Post, ResourceMock.Object, options);

            HttpTransportMock.Verify(t => t.CreateRequest(Url), Times.Once());
            HttpTransportMock.Verify(t => t.Send(request, PayLoad), Times.Once());
        }
コード例 #4
0
        public void UserAgent()
        {
            var connector =
                new BasicConnector(HttpTransportMock.Object, Digest, "aboogie");
            Assert.That(connector.UserAgent, Is.Not.Null);

            connector.UserAgent.AddField("Module", "Magento", "5.0",
                new[] { "LanguagePack/7", "JsLib/2.0" });

            var userAgentString = connector.UserAgent.ToString();
            Assert.That(Regex.IsMatch(userAgentString, @"^.*Library\/[^\ ]+_[^\ ]+.*"), Is.True);
            Assert.That(Regex.IsMatch(userAgentString, @"^.*OS\/[^\ ]+_[^\ ]+.*"), Is.True);
            Assert.That(Regex.IsMatch(userAgentString, @"^.*Language\/[^\ ]+_[^\ ]+.*"), Is.True);
            Assert.That(Regex.IsMatch(userAgentString, @"^.*Webserver\/[^\ ]+_[^\ ]+.*"), Is.True);
            Assert.That(Regex.IsMatch(userAgentString, @"^.*Module\/Magento_5.0 \(LanguagePack\/7 ; JsLib\/2.0\).*"), Is.True);
        }
コード例 #5
0
        public void ApplyUrlInResource()
        {
            var connector = new BasicConnector(HttpTransportMock.Object, Digest, Secret);

            ResourceMock.SetupProperty(r => r.Location, Url);

            var request = (HttpWebRequest)WebRequest.Create(Url);
            HttpTransportMock.Setup(t => t.CreateRequest(Url)).Returns(request);
            ResponseMock.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request, PayLoad)).Returns(ResponseMock.Object);

            connector.Apply(HttpMethod.Get, ResourceMock.Object, null);

            HttpTransportMock.Verify(t => t.CreateRequest(Url), Times.Once());
        }
コード例 #6
0
        public void ApplyUrlInOptions()
        {
            var connector = new BasicConnector(HttpTransportMock.Object, Digest, Secret);

            var request = (HttpWebRequest)WebRequest.Create(Url);
            HttpTransportMock.Setup(t => t.CreateRequest(Url)).Returns(request);
            ResponseMock.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request, PayLoad)).Returns(ResponseMock.Object);

            connector.Apply(HttpMethod.Get, ResourceMock.Object,
                new Dictionary<string, object> { { "url", Url } });

            HttpTransportMock.Verify(t => t.CreateRequest(Url), Times.Once());
        }
コード例 #7
0
        public void ApplyPostError(HttpStatusCode statusCode, int expectedCode)
        {
            var connector = new BasicConnector(HttpTransportMock.Object, Digest, Secret);

            ResourceMock.SetupProperty(r => r.Location, Url);
            ResourceMock.SetupGet(r => r.ContentType).Returns(ContentType);
            var resourceData = new Dictionary<string, object>() { { "Year", 2012 } };
            ResourceMock.Setup(r => r.Marshal()).Returns(resourceData);

            var request = (HttpWebRequest)WebRequest.Create(Url);
            HttpTransportMock.Setup(t => t.CreateRequest(Url)).Returns(request);
            ResponseMock.SetupGet(r => r.StatusCode).Returns(statusCode);
            PayLoad = "{\"Year\":2012}";
            HttpTransportMock.Setup(t => t.Send(request, PayLoad)).Returns(ResponseMock.Object);

            var ex = Assert.Throws<ConnectorException>(
                () => connector.Apply(HttpMethod.Post, ResourceMock.Object, null));

            var code = (HttpStatusCode)ex.Data["HttpStatusCode"];
            Assert.That(code, Is.Not.Null);
            Assert.That((int)code, Is.EqualTo(expectedCode));

            HttpTransportMock.Verify(t => t.CreateRequest(Url), Times.Once());
            HttpTransportMock.Verify(t => t.Send(request, PayLoad), Times.Once());
        }
コード例 #8
0
        public void ApplyPost303And503()
        {
            var connector = new BasicConnector(HttpTransportMock.Object, Digest, Secret);
            var newLocation = new Uri("http://NewLocation.com");

            ResourceMock.SetupProperty(r => r.Location, Url);
            ResourceMock.SetupGet(r => r.ContentType).Returns(ContentType);
            var resourceData = new Dictionary<string, object>() { { "Year", 2012 } };
            ResourceMock.Setup(r => r.Marshal()).Returns(resourceData);

            // First request and response
            var request1 = (HttpWebRequest)WebRequest.Create(Url);
            HttpTransportMock.Setup(t => t.CreateRequest(Url)).Returns(request1);
            var responseMock1 = new Mock<IHttpResponse>();
            responseMock1.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.SeeOther);
            responseMock1.Setup(r => r.Header("Location")).Returns(newLocation.OriginalString);
            PayLoad = "{\"Year\":2012}";
            responseMock1.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request1, PayLoad)).Returns(responseMock1.Object);

            // Second request and response
            var request2 = (HttpWebRequest)WebRequest.Create(newLocation);
            request2.Method = "GET";
            HttpTransportMock.Setup(t => t.CreateRequest(newLocation)).Returns(request2);
            var responseMock2 = new Mock<IHttpResponse>();
            responseMock2.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.ServiceUnavailable);
            responseMock2.Setup(r => r.Header("Location")).Returns(Url.OriginalString);
            responseMock2.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request2, string.Empty)).Returns(responseMock2.Object);

            var ex = Assert.Throws<ConnectorException>(
                () => connector.Apply(HttpMethod.Post, ResourceMock.Object, null));

            var code = (HttpStatusCode)ex.Data["HttpStatusCode"];
            Assert.That(code, Is.Not.Null);
            Assert.That((int)code, Is.EqualTo(503));

            HttpTransportMock.Verify(t => t.CreateRequest(Url), Times.Once());
            HttpTransportMock.Verify(t => t.Send(request1, PayLoad), Times.Once());
            HttpTransportMock.Verify(t => t.CreateRequest(newLocation), Times.Once());
            HttpTransportMock.Verify(t => t.Send(request2, string.Empty), Times.Once());

            Assert.That(request1.Method, Is.EqualTo(HttpMethod.Post.ToString().ToUpper()));
            Assert.That(request1.UserAgent, Is.EqualTo(connector.UserAgent.ToString()));
            var authorization = string.Format("Klarna {0}", Digest.Create(string.Concat(PayLoad, Secret)));
            Assert.That(request1.Headers["Authorization"], Is.EqualTo(authorization));
            Assert.That(request1.Accept, Is.EqualTo(ContentType));
            Assert.That(request1.ContentType, Is.EqualTo(ContentType));

            Assert.That(request2.Method, Is.EqualTo(HttpMethod.Get.ToString().ToUpper()));
            Assert.That(request2.UserAgent, Is.EqualTo(connector.UserAgent.ToString()));
            var authorization2 = string.Format("Klarna {0}", Digest.Create(string.Concat(string.Empty, Secret)));
            Assert.That(request2.Headers["Authorization"], Is.EqualTo(authorization2));
            Assert.That(request2.Accept, Is.EqualTo(ContentType));
            Assert.That(request2.ContentType, Is.Null);

            Assert.That(ResourceMock.Object.Location.OriginalString, Is.EqualTo(Url.OriginalString));
        }
コード例 #9
0
        public void ApplyPost301EnsureNotRedirected()
        {
            var connector = new BasicConnector(HttpTransportMock.Object, Digest, Secret);
            var newLocation = new Uri("http://NewLocation.com");

            ResourceMock.SetupProperty(r => r.Location, Url);
            ResourceMock.SetupGet(r => r.ContentType).Returns(ContentType);
            var resourceData = new Dictionary<string, object>() { { "Year", 2012 } };
            ResourceMock.Setup(r => r.Marshal()).Returns(resourceData);

            // First request and response
            var request1 = (HttpWebRequest)WebRequest.Create(Url);
            HttpTransportMock.Setup(t => t.CreateRequest(Url)).Returns(request1);
            var responseMock1 = new Mock<IHttpResponse>();
            responseMock1.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.MovedPermanently);
            responseMock1.Setup(r => r.Header("Location")).Returns(newLocation.OriginalString);
            PayLoad = "{\"Year\":2012}";
            responseMock1.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request1, PayLoad)).Returns(responseMock1.Object);

            // Second request and response
            var request2 = (HttpWebRequest)WebRequest.Create(newLocation);
            HttpTransportMock.Setup(t => t.CreateRequest(newLocation)).Returns(request2);
            var responseMock2 = new Mock<IHttpResponse>();
            responseMock2.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.ServiceUnavailable);
            responseMock2.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request2, PayLoad)).Returns(responseMock2.Object);

            connector.Apply(HttpMethod.Post, ResourceMock.Object, null);

            HttpTransportMock.Verify(t => t.CreateRequest(Url), Times.Once());
            HttpTransportMock.Verify(t => t.Send(request1, PayLoad), Times.Once());
            HttpTransportMock.Verify(t => t.CreateRequest(newLocation), Times.Never());
            HttpTransportMock.Verify(t => t.Send(request2, PayLoad), Times.Never());

            Assert.That(request1.Method, Is.EqualTo(HttpMethod.Post.ToString().ToUpper()));
            Assert.That(request1.UserAgent, Is.EqualTo(connector.UserAgent.ToString()));
            var authorization = string.Format("Klarna {0}", Digest.Create(string.Concat(PayLoad, Secret)));
            Assert.That(request1.Headers["Authorization"], Is.EqualTo(authorization));
            Assert.That(request1.Accept, Is.EqualTo(ContentType));
            Assert.That(request1.ContentType, Is.EqualTo(ContentType));

            Assert.That(ResourceMock.Object.Location.OriginalString, Is.EqualTo(newLocation.OriginalString));
        }
コード例 #10
0
        public void ApplyGet301And200()
        {
            var connector = new BasicConnector(HttpTransportMock.Object, Digest, Secret);
            var newLocation = new Uri("http://NewLocation.com");

            ResourceMock.SetupProperty(r => r.Location, Url);
            ResourceMock.SetupGet(r => r.ContentType).Returns(ContentType);

            // First request and response
            var request1 = (HttpWebRequest)WebRequest.Create(Url);
            HttpTransportMock.Setup(t => t.CreateRequest(Url)).Returns(request1);
            var responseMock1 = new Mock<IHttpResponse>();
            responseMock1.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.MovedPermanently);
            responseMock1.Setup(r => r.Header("Location")).Returns(newLocation.OriginalString);
            responseMock1.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request1, PayLoad)).Returns(responseMock1.Object);

            // Second request and response
            var request2 = (HttpWebRequest)WebRequest.Create(newLocation);
            HttpTransportMock.Setup(t => t.CreateRequest(newLocation)).Returns(request2);
            var responseMock2 = new Mock<IHttpResponse>();
            responseMock2.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK);
            responseMock2.Setup(r => r.Header("Location")).Returns(newLocation.OriginalString);
            responseMock2.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request2, PayLoad)).Returns(responseMock2.Object);

            connector.Apply(HttpMethod.Get, ResourceMock.Object, null);

            HttpTransportMock.Verify(t => t.CreateRequest(Url), Times.Once());
            HttpTransportMock.Verify(t => t.Send(request1, PayLoad), Times.Once());
            HttpTransportMock.Verify(t => t.CreateRequest(newLocation), Times.Once());
            HttpTransportMock.Verify(t => t.Send(request2, PayLoad), Times.Once());

            Assert.That(request1.Method, Is.EqualTo(HttpMethod.Get.ToString().ToUpper()));
            Assert.That(request1.UserAgent, Is.EqualTo(connector.UserAgent.ToString()));
            var authorization = string.Format("Klarna {0}", Digest.Create(string.Concat(PayLoad, Secret)));
            Assert.That(request1.Headers["Authorization"], Is.EqualTo(authorization));
            Assert.That(request1.Accept, Is.EqualTo(ContentType));
            Assert.That(request1.ContentType, Is.Null);

            Assert.That(request2.Method, Is.EqualTo(HttpMethod.Get.ToString().ToUpper()));
            Assert.That(request2.UserAgent, Is.EqualTo(connector.UserAgent.ToString()));
            Assert.That(request2.Headers["Authorization"], Is.EqualTo(authorization));
            Assert.That(request2.Accept, Is.EqualTo(ContentType));
            Assert.That(request2.ContentType, Is.Null);

            Assert.That(ResourceMock.Object.Location.OriginalString, Is.EqualTo(newLocation.OriginalString));
        }
コード例 #11
0
        public void ApplyGet301InfiniteLoop()
        {
            var connector = new BasicConnector(HttpTransportMock.Object, Digest, Secret);

            ResourceMock.SetupProperty(r => r.Location, Url);
            ResourceMock.SetupGet(r => r.ContentType).Returns(ContentType);

            // First request and response
            var request = (HttpWebRequest)WebRequest.Create(Url);
            HttpTransportMock.Setup(t => t.CreateRequest(Url)).Returns(request);

            ResponseMock.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.MovedPermanently);
            ResponseMock.Setup(r => r.Header("Location")).Returns(Url.OriginalString);
            ResponseMock.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request, PayLoad)).Returns(ResponseMock.Object);

            var ex = Assert.Throws<ConnectorException>(() => connector.Apply(HttpMethod.Get, ResourceMock.Object, null));
            Assert.That(ex.Message, Is.EqualTo("Infinite redirect loop detected."));

            HttpTransportMock.Verify(t => t.CreateRequest(Url), Times.Exactly(2));
            HttpTransportMock.Verify(t => t.Send(request, PayLoad), Times.Exactly(2));
        }