public void CopyToAsync() { var ms = new MemoryStream(); ms.WriteByte(4); ms.WriteByte(2); ms.Seek(0, SeekOrigin.Begin); var sc = new StreamContent(ms); var dest = new MemoryStream(); var task = sc.CopyToAsync(dest); task.Wait(); Assert.AreEqual(2, dest.Length, "#1"); bool hit = false; dest = new MemoryStream(); var scm = new StreamContentMock(new ExceptionStream()); scm.OnSerializeToStreamAsync = () => { hit = true; }; task = scm.CopyToAsync(dest); try { task.Wait(); Assert.Fail("#9"); } catch (AggregateException) { } Assert.IsTrue(hit, "#10"); }
public void Headers() { var ms = new MemoryStream(); ms.WriteByte(4); ms.WriteByte(2); ms.Seek(0, SeekOrigin.Begin); var sc = new StreamContent(ms); var headers = sc.Headers; Assert.AreEqual(2, headers.ContentLength, "#1"); headers.ContentLength = 400; Assert.AreEqual(400, headers.ContentLength, "#1a"); headers.ContentLength = null; var scm = new StreamContentMock(MemoryStream.Null); scm.OnTryComputeLength = () => 330; Assert.AreEqual(330, scm.Headers.ContentLength, "#2"); headers.Allow.Add("a1"); headers.ContentEncoding.Add("ce1"); headers.ContentLanguage.Add("cl1"); headers.ContentLength = 23; headers.ContentLocation = new Uri("http://xamarin.com"); headers.ContentMD5 = new byte[] { 3, 5 }; headers.ContentRange = new ContentRangeHeaderValue(88, 444); headers.ContentType = new MediaTypeHeaderValue("multipart/*"); headers.Expires = new DateTimeOffset(DateTime.Today); headers.LastModified = new DateTimeOffset(DateTime.Today); headers.Add("allow", "a2"); headers.Add("content-encoding", "ce3"); headers.Add("content-language", "cl2"); try { headers.Add("content-length", "444"); Assert.Fail("content-length"); } catch (FormatException) { } try { headers.Add("content-location", "cl2"); Assert.Fail("content-location"); } catch (FormatException) { } try { headers.Add("content-MD5", "cmd5"); Assert.Fail("content-MD5"); } catch (FormatException) { } try { headers.Add("content-range", "133"); Assert.Fail("content-range"); } catch (FormatException) { } try { headers.Add("content-type", "ctype"); Assert.Fail("content-type"); } catch (FormatException) { } try { headers.Add("expires", "ctype"); Assert.Fail("expires"); } catch (FormatException) { } try { headers.Add("last-modified", "lmo"); Assert.Fail("last-modified"); } catch (FormatException) { } Assert.IsTrue(headers.Allow.SequenceEqual( new[] { "a1", "a2" } )); Assert.IsTrue(headers.ContentEncoding.SequenceEqual( new[] { "ce1", "ce3" } )); Assert.IsTrue(headers.ContentLanguage.SequenceEqual( new[] { "cl1", "cl2" } )); Assert.AreEqual(23, headers.ContentLength); Assert.AreEqual(new Uri("http://xamarin.com"), headers.ContentLocation); Assert.AreEqual(new byte[] { 3, 5 }, headers.ContentMD5); Assert.AreEqual(new ContentRangeHeaderValue(88, 444), headers.ContentRange); Assert.AreEqual(new MediaTypeHeaderValue("multipart/*"), headers.ContentType); Assert.AreEqual(new DateTimeOffset(DateTime.Today), headers.Expires); Assert.AreEqual(new DateTimeOffset(DateTime.Today), headers.LastModified); }
public void Headers () { var ms = new MemoryStream (); ms.WriteByte (4); ms.WriteByte (2); ms.Seek (0, SeekOrigin.Begin); var sc = new StreamContent (ms); var headers = sc.Headers; Assert.AreEqual (2, headers.ContentLength, "#1"); headers.ContentLength = 400; Assert.AreEqual (400, headers.ContentLength, "#1a"); headers.ContentLength = null; var scm = new StreamContentMock (MemoryStream.Null); scm.OnTryComputeLength = () => 330; Assert.AreEqual (330, scm.Headers.ContentLength, "#2"); headers.Allow.Add ("a1"); headers.ContentEncoding.Add ("ce1"); headers.ContentLanguage.Add ("cl1"); headers.ContentLength = 23; headers.ContentLocation = new Uri ("http://xamarin.com"); headers.ContentMD5 = new byte[] { 3, 5 }; headers.ContentRange = new ContentRangeHeaderValue (88, 444); headers.ContentType = new MediaTypeHeaderValue ("multipart/*"); headers.Expires = new DateTimeOffset (DateTime.Today); headers.LastModified = new DateTimeOffset (DateTime.Today); headers.Add ("allow", "a2"); headers.Add ("content-encoding", "ce3"); headers.Add ("content-language", "cl2"); try { headers.Add ("content-length", "444"); Assert.Fail ("content-length"); } catch (FormatException) { } try { headers.Add ("content-location", "cl2"); Assert.Fail ("content-location"); } catch (FormatException) { } try { headers.Add ("content-MD5", "cmd5"); Assert.Fail ("content-MD5"); } catch (FormatException) { } try { headers.Add ("content-range", "133"); Assert.Fail ("content-range"); } catch (FormatException) { } try { headers.Add ("content-type", "ctype"); Assert.Fail ("content-type"); } catch (FormatException) { } try { headers.Add ("expires", "ctype"); Assert.Fail ("expires"); } catch (FormatException) { } try { headers.Add ("last-modified", "lmo"); Assert.Fail ("last-modified"); } catch (FormatException) { } Assert.IsTrue (headers.Allow.SequenceEqual ( new[] { "a1", "a2" } )); Assert.IsTrue (headers.ContentEncoding.SequenceEqual ( new[] { "ce1", "ce3" } )); Assert.IsTrue (headers.ContentLanguage.SequenceEqual ( new[] { "cl1", "cl2" } )); Assert.AreEqual (23, headers.ContentLength); Assert.AreEqual (new Uri ("http://xamarin.com"), headers.ContentLocation); Assert.AreEqual (new byte[] { 3, 5 }, headers.ContentMD5); Assert.AreEqual (new ContentRangeHeaderValue (88, 444), headers.ContentRange); Assert.AreEqual (new MediaTypeHeaderValue ("multipart/*"), headers.ContentType); Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.Expires); Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.LastModified); }
public void CopyToAsync () { var ms = new MemoryStream (); ms.WriteByte (4); ms.WriteByte (2); ms.Seek (0, SeekOrigin.Begin); var sc = new StreamContent (ms); var dest = new MemoryStream (); var task = sc.CopyToAsync (dest); task.Wait (); Assert.AreEqual (2, dest.Length, "#1"); bool hit = false; dest = new MemoryStream (); var scm = new StreamContentMock (new ExceptionStream ()); scm.OnSerializeToStreamAsync = () => { hit = true; }; task = scm.CopyToAsync (dest); try { task.Wait (); Assert.Fail ("#9"); } catch (AggregateException) { } Assert.IsTrue (hit, "#10"); }