2 // MultipartFormDataContentTest.cs
5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using NUnit.Framework;
31 using System.Net.Http;
33 using System.Threading.Tasks;
36 using System.Net.Http.Headers;
38 namespace MonoTests.System.Net.Http
41 public class MultipartFormDataContentTest
44 public void Ctor_Invalid ()
47 new MultipartFormDataContent (null);
49 } catch (ArgumentException) {
53 new MultipartFormDataContent ("[]");
55 } catch (ArgumentException) {
59 new MultipartFormDataContent ("1234567890123456789012345678901234567890123456789012345678901234567890X");
61 } catch (ArgumentException) {
65 new MultipartFormDataContent ("st ");
67 } catch (ArgumentException) {
71 new MultipartFormDataContent ("@");
73 } catch (ArgumentException) {
80 using (var m = new MultipartFormDataContent ("b")) {
81 m.Headers.Add ("extra", "value");
82 Assert.AreEqual ("multipart/form-data", m.Headers.ContentType.MediaType, "#1");
83 Assert.IsNull (m.Headers.ContentDisposition, "#2");
84 Assert.AreEqual (14, m.Headers.ContentLength, "#3");
85 Assert.AreEqual ("--b\r\n\r\n--b--\r\n", m.ReadAsStringAsync ().Result, "#4");
88 using (var m = new MultipartFormDataContent ()) {
89 Assert.AreEqual ("multipart/form-data", m.Headers.ContentType.MediaType, "#11");
90 Assert.AreEqual (84, m.Headers.ContentLength, "#12");
93 using (var m = new MultipartFormDataContent ("ggg")) {
94 Assert.AreEqual ("multipart/form-data", m.Headers.ContentType.MediaType, "#21");
95 Assert.AreEqual (18, m.Headers.ContentLength, "#22");
102 var m = new MultipartFormDataContent ("b");
104 var other = new MultipartFormDataContent ("44");
105 other.Headers.Expires = new DateTimeOffset (2020, 11, 30, 19, 55, 22, TimeSpan.Zero);
108 Assert.AreEqual ("multipart/form-data", m.Headers.ContentType.MediaType, "#1");
109 Assert.AreEqual (154, m.Headers.ContentLength, "#2");
110 Assert.AreEqual ("--b\r\nContent-Type: multipart/form-data; boundary=\"44\"\r\nExpires: Mon, 30 Nov 2020 19:55:22 GMT\r\nContent-Disposition: form-data\r\n\r\n--44\r\n\r\n--44--\r\n\r\n--b--\r\n", m.ReadAsStringAsync ().Result, "#3");
111 Assert.AreEqual (other, m.First (), "#4");
112 Assert.IsNull (m.Headers.ContentDisposition, "#5");
113 Assert.AreEqual ("form-data", other.Headers.ContentDisposition.ToString (), "#6");
119 var m = new MultipartFormDataContent ("b");
121 var other = new MultipartFormDataContent ("44");
122 m.Add (other, "name", "fname");
124 Assert.AreEqual ("multipart/form-data", m.Headers.ContentType.MediaType, "#1");
125 Assert.AreEqual (165, m.Headers.ContentLength, "#2");
126 Assert.AreEqual ("--b\r\nContent-Type: multipart/form-data; boundary=\"44\"\r\nContent-Disposition: form-data; name=name; filename=fname; filename*=utf-8''fname\r\n\r\n--44\r\n\r\n--44--\r\n\r\n--b--\r\n", m.ReadAsStringAsync ().Result, "#3");
127 Assert.AreEqual (other, m.First (), "#4");
128 Assert.IsNull (m.Headers.ContentDisposition, "#5");
129 Assert.AreEqual ("form-data; name=name; filename=fname; filename*=utf-8''fname", other.Headers.ContentDisposition.ToString (), "#6");
135 var m = new MultipartFormDataContent ("b");
137 var other = new MultipartFormDataContent ("44");
138 other.Headers.ContentDisposition = new ContentDispositionHeaderValue ("dt");
139 m.Add (other, "name", "fname");
141 Assert.AreEqual ("multipart/form-data", m.Headers.ContentType.MediaType, "#1");
142 Assert.AreEqual (107, m.Headers.ContentLength, "#2");
143 Assert.AreEqual ("--b\r\nContent-Type: multipart/form-data; boundary=\"44\"\r\nContent-Disposition: dt\r\n\r\n--44\r\n\r\n--44--\r\n\r\n--b--\r\n", m.ReadAsStringAsync ().Result, "#3");
144 Assert.AreEqual (other, m.First (), "#4");
145 Assert.IsNull (m.Headers.ContentDisposition, "#5");
146 Assert.AreEqual ("dt", other.Headers.ContentDisposition.ToString (), "#6");
150 public void Add_Invalid ()
152 var m = new MultipartFormDataContent ("a");
156 } catch (ArgumentNullException) {
160 m.Add (new MultipartFormDataContent ("44"), null);
162 } catch (ArgumentException) {
166 m.Add (new MultipartFormDataContent ("44"), "s", null);
168 } catch (ArgumentException) {
172 m.Add (new MultipartFormDataContent ("44"), "s", " ");
174 } catch (ArgumentException) {