[jit] Rewrite some logging to work on embedded systems.
[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 using System.Text;
40
41 namespace MonoTests.System.Net.Http
42 {
43         [TestFixture]
44         public class StreamContentTest
45         {
46                 class StreamContentMock : StreamContent
47                 {
48                         public Func<long> OnTryComputeLength;
49                         public Action OnSerializeToStreamAsync;
50
51                         public StreamContentMock (Stream stream)
52                                 : base (stream)
53                         {
54                         }
55
56                         protected override bool TryComputeLength (out long length)
57                         {
58                                 if (OnTryComputeLength != null) {
59                                         length = OnTryComputeLength ();
60                                         return true;
61                                 }
62
63                                 return base.TryComputeLength (out length);
64                         }
65
66                         protected override Task SerializeToStreamAsync (Stream stream, TransportContext context)
67                         {
68                                 if (OnSerializeToStreamAsync != null)
69                                         OnSerializeToStreamAsync ();
70
71                                 return base.SerializeToStreamAsync (stream, context);
72                         }
73                 }
74
75                 class ExceptionStream : MemoryStream
76                 {
77                         public ExceptionStream ()
78                         {
79                                 base.WriteByte (10);
80                                 base.Seek (0, SeekOrigin.Begin);
81                         }
82
83                         public override int Read (byte[] buffer, int offset, int count)
84                         {
85                                 throw new ApplicationException ("Read");
86                         }
87
88                         public override byte[] GetBuffer ()
89                         {
90                                 throw new ApplicationException ("GetBuffer");
91                         }
92                 }
93
94                 class CannotSeekStream : MemoryStream
95                 {
96                         public CannotSeekStream ()
97                                 : base (new byte [11])
98                         {
99                         }
100
101                         public override bool CanSeek {
102                                 get {
103                                         return false;
104                                 }
105                         }
106                 }
107
108                 [Test]
109                 public void Ctor_Invalid ()
110                 {
111                         try {
112                                 new StreamContent (null);
113                                 Assert.Fail ("#1");
114                         } catch (ArgumentNullException) {
115                         }
116
117                         try {
118                                 new StreamContent (new MemoryStream (), 0);
119                                 Assert.Fail ("#2");
120                         } catch (ArgumentOutOfRangeException) {
121                         }
122                 }
123
124                 [Test]
125                 public void Ctor ()
126                 {
127                         var ms = new MemoryStream ();
128                         ms.WriteByte (44);
129
130                         using (var m = new StreamContent (ms)) {
131                         }
132                 }
133
134                 [Test]
135                 public void CopyToAsync_Invalid ()
136                 {
137                         var m = new MemoryStream ();
138
139                         var sc = new StreamContent (new MemoryStream ());
140                         try {
141                                 sc.CopyToAsync (null);
142                                 Assert.Fail ("#1");
143                         } catch (ArgumentNullException) {
144                         }
145
146                         //
147                         // For some reason does not work on .net
148                         //
149                         /*
150                         sc = new StreamContent (new ExceptionStream ());
151                         try {
152                             sc.CopyToAsync (m).Wait ();
153                             Assert.Fail ("#2");
154                         } catch (AggregateException) {
155                         }
156                         */ 
157                 }
158
159                 [Test]
160                 /*
161                  * The .NET runtime hits the "#9" assertion.
162                  * The test succeeds with Mono.
163                  */
164                 [Category ("NotDotNet")]
165                 public void CopyToAsync ()
166                 {
167                         var ms = new MemoryStream ();
168                         ms.WriteByte (4);
169                         ms.WriteByte (2);
170                         ms.Seek (0, SeekOrigin.Begin);
171
172                         var sc = new StreamContent (ms);
173
174                         var dest = new MemoryStream ();
175                         var task = sc.CopyToAsync (dest);
176                         task.Wait ();
177                         Assert.AreEqual (2, dest.Length, "#1");
178
179                         bool hit = false;
180                         dest = new MemoryStream ();
181                         var scm = new StreamContentMock (new ExceptionStream ());
182                         scm.OnSerializeToStreamAsync = () => { hit = true; };
183                         task = scm.CopyToAsync (dest);
184                         try {
185                                 task.Wait ();
186                                 Assert.Fail ("#9");
187                         } catch (AggregateException) {
188                         }
189
190                         Assert.IsTrue (hit, "#10");
191                 }
192
193                 [Test]
194                 public void CopyToAsync_ClosedInput ()
195                 {
196                         var stream = new MemoryStream (new byte[] { 1 });
197                         var content = new StreamContent (stream);
198                         Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
199                         stream.Close ();
200
201                         var stream_out = new MemoryStream (10);
202                         Assert.IsTrue (content.CopyToAsync (stream_out).Wait (3000), "#2");
203                 }
204
205                 [Test]
206                 public void Headers ()
207                 {
208                         var ms = new MemoryStream ();
209                         ms.WriteByte (4);
210                         ms.WriteByte (2);
211                         ms.Seek (0, SeekOrigin.Begin);
212
213                         var sc = new StreamContent (ms);
214                         var headers = sc.Headers;
215                         Assert.AreEqual (2, headers.ContentLength, "#1");
216
217                         headers.ContentLength = 400;
218                         Assert.AreEqual (400, headers.ContentLength, "#1a");
219                         headers.ContentLength = null;
220
221                         var scm = new StreamContentMock (MemoryStream.Null);
222                         scm.OnTryComputeLength = () => 330;
223                         Assert.AreEqual (330, scm.Headers.ContentLength, "#2");
224
225                         headers.Allow.Add ("a1");
226                         headers.ContentDisposition = new ContentDispositionHeaderValue ("cd1");
227                         headers.ContentEncoding.Add ("ce1");
228                         headers.ContentLanguage.Add ("cl1");
229                         headers.ContentLength = 23;
230                         headers.ContentLocation = new Uri ("http://xamarin.com");
231                         headers.ContentMD5 = new byte[] { 3, 5 };
232                         headers.ContentRange = new ContentRangeHeaderValue (88, 444);
233                         headers.ContentType = new MediaTypeHeaderValue ("multipart/*");
234                         headers.Expires = new DateTimeOffset (DateTime.Today);
235                         headers.LastModified = new DateTimeOffset (DateTime.Today);
236
237
238                         headers.Add ("allow", "a2");
239                         try {
240                                 headers.Add ("content-disposition", "cd2");
241                                 Assert.Fail ("content-disposition");
242                         } catch (FormatException) {
243                         }
244
245                         headers.Add ("content-encoding", "ce3");
246                         headers.Add ("content-language", "cl2");
247
248                         try {
249                                 headers.Add ("content-length", "444");
250                                 Assert.Fail ("content-length");
251                         } catch (FormatException) {
252                         }
253
254                         try {
255                                 headers.Add ("content-location", "cl2");
256                                 Assert.Fail ("content-location");
257                         } catch (FormatException) {
258                         }
259
260                         try {
261                                 headers.Add ("content-MD5", "cmd5");
262                                 Assert.Fail ("content-MD5");
263                         } catch (FormatException) {
264                         }
265
266                         try {
267                                 headers.Add ("content-range", "133");
268                                 Assert.Fail ("content-range");
269                         } catch (FormatException) {
270                         }
271
272                         try {
273                                 headers.Add ("content-type", "ctype");
274                                 Assert.Fail ("content-type");
275                         } catch (FormatException) {
276                         }
277
278                         try {
279                                 headers.Add ("expires", "ctype");
280                                 Assert.Fail ("expires");
281                         } catch (FormatException) {
282                         }
283
284                         try {
285                                 headers.Add ("last-modified", "lmo");
286                                 Assert.Fail ("last-modified");
287                         } catch (FormatException) {
288                         }
289
290                         Assert.IsTrue (headers.Allow.SequenceEqual (
291                                 new[] {
292                                         "a1",
293                                         "a2"
294                                 }
295                         ));
296
297                         Assert.IsTrue (headers.ContentEncoding.SequenceEqual (
298                                 new[] {
299                                         "ce1",
300                                         "ce3"
301                                 }
302                         ));
303
304                         Assert.IsTrue (headers.ContentLanguage.SequenceEqual (
305                                 new[] {
306                                         "cl1",
307                                         "cl2"
308                                 }
309                         ));
310
311                         Assert.AreEqual (23, headers.ContentLength);
312                         Assert.AreEqual (new Uri ("http://xamarin.com"), headers.ContentLocation);
313                         Assert.AreEqual (new byte[] { 3, 5 }, headers.ContentMD5);
314                         Assert.AreEqual (new ContentRangeHeaderValue (88, 444), headers.ContentRange);
315                         Assert.AreEqual (new MediaTypeHeaderValue ("multipart/*"), headers.ContentType);
316                         Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.Expires);
317                         Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.LastModified);
318                         Assert.AreEqual (new ContentDispositionHeaderValue ("cd1"), headers.ContentDisposition);
319                 }
320
321                 [Test]
322                 public void Headers_ToString ()
323                 {
324                         var sc = new StreamContent (new MemoryStream ());
325                         var headers = sc.Headers;
326                         headers.ContentMD5 = new byte[] { 3, 5 };
327
328                         Assert.AreEqual ("Content-MD5: AwU=\r\n", headers.ToString (), "#1");
329                 }
330
331                 [Test]
332                 public void Headers_ContentLength ()
333                 {
334                         var content = new StreamContent (new MemoryStream (Encoding.UTF8.GetBytes ("test")));
335                         Assert.AreEqual ("", content.Headers.ToString ());
336                         var length = content.Headers.ContentLength;
337                         Assert.AreEqual ("Content-Length: 4\r\n", content.Headers.ToString ());
338                 }
339
340                 [Test]
341                 public void Headers_Invalid ()
342                 {
343                         var sc = new StreamContent (MemoryStream.Null);
344                         var h = sc.Headers;
345
346                         try {
347                                 h.Add ("Age", "");
348                                 Assert.Fail ("#1");
349                         } catch (InvalidOperationException) {
350                         }
351                 }
352
353                 [Test]
354                 public void Headers_Multi ()
355                 {
356                         var sc = new StreamContent (MemoryStream.Null);
357                         var headers = sc.Headers;
358
359                         headers.Add ("Allow", "");
360                         headers.Add ("Allow", "a , b, c");
361
362                         Assert.AreEqual (3, headers.Allow.Count, "#1a");
363                         Assert.IsTrue (headers.Allow.SequenceEqual (
364                                 new[] { "a", "b", "c" }
365                         ), "#1b");
366                 }
367
368                 [Test]
369                 public void LoadIntoBuffer ()
370                 {
371                         var ms = new MemoryStream ();
372                         ms.WriteByte (4);
373                         ms.Seek (0, SeekOrigin.Begin);
374
375                         var sc = new StreamContent (ms);
376                         Assert.IsTrue (sc.LoadIntoBufferAsync (400).Wait (200));
377                 }
378
379                 [Test]
380                 public void LoadIntoBuffer_BufferOverflow ()
381                 {
382                         var ms = new MemoryStream ();
383                         ms.Write (new byte[10000], 0, 10000);
384                         ms.Seek (0, SeekOrigin.Begin);
385
386                         var sc = new StreamContent (ms);
387                         try {
388                                 Assert.IsTrue (sc.LoadIntoBufferAsync (50).Wait (200));
389                                 Assert.Fail ("#1");
390                         } catch (AggregateException e) {
391                                 Assert.IsTrue (e.InnerException is HttpRequestException, "#2");
392                         }
393                 }
394
395                 [Test]
396                 public void ReadAsByteArrayAsync ()
397                 {
398                         var ms = new MemoryStream ();
399                         ms.WriteByte (4);
400                         ms.WriteByte (55);
401
402                         var sc = new StreamContent (ms);
403                         var res = sc.ReadAsByteArrayAsync ().Result;
404                         Assert.AreEqual (0, res.Length, "#1");
405
406                         ms.Seek (0, SeekOrigin.Begin);
407                         sc = new StreamContent (ms);
408                         res = sc.ReadAsByteArrayAsync ().Result;
409                         Assert.AreEqual (2, res.Length, "#10");
410                         Assert.AreEqual (55, res[1], "#11");
411                 }
412
413                 [Test]
414                 public void ReadAsString ()
415                 {
416                         var ms = new MemoryStream ();
417                         ms.WriteByte (77);
418                         ms.WriteByte (55);
419                         ms.Seek (0, SeekOrigin.Begin);
420
421                         var sc = new StreamContent (ms);
422                         var res = sc.ReadAsStringAsync ().Result;
423                         Assert.AreEqual ("M7", res, "#1");
424                 }
425
426                 [Test]
427                 public void ReadAsStream ()
428                 {
429                         var ms = new MemoryStream ();
430                         ms.WriteByte (77);
431                         ms.WriteByte (55);
432                         ms.Seek (0, SeekOrigin.Begin);
433
434                         var sc = new StreamContent (ms);
435                         var res = sc.ReadAsStreamAsync ().Result;
436                         Assert.AreEqual (77, res.ReadByte (), "#1");
437                 }
438
439                 [Test]
440                 public void ReadAsStreamAsync_ClosedInput ()
441                 {
442                         var stream = new MemoryStream (new byte[] { 1 });
443                         var content = new StreamContent (stream);
444                         Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
445                         stream.Close ();
446
447                         var stream_read = content.ReadAsStreamAsync ().Result;
448                         Assert.IsTrue (stream_read.CanSeek, "#2");
449                         Assert.AreEqual (0, stream_read.Position, "#3");        
450                         Assert.AreEqual (1, stream_read.Length, "#4");
451                 }
452
453                 [Test]
454                 public void ContentLengthAfterLoad ()
455                 {
456                         var sc = new StreamContent (new CannotSeekStream ());
457                         Assert.IsTrue (sc.LoadIntoBufferAsync ().Wait (3000), "#1");
458                         Assert.AreEqual (11, sc.Headers.ContentLength, "#2");
459                 }
460         }
461 }