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