Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Net.Http / Test / System.Net.Http / MultipartFormDataContentTest.cs
1 //
2 // MultipartFormDataContentTest.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 using System;
30 using NUnit.Framework;
31 using System.Net.Http;
32 using System.IO;
33 using System.Threading.Tasks;
34 using System.Text;
35 using System.Linq;
36 using System.Net.Http.Headers;
37
38 namespace MonoTests.System.Net.Http
39 {
40         [TestFixture]
41         public class MultipartFormDataContentTest
42         {
43                 [Test]
44                 public void Ctor_Invalid ()
45                 {
46                         try {
47                                 new MultipartFormDataContent (null);
48                                 Assert.Fail ("#1");
49                         } catch (ArgumentException) {
50                         }
51
52                         try {
53                                 new MultipartFormDataContent ("[]");
54                                 Assert.Fail ("#2");
55                         } catch (ArgumentException) {
56                         }
57
58                         try {
59                                 new MultipartFormDataContent ("1234567890123456789012345678901234567890123456789012345678901234567890X");
60                                 Assert.Fail ("#3");
61                         } catch (ArgumentException) {
62                         }
63
64                         try {
65                                 new MultipartFormDataContent ("st ");
66                                 Assert.Fail ("#4");
67                         } catch (ArgumentException) {
68                         }
69
70                         try {
71                                 new MultipartFormDataContent ("@");
72                                 Assert.Fail ("#5");
73                         } catch (ArgumentException) {
74                         }
75                 }
76
77                 [Test]
78                 public void Ctor ()
79                 {
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");
86                         }
87
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");
91                         }
92
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");
96                         }
97                 }
98
99                 [Test]
100                 public void Add ()
101                 {
102                         var m = new MultipartFormDataContent ("b");
103
104                         var other = new MultipartFormDataContent ("44");
105                         other.Headers.Expires = new DateTimeOffset (2020, 11, 30, 19, 55, 22, TimeSpan.Zero);
106                         m.Add (other);
107
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");
114                 }
115
116                 [Test]
117                 public void Add_2 ()
118                 {
119                         var m = new MultipartFormDataContent ("b");
120
121                         var other = new MultipartFormDataContent ("44");
122                         m.Add (other, "name", "fname");
123
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");
130                 }
131
132                 [Test]
133                 public void Add_3 ()
134                 {
135                         var m = new MultipartFormDataContent ("b");
136
137                         var other = new MultipartFormDataContent ("44");
138                         other.Headers.ContentDisposition = new ContentDispositionHeaderValue ("dt");
139                         m.Add (other, "name", "fname");
140
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");
147                 }
148
149                 [Test]
150                 public void Add_Invalid ()
151                 {
152                         var m = new MultipartFormDataContent ("a");
153                         try {
154                                 m.Add (null);
155                                 Assert.Fail ("#1");
156                         } catch (ArgumentNullException) {
157                         }
158
159                         try {
160                                 m.Add (new MultipartFormDataContent ("44"), null);
161                                 Assert.Fail ("#2");
162                         } catch (ArgumentException) {
163                         }
164
165                         try {
166                                 m.Add (new MultipartFormDataContent ("44"), "s", null);
167                                 Assert.Fail ("#3");
168                         } catch (ArgumentException) {
169                         }
170
171                         try {
172                                 m.Add (new MultipartFormDataContent ("44"), "s", "   ");
173                                 Assert.Fail ("#4");
174                         } catch (ArgumentException) {
175                         }
176                 }
177         }
178 }