public MultipartFormDataObject ToMultipartFormDataObject()
        {
            MultipartFormDataObject result = new MultipartFormDataObject();

            if (File != null)
            {
                result.AddFile("file", File);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public async Task TestMultipartFormDataUploadPostMultipartFormDataObjectWithBodyHandler()
        {
            CommunicatorConfiguration configuration = GetCommunicatorConfiguration();
            // changing the ApiEndpoint changes the underlying configuration section; restore it afterwards
            var apiEndpoint = configuration.ApiEndpoint;

            try
            {
                configuration.ApiEndpoint = new Uri("http://httpbin.org");

                using (Communicator communicator = Factory.CreateCommunicator(configuration))
                {
                    MemoryStream content = new MemoryStream();
                    StreamWriter writer  = new StreamWriter(content);
                    writer.Write("file-content");
                    writer.Flush();
                    content.Position = 0;
                    MultipartFormDataObject multipart = new MultipartFormDataObject();
                    multipart.AddFile("file", new UploadableFile("file.txt", content, "text/plain"));
                    multipart.AddValue("value", "Hello World");

                    await communicator.Post("/post", null, null, multipart, (stream, headers) => {
                        HttpBinResponse response = DefaultMarshaller.Instance.Unmarshal <HttpBinResponse>(stream);

                        Assert.NotNull(response.Form);
                        Assert.AreEqual(1, response.Form.Count);
                        Assert.IsTrue(response.Form.ContainsKey("value"));
                        Assert.AreEqual("Hello World", response.Form["value"]);

                        Assert.NotNull(response.Files);
                        Assert.AreEqual(1, response.Files.Count);
                        Assert.IsTrue(response.Files.ContainsKey("file"));
                        Assert.AreEqual("file-content", response.Files["file"]);
                    }, null);
                }
            }
            finally
            {
                configuration.ApiEndpoint = apiEndpoint;
            }
        }