public void ShouldExtractMultiPartFormData()
        {
            using (FakeHttpServer server = new FakeHttpServer())
            {
                server.Host = "localhost";
                server.Port = 8080;
                server.Path = "api/v1/accounts";
                server.StatusCode = HttpStatusCode.OK;
                server.StatusDescription = "Success";

                MultiPartBodyExtractor extractor = new MultiPartBodyExtractor();
                server.UseBodyExtractor(extractor);

                server.Listen();

                WebRequest request = WebRequest.Create("http://localhost:8080/api/v1/accounts");
                request.Method = "POST";
                request.ContentType = "multipart/form-data; boundary=taco";
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write("--taco\r\n");
                    writer.Write("Content-Disposition: form-data; name=name\r\n");
                    writer.Write("\r\n");
                    writer.Write("bob\r\n");
                    writer.Write("--taco\r\n");
                    writer.Write("Content-Disposition: form-data; name=file; filename=help.txt\r\n");
                    writer.Write("Content-Type: text/plain\r\n");
                    writer.Write("\r\n");
                    writer.Write("These are the contents of the file.\r\n");
                    writer.Write("--taco--\r\n");
                }
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    NameValueCollection collection = extractor.Parameters;
                    Assert.AreEqual("bob", collection.Get("name"), "Could not extract the name parameter.");
                    var files = extractor.Files.GetFiles("file");
                    Assert.AreEqual(1, files.Count(), "The wrong number of files were extracted.");
                    var file = files.First();
                    Assert.AreEqual("file", file.Name, "The name of the file part was not stored.");
                    Assert.AreEqual("help.txt", file.FileName, "The file name was not stored.");
                    Assert.AreEqual("text/plain", file.ContentType, "The file content type was not stored.");
                    Assert.AreEqual("These are the contents of the file.", Encoding.Default.GetString(file.Contents), "The file contents were wrong.");
                }
            }
        }
Ejemplo n.º 2
0
        public void ShouldPostMultuPartWithBase64Content()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                MultiPartBodyExtractor extractor = new MultiPartBodyExtractor();
                server.UseBodyExtractor(extractor);

                server.Listen();

                string message = "Hello, World!Ӽ!";
                byte[] messageRaw = Encoding.UTF8.GetBytes(message);
                string base64 = Convert.ToBase64String(messageRaw);
                byte[] base64Raw = Encoding.UTF8.GetBytes(base64);
                NameValueCollection headers = new NameValueCollection();
                headers.Add("Content-Transfer-Encoding", "base64");

                RestClient client = new RestClient("http://localhost:8080");
                client.Post("api/customers")
                    .WithMultiPartBody(b =>
                    {
                        b.WithFile("file", "file.txt", base64Raw, "text/plain", headers);
                    })
                    .Execute();

                var file = extractor.Files.GetFiles("file").Single();
                CollectionAssert.AreEqual(base64Raw, file.Contents);
            }
        }
Ejemplo n.º 3
0
        public void ShouldPostMultuPartWithInvalidCharacter()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                MultiPartBodyExtractor extractor = new MultiPartBodyExtractor();
                server.UseBodyExtractor(extractor);

                server.Listen();

                RestClient client = new RestClient("http://localhost:8080");
                client.Post("api/customers")
                    .WithMultiPartBody(b =>
                    {
                        b.WithFormData(ub => ub.WithParameter("naӼme", "John Smith"));
                    })
                    .Execute();

                Assert.AreEqual("John Smith", extractor.Parameters["na?me"], "The form data was not transfered.");
            }
        }
Ejemplo n.º 4
0
        public async Task ShouldPOSTMultiPartDataAsync()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                MultiPartBodyExtractor extractor = new MultiPartBodyExtractor();
                server.UseBodyExtractor(extractor);

                server.Listen();

                RestClient client = new RestClient("http://localhost:8080");
                await client.Post("api/customers")
                    .WithMultiPartBody(b =>
                    {
                        b.WithFormData(ub => ub.WithParameter("name", "John Smith"));
                        b.WithFile("file1", "path", Encoding.UTF8.GetBytes("Hello, world"), "text/plain");
                    })
                    .ExecuteAsync();

                Assert.AreEqual("John Smith", extractor.Parameters["name"], "The form data was not transfered.");

                var file = extractor.Files.GetFiles("file1").SingleOrDefault();
                Assert.AreEqual("file1", file.Name);
                Assert.AreEqual("path", file.FileName);
                Assert.AreEqual("text/plain", file.ContentType);
                Assert.AreEqual("Hello, world", Encoding.UTF8.GetString(file.Contents));
            }
        }