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");
94 public void Ctor_Invalid ()
97 new StreamContent (null);
99 } catch (ArgumentNullException) {
103 new StreamContent (new MemoryStream (), 0);
105 } catch (ArgumentOutOfRangeException) {
112 var ms = new MemoryStream ();
115 using (var m = new StreamContent (ms)) {
120 public void CopyToAsync_Invalid ()
122 var m = new MemoryStream ();
124 var sc = new StreamContent (new MemoryStream ());
126 sc.CopyToAsync (null);
128 } catch (ArgumentNullException) {
132 // For some reason does not work on .net
135 sc = new StreamContent (new ExceptionStream ());
137 sc.CopyToAsync (m).Wait ();
139 } catch (AggregateException) {
146 * The .NET runtime hits the "#9" assertion.
147 * The test succeeds with Mono.
149 [Category ("NotWorking")]
150 public void CopyToAsync ()
152 var ms = new MemoryStream ();
155 ms.Seek (0, SeekOrigin.Begin);
157 var sc = new StreamContent (ms);
159 var dest = new MemoryStream ();
160 var task = sc.CopyToAsync (dest);
162 Assert.AreEqual (2, dest.Length, "#1");
165 dest = new MemoryStream ();
166 var scm = new StreamContentMock (new ExceptionStream ());
167 scm.OnSerializeToStreamAsync = () => { hit = true; };
168 task = scm.CopyToAsync (dest);
172 } catch (AggregateException) {
175 Assert.IsTrue (hit, "#10");
179 public void CopyToAsync_ClosedInput ()
181 var stream = new MemoryStream (new byte[] { 1 });
182 var content = new StreamContent (stream);
183 Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
186 var stream_out = new MemoryStream (10);
187 Assert.IsTrue (content.CopyToAsync (stream_out).Wait (3000), "#2");
191 public void Headers ()
193 var ms = new MemoryStream ();
196 ms.Seek (0, SeekOrigin.Begin);
198 var sc = new StreamContent (ms);
199 var headers = sc.Headers;
200 Assert.AreEqual (2, headers.ContentLength, "#1");
202 headers.ContentLength = 400;
203 Assert.AreEqual (400, headers.ContentLength, "#1a");
204 headers.ContentLength = null;
206 var scm = new StreamContentMock (MemoryStream.Null);
207 scm.OnTryComputeLength = () => 330;
208 Assert.AreEqual (330, scm.Headers.ContentLength, "#2");
210 headers.Allow.Add ("a1");
211 headers.ContentEncoding.Add ("ce1");
212 headers.ContentLanguage.Add ("cl1");
213 headers.ContentLength = 23;
214 headers.ContentLocation = new Uri ("http://xamarin.com");
215 headers.ContentMD5 = new byte[] { 3, 5 };
216 headers.ContentRange = new ContentRangeHeaderValue (88, 444);
217 headers.ContentType = new MediaTypeHeaderValue ("multipart/*");
218 headers.Expires = new DateTimeOffset (DateTime.Today);
219 headers.LastModified = new DateTimeOffset (DateTime.Today);
222 headers.Add ("allow", "a2");
223 headers.Add ("content-encoding", "ce3");
224 headers.Add ("content-language", "cl2");
227 headers.Add ("content-length", "444");
228 Assert.Fail ("content-length");
229 } catch (FormatException) {
233 headers.Add ("content-location", "cl2");
234 Assert.Fail ("content-location");
235 } catch (FormatException) {
239 headers.Add ("content-MD5", "cmd5");
240 Assert.Fail ("content-MD5");
241 } catch (FormatException) {
245 headers.Add ("content-range", "133");
246 Assert.Fail ("content-range");
247 } catch (FormatException) {
251 headers.Add ("content-type", "ctype");
252 Assert.Fail ("content-type");
253 } catch (FormatException) {
257 headers.Add ("expires", "ctype");
258 Assert.Fail ("expires");
259 } catch (FormatException) {
263 headers.Add ("last-modified", "lmo");
264 Assert.Fail ("last-modified");
265 } catch (FormatException) {
268 Assert.IsTrue (headers.Allow.SequenceEqual (
275 Assert.IsTrue (headers.ContentEncoding.SequenceEqual (
282 Assert.IsTrue (headers.ContentLanguage.SequenceEqual (
289 Assert.AreEqual (23, headers.ContentLength);
290 Assert.AreEqual (new Uri ("http://xamarin.com"), headers.ContentLocation);
291 Assert.AreEqual (new byte[] { 3, 5 }, headers.ContentMD5);
292 Assert.AreEqual (new ContentRangeHeaderValue (88, 444), headers.ContentRange);
293 Assert.AreEqual (new MediaTypeHeaderValue ("multipart/*"), headers.ContentType);
294 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.Expires);
295 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.LastModified);
299 public void Headers_Invalid ()
301 var sc = new StreamContent (MemoryStream.Null);
307 } catch (InvalidOperationException) {
312 public void Headers_Multi ()
314 var sc = new StreamContent (MemoryStream.Null);
315 var headers = sc.Headers;
317 headers.Add ("Allow", "");
318 headers.Add ("Allow", "a , b, c");
320 Assert.AreEqual (3, headers.Allow.Count, "#1a");
321 Assert.IsTrue (headers.Allow.SequenceEqual (
322 new[] { "a", "b", "c" }
327 public void LoadIntoBuffer ()
329 var ms = new MemoryStream ();
331 ms.Seek (0, SeekOrigin.Begin);
333 var sc = new StreamContent (ms);
334 Assert.IsTrue (sc.LoadIntoBufferAsync (400).Wait (200));
338 public void LoadIntoBuffer_BufferOverflow ()
340 var ms = new MemoryStream ();
341 ms.Write (new byte[10000], 0, 10000);
342 ms.Seek (0, SeekOrigin.Begin);
344 var sc = new StreamContent (ms);
346 Assert.IsTrue (sc.LoadIntoBufferAsync (50).Wait (200));
348 } catch (AggregateException e) {
349 Assert.IsTrue (e.InnerException is HttpRequestException, "#2");
354 public void ReadAsByteArrayAsync ()
356 var ms = new MemoryStream ();
360 var sc = new StreamContent (ms);
361 var res = sc.ReadAsByteArrayAsync ().Result;
362 Assert.AreEqual (0, res.Length, "#1");
364 ms.Seek (0, SeekOrigin.Begin);
365 sc = new StreamContent (ms);
366 res = sc.ReadAsByteArrayAsync ().Result;
367 Assert.AreEqual (2, res.Length, "#10");
368 Assert.AreEqual (55, res[1], "#11");
372 public void ReadAsString ()
374 var ms = new MemoryStream ();
377 ms.Seek (0, SeekOrigin.Begin);
379 var sc = new StreamContent (ms);
380 var res = sc.ReadAsStringAsync ().Result;
381 Assert.AreEqual ("M7", res, "#1");
385 public void ReadAsStream ()
387 var ms = new MemoryStream ();
390 ms.Seek (0, SeekOrigin.Begin);
392 var sc = new StreamContent (ms);
393 var res = sc.ReadAsStreamAsync ().Result;
394 Assert.AreEqual (77, res.ReadByte (), "#1");
398 public void ReadAsStreamAsync_ClosedInput ()
400 var stream = new MemoryStream (new byte[] { 1 });
401 var content = new StreamContent (stream);
402 Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
405 var stream_read = content.ReadAsStreamAsync ().Result;
406 Assert.IsTrue (stream_read.CanSeek, "#2");
407 Assert.AreEqual (0, stream_read.Position, "#3");
408 Assert.AreEqual (1, stream_read.Length, "#4");