Merge branch 'master' of https://github.com/mono/mono into issue4328
[mono.git] / mcs / class / System.Net.Http / Test / System.Net.Http / StreamContentTest.cs
1 //
2 // StreamContentTest.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 System.Collections;
31 using System.Collections.Generic;
32 using NUnit.Framework;
33 using System.Net.Http;
34 using System.Net.Http.Headers;
35 using System.IO;
36 using System.Threading.Tasks;
37 using System.Net;
38 using System.Linq;
39
40 namespace MonoTests.System.Net.Http
41 {
42         [TestFixture]
43         public class StreamContentTest
44         {
45                 class StreamContentMock : StreamContent
46                 {
47                         public Func<long> OnTryComputeLength;
48                         public Action OnSerializeToStreamAsync;
49
50                         public StreamContentMock (Stream stream)
51                                 : base (stream)
52                         {
53                         }
54
55                         protected override bool TryComputeLength (out long length)
56                         {
57                                 if (OnTryComputeLength != null) {
58                                         length = OnTryComputeLength ();
59                                         return true;
60                                 }
61
62                                 return base.TryComputeLength (out length);
63                         }
64
65                         protected override Task SerializeToStreamAsync (Stream stream, TransportContext context)
66                         {
67                                 if (OnSerializeToStreamAsync != null)
68                                         OnSerializeToStreamAsync ();
69
70                                 return base.SerializeToStreamAsync (stream, context);
71                         }
72                 }
73
74                 class ExceptionStream : MemoryStream
75                 {
76                         public ExceptionStream ()
77                         {
78                                 base.WriteByte (10);
79                                 base.Seek (0, SeekOrigin.Begin);
80                         }
81
82                         public override int Read (byte[] buffer, int offset, int count)
83                         {
84                                 throw new ApplicationException ("Read");
85                         }
86
87                         public override byte[] GetBuffer ()
88                         {
89                                 throw new ApplicationException ("GetBuffer");
90                         }
91                 }
92
93                 [Test]
94                 public void Ctor_Invalid ()
95                 {
96                         try {
97                                 new StreamContent (null);
98                                 Assert.Fail ("#1");
99                         } catch (ArgumentNullException) {
100                         }
101
102                         try {
103                                 new StreamContent (new MemoryStream (), 0);
104                                 Assert.Fail ("#2");
105                         } catch (ArgumentOutOfRangeException) {
106                         }
107                 }
108
109                 [Test]
110                 public void Ctor ()
111                 {
112                         var ms = new MemoryStream ();
113                         ms.WriteByte (44);
114
115                         using (var m = new StreamContent (ms)) {
116                         }
117                 }
118
119                 [Test]
120                 public void CopyToAsync_Invalid ()
121                 {
122                         var m = new MemoryStream ();
123
124                         var sc = new StreamContent (new MemoryStream ());
125                         try {
126                                 sc.CopyToAsync (null);
127                                 Assert.Fail ("#1");
128                         } catch (ArgumentNullException) {
129                         }
130
131                         //
132                         // For some reason does not work on .net
133                         //
134                         /*
135                         sc = new StreamContent (new ExceptionStream ());
136                         try {
137                             sc.CopyToAsync (m).Wait ();
138                             Assert.Fail ("#2");
139                         } catch (AggregateException) {
140                         }
141                         */ 
142                 }
143
144                 [Test]
145                 /*
146                  * The .NET runtime hits the "#9" assertion.
147                  * The test succeeds with Mono.
148                  */
149                 [Category ("NotWorking")]
150                 public void CopyToAsync ()
151                 {
152                         var ms = new MemoryStream ();
153                         ms.WriteByte (4);
154                         ms.WriteByte (2);
155                         ms.Seek (0, SeekOrigin.Begin);
156
157                         var sc = new StreamContent (ms);
158
159                         var dest = new MemoryStream ();
160                         var task = sc.CopyToAsync (dest);
161                         task.Wait ();
162                         Assert.AreEqual (2, dest.Length, "#1");
163
164                         bool hit = false;
165                         dest = new MemoryStream ();
166                         var scm = new StreamContentMock (new ExceptionStream ());
167                         scm.OnSerializeToStreamAsync = () => { hit = true; };
168                         task = scm.CopyToAsync (dest);
169                         try {
170                                 task.Wait ();
171                                 Assert.Fail ("#9");
172                         } catch (AggregateException) {
173                         }
174
175                         Assert.IsTrue (hit, "#10");
176                 }
177
178                 [Test]
179                 public void Headers ()
180                 {
181                         var ms = new MemoryStream ();
182                         ms.WriteByte (4);
183                         ms.WriteByte (2);
184                         ms.Seek (0, SeekOrigin.Begin);
185
186                         var sc = new StreamContent (ms);
187                         var headers = sc.Headers;
188                         Assert.AreEqual (2, headers.ContentLength, "#1");
189
190                         headers.ContentLength = 400;
191                         Assert.AreEqual (400, headers.ContentLength, "#1a");
192                         headers.ContentLength = null;
193
194                         var scm = new StreamContentMock (MemoryStream.Null);
195                         scm.OnTryComputeLength = () => 330;
196                         Assert.AreEqual (330, scm.Headers.ContentLength, "#2");
197
198                         headers.Allow.Add ("a1");
199                         headers.ContentEncoding.Add ("ce1");
200                         headers.ContentLanguage.Add ("cl1");
201                         headers.ContentLength = 23;
202                         headers.ContentLocation = new Uri ("http://xamarin.com");
203                         headers.ContentMD5 = new byte[] { 3, 5 };
204                         headers.ContentRange = new ContentRangeHeaderValue (88, 444);
205                         headers.ContentType = new MediaTypeHeaderValue ("multipart/*");
206                         headers.Expires = new DateTimeOffset (DateTime.Today);
207                         headers.LastModified = new DateTimeOffset (DateTime.Today);
208
209
210                         headers.Add ("allow", "a2");
211                         headers.Add ("content-encoding", "ce3");
212                         headers.Add ("content-language", "cl2");
213
214                         try {
215                                 headers.Add ("content-length", "444");
216                                 Assert.Fail ("content-length");
217                         } catch (FormatException) {
218                         }
219
220                         try {
221                                 headers.Add ("content-location", "cl2");
222                                 Assert.Fail ("content-location");
223                         } catch (FormatException) {
224                         }
225
226                         try {
227                                 headers.Add ("content-MD5", "cmd5");
228                                 Assert.Fail ("content-MD5");
229                         } catch (FormatException) {
230                         }
231
232                         try {
233                                 headers.Add ("content-range", "133");
234                                 Assert.Fail ("content-range");
235                         } catch (FormatException) {
236                         }
237
238                         try {
239                                 headers.Add ("content-type", "ctype");
240                                 Assert.Fail ("content-type");
241                         } catch (FormatException) {
242                         }
243
244                         try {
245                                 headers.Add ("expires", "ctype");
246                                 Assert.Fail ("expires");
247                         } catch (FormatException) {
248                         }
249
250                         try {
251                                 headers.Add ("last-modified", "lmo");
252                                 Assert.Fail ("last-modified");
253                         } catch (FormatException) {
254                         }
255
256                         Assert.IsTrue (headers.Allow.SequenceEqual (
257                                 new[] {
258                                         "a1",
259                                         "a2"
260                                 }
261                         ));
262
263                         Assert.IsTrue (headers.ContentEncoding.SequenceEqual (
264                                 new[] {
265                                         "ce1",
266                                         "ce3"
267                                 }
268                         ));
269
270                         Assert.IsTrue (headers.ContentLanguage.SequenceEqual (
271                                 new[] {
272                                         "cl1",
273                                         "cl2"
274                                 }
275                         ));
276
277                         Assert.AreEqual (23, headers.ContentLength);
278                         Assert.AreEqual (new Uri ("http://xamarin.com"), headers.ContentLocation);
279                         Assert.AreEqual (new byte[] { 3, 5 }, headers.ContentMD5);
280                         Assert.AreEqual (new ContentRangeHeaderValue (88, 444), headers.ContentRange);
281                         Assert.AreEqual (new MediaTypeHeaderValue ("multipart/*"), headers.ContentType);
282                         Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.Expires);
283                         Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.LastModified);
284                 }
285
286                 [Test]
287                 public void Headers_Invalid ()
288                 {
289                         var sc = new StreamContent (MemoryStream.Null);
290                         var h = sc.Headers;
291
292                         try {
293                                 h.Add ("Age", "");
294                                 Assert.Fail ("#1");
295                         } catch (InvalidOperationException) {
296                         }
297                 }
298
299                 [Test]
300                 public void LoadIntoBuffer ()
301                 {
302                         var ms = new MemoryStream ();
303                         ms.WriteByte (4);
304                         ms.Seek (0, SeekOrigin.Begin);
305
306                         var sc = new StreamContent (ms);
307                         Assert.IsTrue (sc.LoadIntoBufferAsync (400).Wait (200));
308                 }
309
310                 [Test]
311                 public void LoadIntoBuffer_BufferOverflow ()
312                 {
313                         var ms = new MemoryStream ();
314                         ms.Write (new byte[10000], 0, 10000);
315                         ms.Seek (0, SeekOrigin.Begin);
316
317                         var sc = new StreamContent (ms);
318                         try {
319                                 Assert.IsTrue (sc.LoadIntoBufferAsync (50).Wait (200));
320                                 Assert.Fail ("#1");
321                         } catch (AggregateException e) {
322                                 Assert.IsInstanceOfType (typeof (HttpRequestException), e.InnerException, "#2");
323                         }
324                 }
325
326                 [Test]
327                 public void ReadAsByteArrayAsync ()
328                 {
329                         var ms = new MemoryStream ();
330                         ms.WriteByte (4);
331                         ms.WriteByte (55);
332
333                         var sc = new StreamContent (ms);
334                         var res = sc.ReadAsByteArrayAsync ().Result;
335                         Assert.AreEqual (0, res.Length, "#1");
336
337                         ms.Seek (0, SeekOrigin.Begin);
338                         sc = new StreamContent (ms);
339                         res = sc.ReadAsByteArrayAsync ().Result;
340                         Assert.AreEqual (2, res.Length, "#10");
341                         Assert.AreEqual (55, res[1], "#11");
342                 }
343
344                 [Test]
345                 public void ReadAsString ()
346                 {
347                         var ms = new MemoryStream ();
348                         ms.WriteByte (77);
349                         ms.WriteByte (55);
350                         ms.Seek (0, SeekOrigin.Begin);
351
352                         var sc = new StreamContent (ms);
353                         var res = sc.ReadAsStringAsync ().Result;
354                         Assert.AreEqual ("M7", res, "#1");
355                 }
356
357                 [Test]
358                 public void ReadAsStream ()
359                 {
360                         var ms = new MemoryStream ();
361                         ms.WriteByte (77);
362                         ms.WriteByte (55);
363                         ms.Seek (0, SeekOrigin.Begin);
364
365                         var sc = new StreamContent (ms);
366                         var res = sc.ReadAsStreamAsync ().Result;
367                         Assert.AreEqual (77, res.ReadByte (), "#1");
368                 }
369         }
370 }