public void ExecuteAndDeserializeRequestHandlesComplexRequest() { var token = "xxx"; var path = "/root"; var jsonPayload = "{\"json\": \"result\""; var dictionaryParams = new Dictionary <string, string> { { "foo", "bar" } }; var fileRequest = new FileUploadRequest { Title = "file", Filename = "file.txt", FileData = new byte[10] }; var restResponse = new RestResponse(); restResponse.Content = jsonPayload; var mockRestClient = new Mock <IRestClient>(); mockRestClient.Setup(r => r.Execute(It.IsAny <IRestRequest>())) .Callback <IRestRequest>(r => { Assert.Equal(1, r.Files.Count); Assert.Equal(fileRequest.Title, r.Files[0].Name); Assert.Equal(fileRequest.Filename, r.Files[0].FileName); Assert.Equal(fileRequest.FileData.Length, r.Files[0].ContentLength); Assert.Equal(Method.PUT, r.Method); Assert.Equal(path, r.Resource); Assert.Equal(2, r.Parameters.Count); Assert.Equal(token, r.Parameters.First(p => p.Name == "token").Value); Assert.Equal("bar", r.Parameters.First(p => p.Name == "foo").Value); }) .Returns(restResponse) .Verifiable(); var mockParser = new Mock <IResponseParser>(); mockParser.Setup(p => p.Deserialize <TestParams>(jsonPayload)) .Returns(new TestParams { MyInt = 90 }) .Verifiable(); var subject = new RestSharpRequestHandler(token); subject.RestClient = mockRestClient.Object; subject.ResponseParser = mockParser.Object; var result = subject.ExecuteAndDeserializeRequest <TestParams>(path, dictionaryParams, HttpMethod.PUT, fileRequest); Assert.Equal(90, result.MyInt); mockParser.Verify(); mockRestClient.Verify(); }
public void ExecuteAndDeserializeRequestHandlesBasicRequest() { var token = "xxx"; var path = "/root"; var jsonPayload = "{\"json\": \"result\""; var restResponse = new RestResponse(); restResponse.Content = jsonPayload; var mockRestClient = new Mock <IRestClient>(); mockRestClient.Setup(r => r.Execute(It.IsAny <IRestRequest>())) .Callback <IRestRequest>(r => { Assert.Equal(0, r.Files.Count); Assert.Equal(Method.POST, r.Method); Assert.Equal(path, r.Resource); Assert.Equal(1, r.Parameters.Count); Assert.Equal(token, r.Parameters[0].Value); }) .Returns(restResponse) .Verifiable(); var mockParser = new Mock <IResponseParser>(); mockParser.Setup(p => p.Deserialize <TestParams>(jsonPayload)) .Returns(new TestParams { MyInt = 90 }) .Verifiable(); var subject = new RestSharpRequestHandler(token); subject.RestClient = mockRestClient.Object; subject.ResponseParser = mockParser.Object; var result = subject.ExecuteAndDeserializeRequest <TestParams>(path); Assert.Equal(90, result.MyInt); mockParser.Verify(); mockRestClient.Verify(); }
public void ExecuteAndDeserializeRequestHandlesComplexRequest() { var token = "xxx"; var path = "/root"; var jsonPayload = "{\"json\": \"result\""; var dictionaryParams = new Dictionary<string, string> { { "foo", "bar" } }; var fileRequest = new FileUploadRequest { Title = "file", Filename = "file.txt", FileData = new byte[10] }; var restResponse = new RestResponse(); restResponse.Content = jsonPayload; var mockRestClient = new Mock<IRestClient>(); mockRestClient.Setup(r => r.Execute(It.IsAny<IRestRequest>())) .Callback<IRestRequest>(r => { Assert.Equal(1, r.Files.Count); Assert.Equal(fileRequest.Title, r.Files[0].Name); Assert.Equal(fileRequest.Filename, r.Files[0].FileName); Assert.Equal(fileRequest.FileData.Length, r.Files[0].ContentLength); Assert.Equal(Method.PUT, r.Method); Assert.Equal(path, r.Resource); Assert.Equal(2, r.Parameters.Count); Assert.Equal(token, r.Parameters.First(p => p.Name == "token").Value); Assert.Equal("bar", r.Parameters.First(p => p.Name == "foo").Value); }) .Returns(restResponse) .Verifiable(); var mockParser = new Mock<IResponseParser>(); mockParser.Setup(p => p.Deserialize<TestParams>(jsonPayload)) .Returns(new TestParams { MyInt = 90 }) .Verifiable(); var subject = new RestSharpRequestHandler(token); subject.RestClient = mockRestClient.Object; subject.ResponseParser = mockParser.Object; var result = subject.ExecuteAndDeserializeRequest<TestParams>(path, dictionaryParams, HttpMethod.PUT, fileRequest); Assert.Equal(90, result.MyInt); mockParser.Verify(); mockRestClient.Verify(); }
public void ExecuteAndDeserializeRequestHandlesBasicRequest() { var token = "xxx"; var path = "/root"; var jsonPayload = "{\"json\": \"result\""; var restResponse = new RestResponse(); restResponse.Content = jsonPayload; var mockRestClient = new Mock<IRestClient>(); mockRestClient.Setup(r => r.Execute(It.IsAny<IRestRequest>())) .Callback<IRestRequest>(r => { Assert.Equal(0, r.Files.Count); Assert.Equal(Method.POST, r.Method); Assert.Equal(path, r.Resource); Assert.Equal(1, r.Parameters.Count); Assert.Equal(token, r.Parameters[0].Value); }) .Returns(restResponse) .Verifiable(); var mockParser = new Mock<IResponseParser>(); mockParser.Setup(p => p.Deserialize<TestParams>(jsonPayload)) .Returns(new TestParams { MyInt = 90 }) .Verifiable(); var subject = new RestSharpRequestHandler(token); subject.RestClient = mockRestClient.Object; subject.ResponseParser = mockParser.Object; var result = subject.ExecuteAndDeserializeRequest<TestParams>(path); Assert.Equal(90, result.MyInt); mockParser.Verify(); mockRestClient.Verify(); }