Пример #1
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));
        }
Пример #2
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));
        }