Example #1
0
        public FileResponse Upload(FileUploadRequest request)
        {
            var requestParams = this.request.BuildRequestParams(request);
            var response = this.request.ExecuteAndDeserializeRequest<FileResponse>("/files.upload", requestParams, file: request);

            return response;
        }
Example #2
0
        public void FileUploadShouldCallCorrectEndpoint()
        {
            var requestHandlerMock = ExecRequestMock<FileResponse>("/files.upload");

            var request = new FileUploadRequest
            {
                Filename = "hello.txt",
                FileData = Encoding.ASCII.GetBytes("hello world")
            };

            requestHandlerMock.Setup(r => r.BuildRequestParams(request))
                              .Returns<Dictionary<string, string>>(null)
                              .Verifiable();

            var subject = new FilesApi(requestHandlerMock.Object);
            var result = subject.Upload(request);

            requestHandlerMock.Verify();
            Assert.NotNull(result);
        }
        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();
        }