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;
40 namespace MonoTests.System.Net.Http
43 public class StreamContentTest
45 class StreamContentMock : StreamContent
47 public Func<long> OnTryComputeLength;
48 public Action OnSerializeToStreamAsync;
50 public StreamContentMock (Stream stream)
55 protected override bool TryComputeLength (out long length)
57 if (OnTryComputeLength != null) {
58 length = OnTryComputeLength ();
62 return base.TryComputeLength (out length);
65 protected override Task SerializeToStreamAsync (Stream stream, TransportContext context)
67 if (OnSerializeToStreamAsync != null)
68 OnSerializeToStreamAsync ();
70 return base.SerializeToStreamAsync (stream, context);
74 class ExceptionStream : MemoryStream
76 public ExceptionStream ()
79 base.Seek (0, SeekOrigin.Begin);
82 public override int Read (byte[] buffer, int offset, int count)
84 throw new ApplicationException ("Read");
87 public override byte[] GetBuffer ()
89 throw new ApplicationException ("GetBuffer");
93 class CannotSeekStream : MemoryStream
95 public CannotSeekStream ()
96 : base (new byte [11])
100 public override bool CanSeek {
108 public void Ctor_Invalid ()
111 new StreamContent (null);
113 } catch (ArgumentNullException) {
117 new StreamContent (new MemoryStream (), 0);
119 } catch (ArgumentOutOfRangeException) {
126 var ms = new MemoryStream ();
129 using (var m = new StreamContent (ms)) {
134 public void CopyToAsync_Invalid ()
136 var m = new MemoryStream ();
138 var sc = new StreamContent (new MemoryStream ());
140 sc.CopyToAsync (null);
142 } catch (ArgumentNullException) {
146 // For some reason does not work on .net
149 sc = new StreamContent (new ExceptionStream ());
151 sc.CopyToAsync (m).Wait ();
153 } catch (AggregateException) {
160 * The .NET runtime hits the "#9" assertion.
161 * The test succeeds with Mono.
163 [Category ("NotDotNet")]
164 public void CopyToAsync ()
166 var ms = new MemoryStream ();
169 ms.Seek (0, SeekOrigin.Begin);
171 var sc = new StreamContent (ms);
173 var dest = new MemoryStream ();
174 var task = sc.CopyToAsync (dest);
176 Assert.AreEqual (2, dest.Length, "#1");
179 dest = new MemoryStream ();
180 var scm = new StreamContentMock (new ExceptionStream ());
181 scm.OnSerializeToStreamAsync = () => { hit = true; };
182 task = scm.CopyToAsync (dest);
186 } catch (AggregateException) {
189 Assert.IsTrue (hit, "#10");
193 public void CopyToAsync_ClosedInput ()
195 var stream = new MemoryStream (new byte[] { 1 });
196 var content = new StreamContent (stream);
197 Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
200 var stream_out = new MemoryStream (10);
201 Assert.IsTrue (content.CopyToAsync (stream_out).Wait (3000), "#2");
205 public void Headers ()
207 var ms = new MemoryStream ();
210 ms.Seek (0, SeekOrigin.Begin);
212 var sc = new StreamContent (ms);
213 var headers = sc.Headers;
214 Assert.AreEqual (2, headers.ContentLength, "#1");
216 headers.ContentLength = 400;
217 Assert.AreEqual (400, headers.ContentLength, "#1a");
218 headers.ContentLength = null;
220 var scm = new StreamContentMock (MemoryStream.Null);
221 scm.OnTryComputeLength = () => 330;
222 Assert.AreEqual (330, scm.Headers.ContentLength, "#2");
224 headers.Allow.Add ("a1");
225 headers.ContentDisposition = new ContentDispositionHeaderValue ("cd1");
226 headers.ContentEncoding.Add ("ce1");
227 headers.ContentLanguage.Add ("cl1");
228 headers.ContentLength = 23;
229 headers.ContentLocation = new Uri ("http://xamarin.com");
230 headers.ContentMD5 = new byte[] { 3, 5 };
231 headers.ContentRange = new ContentRangeHeaderValue (88, 444);
232 headers.ContentType = new MediaTypeHeaderValue ("multipart/*");
233 headers.Expires = new DateTimeOffset (DateTime.Today);
234 headers.LastModified = new DateTimeOffset (DateTime.Today);
237 headers.Add ("allow", "a2");
239 headers.Add ("content-disposition", "cd2");
240 Assert.Fail ("content-disposition");
241 } catch (FormatException) {
244 headers.Add ("content-encoding", "ce3");
245 headers.Add ("content-language", "cl2");
248 headers.Add ("content-length", "444");
249 Assert.Fail ("content-length");
250 } catch (FormatException) {
254 headers.Add ("content-location", "cl2");
255 Assert.Fail ("content-location");
256 } catch (FormatException) {
260 headers.Add ("content-MD5", "cmd5");
261 Assert.Fail ("content-MD5");
262 } catch (FormatException) {
266 headers.Add ("content-range", "133");
267 Assert.Fail ("content-range");
268 } catch (FormatException) {
272 headers.Add ("content-type", "ctype");
273 Assert.Fail ("content-type");
274 } catch (FormatException) {
278 headers.Add ("expires", "ctype");
279 Assert.Fail ("expires");
280 } catch (FormatException) {
284 headers.Add ("last-modified", "lmo");
285 Assert.Fail ("last-modified");
286 } catch (FormatException) {
289 Assert.IsTrue (headers.Allow.SequenceEqual (
296 Assert.IsTrue (headers.ContentEncoding.SequenceEqual (
303 Assert.IsTrue (headers.ContentLanguage.SequenceEqual (
310 Assert.AreEqual (23, headers.ContentLength);
311 Assert.AreEqual (new Uri ("http://xamarin.com"), headers.ContentLocation);
312 Assert.AreEqual (new byte[] { 3, 5 }, headers.ContentMD5);
313 Assert.AreEqual (new ContentRangeHeaderValue (88, 444), headers.ContentRange);
314 Assert.AreEqual (new MediaTypeHeaderValue ("multipart/*"), headers.ContentType);
315 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.Expires);
316 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.LastModified);
317 Assert.AreEqual (new ContentDispositionHeaderValue ("cd1"), headers.ContentDisposition);
321 public void Headers_ToString ()
323 var sc = new StreamContent (new MemoryStream ());
324 var headers = sc.Headers;
325 headers.ContentMD5 = new byte[] { 3, 5 };
327 Assert.AreEqual ("Content-MD5: AwU=\r\n", headers.ToString (), "#1");
331 public void Headers_Invalid ()
333 var sc = new StreamContent (MemoryStream.Null);
339 } catch (InvalidOperationException) {
344 public void Headers_Multi ()
346 var sc = new StreamContent (MemoryStream.Null);
347 var headers = sc.Headers;
349 headers.Add ("Allow", "");
350 headers.Add ("Allow", "a , b, c");
352 Assert.AreEqual (3, headers.Allow.Count, "#1a");
353 Assert.IsTrue (headers.Allow.SequenceEqual (
354 new[] { "a", "b", "c" }
359 public void LoadIntoBuffer ()
361 var ms = new MemoryStream ();
363 ms.Seek (0, SeekOrigin.Begin);
365 var sc = new StreamContent (ms);
366 Assert.IsTrue (sc.LoadIntoBufferAsync (400).Wait (200));
370 public void LoadIntoBuffer_BufferOverflow ()
372 var ms = new MemoryStream ();
373 ms.Write (new byte[10000], 0, 10000);
374 ms.Seek (0, SeekOrigin.Begin);
376 var sc = new StreamContent (ms);
378 Assert.IsTrue (sc.LoadIntoBufferAsync (50).Wait (200));
380 } catch (AggregateException e) {
381 Assert.IsTrue (e.InnerException is HttpRequestException, "#2");
386 public void ReadAsByteArrayAsync ()
388 var ms = new MemoryStream ();
392 var sc = new StreamContent (ms);
393 var res = sc.ReadAsByteArrayAsync ().Result;
394 Assert.AreEqual (0, res.Length, "#1");
396 ms.Seek (0, SeekOrigin.Begin);
397 sc = new StreamContent (ms);
398 res = sc.ReadAsByteArrayAsync ().Result;
399 Assert.AreEqual (2, res.Length, "#10");
400 Assert.AreEqual (55, res[1], "#11");
404 public void ReadAsString ()
406 var ms = new MemoryStream ();
409 ms.Seek (0, SeekOrigin.Begin);
411 var sc = new StreamContent (ms);
412 var res = sc.ReadAsStringAsync ().Result;
413 Assert.AreEqual ("M7", res, "#1");
417 public void ReadAsStream ()
419 var ms = new MemoryStream ();
422 ms.Seek (0, SeekOrigin.Begin);
424 var sc = new StreamContent (ms);
425 var res = sc.ReadAsStreamAsync ().Result;
426 Assert.AreEqual (77, res.ReadByte (), "#1");
430 public void ReadAsStreamAsync_ClosedInput ()
432 var stream = new MemoryStream (new byte[] { 1 });
433 var content = new StreamContent (stream);
434 Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
437 var stream_read = content.ReadAsStreamAsync ().Result;
438 Assert.IsTrue (stream_read.CanSeek, "#2");
439 Assert.AreEqual (0, stream_read.Position, "#3");
440 Assert.AreEqual (1, stream_read.Length, "#4");
444 public void ContentLengthAfterLoad ()
446 var sc = new StreamContent (new CannotSeekStream ());
447 Assert.IsTrue (sc.LoadIntoBufferAsync ().Wait (3000), "#1");
448 Assert.AreEqual (11, sc.Headers.ContentLength, "#2");