/// <summary>
        ///     Contructor.
        /// </summary>
        public MultipartFormFormatter()
        {
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/form-data"));

            _uploadPath    = Path.GetTempPath();
            _multipartForm = new HttpMultipartForm();
        }
Пример #2
0
        public void TestHttpMultipartFormHandler()
        {
            // Arrange
            var         stream      = new MemoryStream(Encoding.UTF8.GetBytes(Body));
            HttpContent httpContent = new StreamContent(stream);

            httpContent.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
            httpContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("boundary", "----WebKitFormBoundaryAIv1HQdnxqjt9a5y"));
            httpContent.Headers.ContentLength = 465;

            var requestMock = new Mock <HttpRequestMessage>();

            requestMock.Object.Method  = new HttpMethod("POST");
            requestMock.Object.Content = httpContent;

            // For returning non-async stuff, use a TaskCompletionSource to avoid thread switches
            // Save file
            var provider = new MultipartFormDataStreamProvider(Path.GetTempPath());

            var task = requestMock.Object.Content.ReadAsMultipartAsync(provider);

            task.Wait();

            // Act
            IDictionary <string, string> result = HttpMultipartForm.HandleMultipartBody(provider);

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.ContainsKey("Name"));
            Assert.IsTrue(result.ContainsKey("Data"));
            Assert.IsTrue(result.ContainsKey("Video"));
            Assert.IsTrue(result.ContainsKey("DataUri"));
            Assert.IsTrue(result.ContainsKey("DataContentType"));
            Assert.IsTrue(result.ContainsKey("VideoUri"));
            Assert.IsTrue(result.ContainsKey("VideoContentType"));
            Assert.IsFalse(string.IsNullOrEmpty(result["Name"]));
            Assert.IsFalse(string.IsNullOrEmpty(result["Data"]));
            Assert.IsFalse(string.IsNullOrEmpty(result["Video"]));
            Assert.IsFalse(string.IsNullOrEmpty(result["DataUri"]));
            Assert.IsFalse(string.IsNullOrEmpty(result["DataContentType"]));
            Assert.IsFalse(string.IsNullOrEmpty(result["VideoUri"]));
            Assert.IsFalse(string.IsNullOrEmpty(result["VideoContentType"]));
            Assert.IsTrue(File.Exists(result["DataUri"]));
            Assert.IsTrue(File.Exists(result["VideoUri"]));
            File.Delete(result["DataUri"]);
            File.Delete(result["VideoUri"]);
        }