Пример #1
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());
        }
Пример #2
0
        public void ApplyPost200InvalidJson()
        {
            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.Data).Returns("{{{{");
            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));

            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
        static void Main(string[] args)
        {
            //$logger = New-Logger -FileName "c:\temp\netomity-log.txt"
            var logger = new Logger(@"C:\temp\netomity-log.txt");

            var ns = NetomitySystem.Factory();

//$insteonPort = New-SerialIO -Name "InsteonSerial" -PortName "COM5" -PortSpeed 19200
            var insteonPort = new SerialIO(portName: "COM5", portSpeed: 19200);
//$w800Port = New-SerialIO -Name "W800Serial" -PortName "COM4" -PortSpeed 4800
            var w800Port = new SerialIO(portName: "COM4", portSpeed: 4800);
//$insteonTCP = New-TCPServer -Name "InsteonTCP" -PortNumber 3333
            var insteonTCP = new TCPServer(3333);
//$w800TCP = New-TCPServer -Name "W800TCP" -PortNumber 3334
            var W800TCP = new TCPServer(3334);
//$insteonBC = New-BasicConnector -Name "InsteonBC" -Interfaces $insteonPort, $insteonTCP
            var insteonBC = new BasicConnector(insteonPort, insteonTCP);
//$w800BC = New-BasicConnector -Name "W800BC" -Interfaces $w800Port, $w800TCP
            var w800BC = new BasicConnector(w800Port, W800TCP);
//$insteonTask = $insteonBC.Open()
            var insteonTask = insteonBC.Open();

            //$w800Task = $w800BC.Open()
            var w800Task = w800BC.Open();

//$insteonTask.Wait();
//            insteonTask.Wait();
//            Thread.Sleep(60000);
            ns.Run();
        }
Пример #5
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));
        }
Пример #6
0
        public void ApplyGet200()
        {
            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(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());
            HttpTransportMock.Verify(t => t.Send(request, PayLoad), Times.Once());

            Assert.That(request.Method, Is.EqualTo(HttpMethod.Get.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.Null);
        }
Пример #7
0
        public void ApplyGet303And503()
        {
            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.SeeOther);
            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.ServiceUnavailable);
            responseMock2.SetupGet(r => r.Data).Returns(PayLoad);
            HttpTransportMock.Setup(t => t.Send(request2, PayLoad)).Returns(responseMock2.Object);

            var ex = Assert.Throws <ConnectorException>(
                () => connector.Apply(HttpMethod.Get, 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, 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(Url.OriginalString));
        }
Пример #8
0
        public void ApplyPost302EnsureNotRedirected()
        {
            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.Found);
            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(Url.OriginalString));
        }
Пример #9
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());
        }
Пример #10
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."));
        }
Пример #11
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());
        }
Пример #12
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);
        }
Пример #13
0
        public void Functional1()
        {
            var log  = new Logger(@"C:\temp\log.txt");
            var tcp1 = new TCPServer(2222)
            {
                Name = "Server 1"
            };
            var tcp2 = new TCPServer(2223)
            {
                Name = "Server 2"
            };
            var tcp3 = new TCPClient("127.0.0.1", 2223)
            {
                Name = "Client 2"
            };
            var bc = new BasicConnector(new List <BasicInterface>()
            {
                tcp1, tcp2
            })
            {
                Name = "BC"
            };

            tcp3.DataReceived += ReceivedData;

            var task = bc.Open();

            task.Wait(5000);


            _tcp.Open();
            Thread.Sleep(1000);
            tcp3.Open();
            Thread.Sleep(5000);

            _tcp.Send("asdf");
            Thread.Sleep(1000);

            Assert.AreEqual(_str1, "asdf");

            Thread.Sleep(10000);
        }
Пример #14
0
        private IConnector GetMockConnector()
        {
            var url               = new Uri("http://klarna.com");
            var secret            = "My Secret";
            var digest            = new Digest();
            var httpTransportMock = new Mock <IHttpTransport>();

            var createdresponseMock = new Mock <IHttpResponse>();

            //Base
            PrepareBaseMock(httpTransportMock);
            PreparePostProcessMock(httpTransportMock);


            httpTransportMock.Setup(t => t.CreateRequest(It.IsAny <Uri>())).Returns((HttpWebRequest)WebRequest.Create("http://www.contoso.com/"));

            IConnector connector = new BasicConnector(httpTransportMock.Object, digest, secret, url);

            return(connector);
        }
Пример #15
0
        public void ApplyPost201UpdatedLocation()
        {
            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);
            ResponseMock.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.Created);
            const string UpdatedLocation = "http://NewLocation.com";

            ResponseMock.Setup(r => r.Header("Location")).Returns(UpdatedLocation);

            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));

            Assert.That(ResourceMock.Object.Location.OriginalString, Is.EqualTo(UpdatedLocation));
        }
Пример #16
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());
        }
Пример #17
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));
        }
Пример #18
0
        public void SetUp()
        {
            var logger = new Netomity.Core.Fakes.StubLogger();

            _intA = new Netomity.Interfaces.Basic.Fakes.StubBasicInterface()
            {
                Open01 = () =>
                {
                    _intAOpen = true;
                    return(Task.Factory.StartNew(() => {}));
                },
                IsOpenGet = () => { return(_intAOpen); },
            };
            _intB = new Netomity.Interfaces.Basic.Fakes.StubBasicInterface()
            {
                Open01 = () =>
                {
                    _intBOpen = true;
                    return(Task.Factory.StartNew(() => {}));
                },
                IsOpenGet = () => { return(_intBOpen); },
            };
            _bc = new BasicConnector(_intA, _intB);
        }
Пример #19
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));
        }
Пример #20
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));
        }
Пример #21
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));
        }
Пример #22
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));
        }
Пример #23
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);
        }
Пример #24
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());
        }
Пример #25
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());
        }
Пример #26
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());
        }