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); }
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()); }
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()); }
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.")); }
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()); }
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()); }
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)); }
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)); }