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