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 Headers ()
181 var ms = new MemoryStream ();
184 ms.Seek (0, SeekOrigin.Begin);
186 var sc = new StreamContent (ms);
187 var headers = sc.Headers;
188 Assert.AreEqual (2, headers.ContentLength, "#1");
190 headers.ContentLength = 400;
191 Assert.AreEqual (400, headers.ContentLength, "#1a");
192 headers.ContentLength = null;
194 var scm = new StreamContentMock (MemoryStream.Null);
195 scm.OnTryComputeLength = () => 330;
196 Assert.AreEqual (330, scm.Headers.ContentLength, "#2");
198 headers.Allow.Add ("a1");
199 headers.ContentEncoding.Add ("ce1");
200 headers.ContentLanguage.Add ("cl1");
201 headers.ContentLength = 23;
202 headers.ContentLocation = new Uri ("http://xamarin.com");
203 headers.ContentMD5 = new byte[] { 3, 5 };
204 headers.ContentRange = new ContentRangeHeaderValue (88, 444);
205 headers.ContentType = new MediaTypeHeaderValue ("multipart/*");
206 headers.Expires = new DateTimeOffset (DateTime.Today);
207 headers.LastModified = new DateTimeOffset (DateTime.Today);
210 headers.Add ("allow", "a2");
211 headers.Add ("content-encoding", "ce3");
212 headers.Add ("content-language", "cl2");
215 headers.Add ("content-length", "444");
216 Assert.Fail ("content-length");
217 } catch (FormatException) {
221 headers.Add ("content-location", "cl2");
222 Assert.Fail ("content-location");
223 } catch (FormatException) {
227 headers.Add ("content-MD5", "cmd5");
228 Assert.Fail ("content-MD5");
229 } catch (FormatException) {
233 headers.Add ("content-range", "133");
234 Assert.Fail ("content-range");
235 } catch (FormatException) {
239 headers.Add ("content-type", "ctype");
240 Assert.Fail ("content-type");
241 } catch (FormatException) {
245 headers.Add ("expires", "ctype");
246 Assert.Fail ("expires");
247 } catch (FormatException) {
251 headers.Add ("last-modified", "lmo");
252 Assert.Fail ("last-modified");
253 } catch (FormatException) {
256 Assert.IsTrue (headers.Allow.SequenceEqual (
263 Assert.IsTrue (headers.ContentEncoding.SequenceEqual (
270 Assert.IsTrue (headers.ContentLanguage.SequenceEqual (
277 Assert.AreEqual (23, headers.ContentLength);
278 Assert.AreEqual (new Uri ("http://xamarin.com"), headers.ContentLocation);
279 Assert.AreEqual (new byte[] { 3, 5 }, headers.ContentMD5);
280 Assert.AreEqual (new ContentRangeHeaderValue (88, 444), headers.ContentRange);
281 Assert.AreEqual (new MediaTypeHeaderValue ("multipart/*"), headers.ContentType);
282 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.Expires);
283 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.LastModified);
287 public void Headers_Invalid ()
289 var sc = new StreamContent (MemoryStream.Null);
295 } catch (InvalidOperationException) {
300 public void LoadIntoBuffer ()
302 var ms = new MemoryStream ();
304 ms.Seek (0, SeekOrigin.Begin);
306 var sc = new StreamContent (ms);
307 Assert.IsTrue (sc.LoadIntoBufferAsync (400).Wait (200));
311 public void LoadIntoBuffer_BufferOverflow ()
313 var ms = new MemoryStream ();
314 ms.Write (new byte[10000], 0, 10000);
315 ms.Seek (0, SeekOrigin.Begin);
317 var sc = new StreamContent (ms);
319 Assert.IsTrue (sc.LoadIntoBufferAsync (50).Wait (200));
321 } catch (AggregateException e) {
322 Assert.IsInstanceOfType (typeof (HttpRequestException), e.InnerException, "#2");
327 public void ReadAsByteArrayAsync ()
329 var ms = new MemoryStream ();
333 var sc = new StreamContent (ms);
334 var res = sc.ReadAsByteArrayAsync ().Result;
335 Assert.AreEqual (0, res.Length, "#1");
337 ms.Seek (0, SeekOrigin.Begin);
338 sc = new StreamContent (ms);
339 res = sc.ReadAsByteArrayAsync ().Result;
340 Assert.AreEqual (2, res.Length, "#10");
341 Assert.AreEqual (55, res[1], "#11");
345 public void ReadAsString ()
347 var ms = new MemoryStream ();
350 ms.Seek (0, SeekOrigin.Begin);
352 var sc = new StreamContent (ms);
353 var res = sc.ReadAsStringAsync ().Result;
354 Assert.AreEqual ("M7", res, "#1");
358 public void ReadAsStream ()
360 var ms = new MemoryStream ();
363 ms.Seek (0, SeekOrigin.Begin);
365 var sc = new StreamContent (ms);
366 var res = sc.ReadAsStreamAsync ().Result;
367 Assert.AreEqual (77, res.ReadByte (), "#1");