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_ClosedInput ()
196 var stream = new MemoryStream (new byte[] { 1 });
197 var content = new StreamContent (stream);
198 Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
201 var stream_out = new MemoryStream (10);
202 Assert.IsTrue (content.CopyToAsync (stream_out).Wait (3000), "#2");
206 public void Headers ()
208 var ms = new MemoryStream ();
211 ms.Seek (0, SeekOrigin.Begin);
213 var sc = new StreamContent (ms);
214 var headers = sc.Headers;
215 Assert.AreEqual (2, headers.ContentLength, "#1");
217 headers.ContentLength = 400;
218 Assert.AreEqual (400, headers.ContentLength, "#1a");
219 headers.ContentLength = null;
221 var scm = new StreamContentMock (MemoryStream.Null);
222 scm.OnTryComputeLength = () => 330;
223 Assert.AreEqual (330, scm.Headers.ContentLength, "#2");
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);
238 headers.Add ("allow", "a2");
240 headers.Add ("content-disposition", "cd2");
241 Assert.Fail ("content-disposition");
242 } catch (FormatException) {
245 headers.Add ("content-encoding", "ce3");
246 headers.Add ("content-language", "cl2");
249 headers.Add ("content-length", "444");
250 Assert.Fail ("content-length");
251 } catch (FormatException) {
255 headers.Add ("content-location", "cl2");
256 Assert.Fail ("content-location");
257 } catch (FormatException) {
261 headers.Add ("content-MD5", "cmd5");
262 Assert.Fail ("content-MD5");
263 } catch (FormatException) {
267 headers.Add ("content-range", "133");
268 Assert.Fail ("content-range");
269 } catch (FormatException) {
273 headers.Add ("content-type", "ctype");
274 Assert.Fail ("content-type");
275 } catch (FormatException) {
279 headers.Add ("expires", "ctype");
280 Assert.Fail ("expires");
281 } catch (FormatException) {
285 headers.Add ("last-modified", "lmo");
286 Assert.Fail ("last-modified");
287 } catch (FormatException) {
290 Assert.IsTrue (headers.Allow.SequenceEqual (
297 Assert.IsTrue (headers.ContentEncoding.SequenceEqual (
304 Assert.IsTrue (headers.ContentLanguage.SequenceEqual (
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);
322 public void Headers_ToString ()
324 var sc = new StreamContent (new MemoryStream ());
325 var headers = sc.Headers;
326 headers.ContentMD5 = new byte[] { 3, 5 };
328 Assert.AreEqual ("Content-MD5: AwU=\r\n", headers.ToString (), "#1");
332 public void Headers_ContentLength ()
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 ());
341 public void Headers_Invalid ()
343 var sc = new StreamContent (MemoryStream.Null);
349 } catch (InvalidOperationException) {
354 public void Headers_Multi ()
356 var sc = new StreamContent (MemoryStream.Null);
357 var headers = sc.Headers;
359 headers.Add ("Allow", "");
360 headers.Add ("Allow", "a , b, c");
362 Assert.AreEqual (3, headers.Allow.Count, "#1a");
363 Assert.IsTrue (headers.Allow.SequenceEqual (
364 new[] { "a", "b", "c" }
369 public void LoadIntoBuffer ()
371 var ms = new MemoryStream ();
373 ms.Seek (0, SeekOrigin.Begin);
375 var sc = new StreamContent (ms);
376 Assert.IsTrue (sc.LoadIntoBufferAsync (400).Wait (200));
380 public void LoadIntoBuffer_BufferOverflow ()
382 var ms = new MemoryStream ();
383 ms.Write (new byte[10000], 0, 10000);
384 ms.Seek (0, SeekOrigin.Begin);
386 var sc = new StreamContent (ms);
388 Assert.IsTrue (sc.LoadIntoBufferAsync (50).Wait (200));
390 } catch (AggregateException e) {
391 Assert.IsTrue (e.InnerException is HttpRequestException, "#2");
396 public void ReadAsByteArrayAsync ()
398 var ms = new MemoryStream ();
402 var sc = new StreamContent (ms);
403 var res = sc.ReadAsByteArrayAsync ().Result;
404 Assert.AreEqual (0, res.Length, "#1");
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");
414 public void ReadAsString ()
416 var ms = new MemoryStream ();
419 ms.Seek (0, SeekOrigin.Begin);
421 var sc = new StreamContent (ms);
422 var res = sc.ReadAsStringAsync ().Result;
423 Assert.AreEqual ("M7", res, "#1");
427 public void ReadAsStream ()
429 var ms = new MemoryStream ();
432 ms.Seek (0, SeekOrigin.Begin);
434 var sc = new StreamContent (ms);
435 var res = sc.ReadAsStreamAsync ().Result;
436 Assert.AreEqual (77, res.ReadByte (), "#1");
440 public void ReadAsStreamAsync_ClosedInput ()
442 var stream = new MemoryStream (new byte[] { 1 });
443 var content = new StreamContent (stream);
444 Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
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");
454 public void ContentLengthAfterLoad ()
456 var sc = new StreamContent (new CannotSeekStream ());
457 Assert.IsTrue (sc.LoadIntoBufferAsync ().Wait (3000), "#1");
458 Assert.AreEqual (11, sc.Headers.ContentLength, "#2");