2 // StreamContentTest.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 System.Collections;
31 using System.Collections.Generic;
32 using NUnit.Framework;
33 using System.Net.Http;
34 using System.Net.Http.Headers;
36 using System.Threading.Tasks;
41 namespace MonoTests.System.Net.Http
44 public class StreamContentTest
46 class StreamContentMock : StreamContent
48 public Func<long> OnTryComputeLength;
49 public Action OnSerializeToStreamAsync;
51 public StreamContentMock (Stream stream)
56 protected override bool TryComputeLength (out long length)
58 if (OnTryComputeLength != null) {
59 length = OnTryComputeLength ();
63 return base.TryComputeLength (out length);
66 protected override Task SerializeToStreamAsync (Stream stream, TransportContext context)
68 if (OnSerializeToStreamAsync != null)
69 OnSerializeToStreamAsync ();
71 return base.SerializeToStreamAsync (stream, context);
75 class ExceptionStream : MemoryStream
77 public ExceptionStream ()
80 base.Seek (0, SeekOrigin.Begin);
83 public override int Read (byte[] buffer, int offset, int count)
85 throw new ApplicationException ("Read");
88 public override byte[] GetBuffer ()
90 throw new ApplicationException ("GetBuffer");
94 class CannotSeekStream : MemoryStream
96 public CannotSeekStream ()
97 : base (new byte [11])
101 public override bool CanSeek {
109 public void Ctor_Invalid ()
112 new StreamContent (null);
114 } catch (ArgumentNullException) {
118 new StreamContent (new MemoryStream (), 0);
120 } catch (ArgumentOutOfRangeException) {
127 var ms = new MemoryStream ();
130 using (var m = new StreamContent (ms)) {
135 public void CopyToAsync_Invalid ()
137 var m = new MemoryStream ();
139 var sc = new StreamContent (new MemoryStream ());
141 sc.CopyToAsync (null);
143 } catch (ArgumentNullException) {
147 // For some reason does not work on .net
150 sc = new StreamContent (new ExceptionStream ());
152 sc.CopyToAsync (m).Wait ();
154 } catch (AggregateException) {
161 * The .NET runtime hits the "#9" assertion.
162 * The test succeeds with Mono.
164 [Category ("NotDotNet")]
165 public void CopyToAsync ()
167 var ms = new MemoryStream ();
170 ms.Seek (0, SeekOrigin.Begin);
172 var sc = new StreamContent (ms);
174 var dest = new MemoryStream ();
175 var task = sc.CopyToAsync (dest);
177 Assert.AreEqual (2, dest.Length, "#1");
180 dest = new MemoryStream ();
181 var scm = new StreamContentMock (new ExceptionStream ());
182 scm.OnSerializeToStreamAsync = () => { hit = true; };
183 task = scm.CopyToAsync (dest);
187 } catch (AggregateException) {
190 Assert.IsTrue (hit, "#10");
194 public void CopyToAsync_Twice ()
196 var ms = new MemoryStream();
200 ms.Seek(1, SeekOrigin.Begin);
202 var sc = new StreamContent(ms);
204 var dest = new MemoryStream();
205 var task = sc.CopyToAsync(dest);
206 Assert.True(task.Wait(3000), "#0");
207 Assert.AreEqual(2, dest.Length, "#1");
208 dest.Seek(0, SeekOrigin.Begin);
209 Assert.AreEqual(12, dest.ReadByte(), "#2");
211 dest = new MemoryStream();
212 task = sc.CopyToAsync(dest);
213 Assert.True(task.Wait(3000), "#10");
214 Assert.AreEqual(2, dest.Length, "#11");
215 dest.Seek(0, SeekOrigin.Begin);
216 Assert.AreEqual(12, dest.ReadByte(), "#12");
220 public void CopyToAsync_ClosedInput ()
222 var stream = new MemoryStream (new byte[] { 1 });
223 var content = new StreamContent (stream);
224 Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
227 var stream_out = new MemoryStream (10);
228 Assert.IsTrue (content.CopyToAsync (stream_out).Wait (3000), "#2");
232 public void Headers ()
234 var ms = new MemoryStream ();
237 ms.Seek (0, SeekOrigin.Begin);
239 var sc = new StreamContent (ms);
240 var headers = sc.Headers;
241 Assert.AreEqual (2, headers.ContentLength, "#1");
243 headers.ContentLength = 400;
244 Assert.AreEqual (400, headers.ContentLength, "#1a");
245 headers.ContentLength = null;
247 var scm = new StreamContentMock (MemoryStream.Null);
248 scm.OnTryComputeLength = () => 330;
249 Assert.AreEqual (330, scm.Headers.ContentLength, "#2");
251 headers.Allow.Add ("a1");
252 headers.ContentDisposition = new ContentDispositionHeaderValue ("cd1");
253 headers.ContentEncoding.Add ("ce1");
254 headers.ContentLanguage.Add ("cl1");
255 headers.ContentLength = 23;
256 headers.ContentLocation = new Uri ("http://xamarin.com");
257 headers.ContentMD5 = new byte[] { 3, 5 };
258 headers.ContentRange = new ContentRangeHeaderValue (88, 444);
259 headers.ContentType = new MediaTypeHeaderValue ("multipart/*");
260 headers.Expires = new DateTimeOffset (DateTime.Today);
261 headers.LastModified = new DateTimeOffset (DateTime.Today);
264 headers.Add ("allow", "a2");
266 headers.Add ("content-disposition", "cd2");
267 Assert.Fail ("content-disposition");
268 } catch (FormatException) {
271 headers.Add ("content-encoding", "ce3");
272 headers.Add ("content-language", "cl2");
275 headers.Add ("content-length", "444");
276 Assert.Fail ("content-length");
277 } catch (FormatException) {
281 headers.Add ("content-location", "cl2");
282 Assert.Fail ("content-location");
283 } catch (FormatException) {
287 headers.Add ("content-MD5", "cmd5");
288 Assert.Fail ("content-MD5");
289 } catch (FormatException) {
293 headers.Add ("content-range", "133");
294 Assert.Fail ("content-range");
295 } catch (FormatException) {
299 headers.Add ("content-type", "ctype");
300 Assert.Fail ("content-type");
301 } catch (FormatException) {
305 headers.Add ("expires", "ctype");
306 Assert.Fail ("expires");
307 } catch (FormatException) {
311 headers.Add ("last-modified", "lmo");
312 Assert.Fail ("last-modified");
313 } catch (FormatException) {
316 Assert.IsTrue (headers.Allow.SequenceEqual (
323 Assert.IsTrue (headers.ContentEncoding.SequenceEqual (
330 Assert.IsTrue (headers.ContentLanguage.SequenceEqual (
337 Assert.AreEqual (23, headers.ContentLength);
338 Assert.AreEqual (new Uri ("http://xamarin.com"), headers.ContentLocation);
339 Assert.AreEqual (new byte[] { 3, 5 }, headers.ContentMD5);
340 Assert.AreEqual (new ContentRangeHeaderValue (88, 444), headers.ContentRange);
341 Assert.AreEqual (new MediaTypeHeaderValue ("multipart/*"), headers.ContentType);
342 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.Expires);
343 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.LastModified);
344 Assert.AreEqual (new ContentDispositionHeaderValue ("cd1"), headers.ContentDisposition);
348 public void Headers_ToString ()
350 var sc = new StreamContent (new MemoryStream ());
351 var headers = sc.Headers;
352 headers.ContentMD5 = new byte[] { 3, 5 };
354 Assert.AreEqual ("Content-MD5: AwU=\r\n", headers.ToString (), "#1");
358 public void Headers_ContentLength ()
360 var content = new StreamContent (new MemoryStream (Encoding.UTF8.GetBytes ("test")));
361 Assert.AreEqual ("", content.Headers.ToString ());
362 var length = content.Headers.ContentLength;
363 Assert.AreEqual ("Content-Length: 4\r\n", content.Headers.ToString ());
367 public void Headers_Invalid ()
369 var sc = new StreamContent (MemoryStream.Null);
375 } catch (InvalidOperationException) {
380 public void Headers_Multi ()
382 var sc = new StreamContent (MemoryStream.Null);
383 var headers = sc.Headers;
385 headers.Add ("Allow", "");
386 headers.Add ("Allow", "a , b, c");
388 Assert.AreEqual (3, headers.Allow.Count, "#1a");
389 Assert.IsTrue (headers.Allow.SequenceEqual (
390 new[] { "a", "b", "c" }
395 public void LoadIntoBuffer ()
397 var ms = new MemoryStream ();
399 ms.Seek (0, SeekOrigin.Begin);
401 var sc = new StreamContent (ms);
402 Assert.IsTrue (sc.LoadIntoBufferAsync (400).Wait (200));
406 public void LoadIntoBuffer_BufferOverflow ()
408 var ms = new MemoryStream ();
409 ms.Write (new byte[10000], 0, 10000);
410 ms.Seek (0, SeekOrigin.Begin);
412 var sc = new StreamContent (ms);
414 Assert.IsTrue (sc.LoadIntoBufferAsync (50).Wait (200));
416 } catch (AggregateException e) {
417 Assert.IsTrue (e.InnerException is HttpRequestException, "#2");
422 public void ReadAsByteArrayAsync ()
424 var ms = new MemoryStream ();
428 var sc = new StreamContent (ms);
429 var res = sc.ReadAsByteArrayAsync ().Result;
430 Assert.AreEqual (0, res.Length, "#1");
432 ms.Seek (0, SeekOrigin.Begin);
433 sc = new StreamContent (ms);
434 res = sc.ReadAsByteArrayAsync ().Result;
435 Assert.AreEqual (2, res.Length, "#10");
436 Assert.AreEqual (55, res[1], "#11");
440 public void ReadAsString ()
442 var ms = new MemoryStream ();
445 ms.Seek (0, SeekOrigin.Begin);
447 var sc = new StreamContent (ms);
448 var res = sc.ReadAsStringAsync ().Result;
449 Assert.AreEqual ("M7", res, "#1");
453 public void ReadAsStream ()
455 var ms = new MemoryStream ();
458 ms.Seek (0, SeekOrigin.Begin);
460 var sc = new StreamContent (ms);
461 var res = sc.ReadAsStreamAsync ().Result;
462 Assert.AreEqual (77, res.ReadByte (), "#1");
466 public void ReadAsStreamAsync_ClosedInput ()
468 var stream = new MemoryStream (new byte[] { 1 });
469 var content = new StreamContent (stream);
470 Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
473 var stream_read = content.ReadAsStreamAsync ().Result;
474 Assert.IsTrue (stream_read.CanSeek, "#2");
475 Assert.AreEqual (0, stream_read.Position, "#3");
476 Assert.AreEqual (1, stream_read.Length, "#4");
480 public void ContentLengthAfterLoad ()
482 var sc = new StreamContent (new CannotSeekStream ());
483 Assert.IsTrue (sc.LoadIntoBufferAsync ().Wait (3000), "#1");
484 Assert.AreEqual (11, sc.Headers.ContentLength, "#2");