Beispiel #1
0
        public void BasicUpload()
        {
            using (var server = new HttpServer())
            {
                server.RequestReceived += (s, e) =>
                {
                    Assert.That(e.Request.Form.AllKeys, Is.EquivalentTo(new[] { "key" }));
                    Assert.AreEqual("value", e.Request.Form["key"]);
                    Assert.AreEqual("multipart/form-data", e.Request.ContentType);
                    Assert.AreEqual("POST", e.Request.HttpMethod);
                    Assert.IsNull(e.Request.InputStream);
                };

                server.Start();

                var request = (HttpWebRequest)WebRequest.Create(
                    String.Format("http://{0}/", server.EndPoint)
                );

                using (var writer = new MultiPartWriter(request))
                {
                    writer.WriteValue("key", "value");
                }

                GetResponseFromRequest(request);
            }
        }
Beispiel #2
0
        public void BasicFileUpload()
        {
            using (var server = new HttpServer())
            using (var uploadStream = new MemoryStream())
            {
                WriteRandomData(uploadStream, UploadSize);

                server.RequestReceived += (s, e) =>
                {
                    uploadStream.Position = 0;

                    Assert.That(e.Request.Files.AllKeys, Is.EquivalentTo(new[] { "file" }));
                    Assert.AreEqual(uploadStream, e.Request.Files["file"].InputStream);
                    Assert.AreEqual("filename", e.Request.Files["file"].FileName);
                    Assert.AreEqual("application/octet-stream", e.Request.Files["file"].ContentType);
                    Assert.AreEqual(UploadSize, e.Request.Files["file"].ContentLength);
                    Assert.AreEqual("multipart/form-data", e.Request.ContentType);
                    Assert.AreEqual("POST", e.Request.HttpMethod);
                    Assert.IsNull(e.Request.InputStream);
                };

                server.Start();

                var request = (HttpWebRequest)WebRequest.Create(
                    String.Format("http://{0}/", server.EndPoint)
                );

                using (var writer = new MultiPartWriter(request))
                {
                    uploadStream.Position = 0;

                    writer.WriteFile("file", "filename", "application/octet-stream", uploadStream);
                }

                GetResponseFromRequest(request);
            }
        }