|
| 1 | +// |
| 2 | +// Copyright (c) .NET Foundation and Contributors |
| 3 | +// Portions Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +// See LICENSE file in the project root for full license information. |
| 5 | +// |
| 6 | + |
| 7 | +using nanoFramework.TestFramework; |
| 8 | +using System; |
| 9 | +using System.IO; |
| 10 | +using System.Net.Http; |
| 11 | +using System.Text; |
| 12 | + |
| 13 | +namespace HttpUnitTests |
| 14 | +{ |
| 15 | + [TestClass] |
| 16 | + public class ByteArrayContentTest |
| 17 | + { |
| 18 | + [TestMethod] |
| 19 | + public void Ctor_NullSourceArray_ThrowsArgumentNullException() |
| 20 | + { |
| 21 | + Assert.Throws(typeof(ArgumentNullException), () => new ByteArrayContent(null)); |
| 22 | + } |
| 23 | + |
| 24 | + [TestMethod] |
| 25 | + public void Ctor_NullSourceArrayWithRange_ThrowsArgumentNullException() |
| 26 | + { |
| 27 | + Assert.Throws(typeof(ArgumentNullException), () => new ByteArrayContent(null, 0, 1)); |
| 28 | + } |
| 29 | + |
| 30 | + [TestMethod] |
| 31 | + public void Ctor_EmptySourceArrayWithRange_ThrowsArgumentOutOfRangeException() |
| 32 | + { |
| 33 | + Assert.Throws(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[0], 0, 1)); |
| 34 | + } |
| 35 | + |
| 36 | + [TestMethod] |
| 37 | + public void Ctor_StartIndexTooBig_ThrowsArgumentOufOfRangeException() |
| 38 | + { |
| 39 | + Assert.Throws(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[5], 5, 1)); |
| 40 | + } |
| 41 | + |
| 42 | + [TestMethod] |
| 43 | + public void Ctor_StartIndexNegative_ThrowsArgumentOutOfRangeException() |
| 44 | + { |
| 45 | + Assert.Throws(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[5], -1, 1)); |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + public void Ctor_LengthTooBig_ThrowsArgumentOutOfRangeException() |
| 50 | + { |
| 51 | + Assert.Throws(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[5], 1, 5)); |
| 52 | + } |
| 53 | + |
| 54 | + [TestMethod] |
| 55 | + public void Ctor_LengthPlusOffsetCauseIntOverflow_ThrowsArgumentOutOfRangeException() |
| 56 | + { |
| 57 | + Assert.Throws(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[5], 1, int.MaxValue)); |
| 58 | + } |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + public void Ctor_LengthNegative_ThrowsArgumentOutOfRangeException() |
| 62 | + { |
| 63 | + Assert.Throws(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[5], 0, -1)); |
| 64 | + } |
| 65 | + |
| 66 | + // TODO need to fix processing of exception |
| 67 | + //[TestMethod] |
| 68 | + //public void ContentLength_UseWholeSourceArray_LengthMatchesArrayLength() |
| 69 | + //{ |
| 70 | + // var contentData = new byte[10]; |
| 71 | + // var content = new ByteArrayContent(contentData); |
| 72 | + |
| 73 | + // Assert.Equal(contentData.Length, content.Headers.ContentLength); |
| 74 | + //} |
| 75 | + |
| 76 | + // TODO need to fix processing of exception |
| 77 | + //[TestMethod] |
| 78 | + //public void ContentLength_UsePartialSourceArray_LengthMatchesArrayLength() |
| 79 | + //{ |
| 80 | + // Assert.SkipTest("Test disabled on API failure"); |
| 81 | + |
| 82 | + // // TODO need to fix edge case in ByteArrayContent |
| 83 | + |
| 84 | + // var contentData = new byte[10]; |
| 85 | + // var content = new ByteArrayContent(contentData, 5, 3); |
| 86 | + |
| 87 | + // Assert.Equal(3, content.Headers.ContentLength); |
| 88 | + //} |
| 89 | + |
| 90 | + [TestMethod] |
| 91 | + public void ReadAsStreamAsync_EmptySourceArray_Succeed() |
| 92 | + { |
| 93 | + var content = new ByteArrayContent(new byte[0]); |
| 94 | + Stream stream = content.ReadAsStream(); |
| 95 | + Assert.Equal(0, stream.Length); |
| 96 | + } |
| 97 | + |
| 98 | + // TODO need to fix processing of exception |
| 99 | + //[TestMethod] |
| 100 | + //public void ReadAsStream_Call_MemoryStreamWrappingByteArrayReturned() |
| 101 | + //{ |
| 102 | + // Assert.SkipTest("Test disabled on API failure"); |
| 103 | + |
| 104 | + // // TODO need to fix edge case in stream reader |
| 105 | + |
| 106 | + // var contentData = new byte[10]; |
| 107 | + // var content = new MockByteArrayContent(contentData, 5, 3); |
| 108 | + |
| 109 | + // Stream stream = content.ReadAsStream(); |
| 110 | + // Assert.False(stream.CanWrite); |
| 111 | + // Assert.Equal(3, stream.Length); |
| 112 | + // Assert.Equal(0, content.CopyToCount); |
| 113 | + //} |
| 114 | + |
| 115 | + // TODO need to fix processing of exception |
| 116 | + //[TestMethod] |
| 117 | + //public void CopyTo_NullDestination_ThrowsArgumentNullException() |
| 118 | + //{ |
| 119 | + // byte[] contentData = CreateSourceArray(); |
| 120 | + // var content = new ByteArrayContent(contentData); |
| 121 | + |
| 122 | + // Assert.Throws(typeof(ArgumentNullException), |
| 123 | + // () => |
| 124 | + // { |
| 125 | + // content.CopyTo(null); |
| 126 | + // }); |
| 127 | + //} |
| 128 | + |
| 129 | + [TestMethod] |
| 130 | + public void CopyTo_UseWholeSourceArray_WholeContentCopied() |
| 131 | + { |
| 132 | + byte[] contentData = CreateSourceArray(); |
| 133 | + var content = new ByteArrayContent(contentData); |
| 134 | + |
| 135 | + var destination = new MemoryStream(); |
| 136 | + content.CopyTo(destination); |
| 137 | + |
| 138 | + Assert.Equal(contentData.Length, destination.Length); |
| 139 | + CheckResult(destination, 0); |
| 140 | + } |
| 141 | + |
| 142 | + [TestMethod] |
| 143 | + public void CopyTo_UsePartialSourceArray_PartialContentCopied() |
| 144 | + { |
| 145 | + byte[] contentData = CreateSourceArray(); |
| 146 | + var content = new ByteArrayContent(contentData, 3, 5); |
| 147 | + |
| 148 | + var destination = new MemoryStream(); |
| 149 | + content.CopyTo(destination); |
| 150 | + |
| 151 | + Assert.Equal(5, destination.Length); |
| 152 | + CheckResult(destination, 3); |
| 153 | + } |
| 154 | + |
| 155 | + // TODO need to fix processing of exception |
| 156 | + //[TestMethod] |
| 157 | + //public void CopyTo_UseEmptySourceArray_NothingCopied() |
| 158 | + //{ |
| 159 | + // var contentData = new byte[0]; |
| 160 | + // var content = new ByteArrayContent(contentData, 0, 0); |
| 161 | + |
| 162 | + // var destination = new MemoryStream(); |
| 163 | + // content.CopyTo(destination); |
| 164 | + |
| 165 | + // Assert.Equal(0, destination.Length); |
| 166 | + //} |
| 167 | + |
| 168 | + #region Helper methods |
| 169 | + |
| 170 | + private static byte[] CreateSourceArray() |
| 171 | + { |
| 172 | + var contentData = new byte[10]; |
| 173 | + for (int i = 0; i < contentData.Length; i++) |
| 174 | + { |
| 175 | + contentData[i] = (byte)(i % 256); |
| 176 | + } |
| 177 | + return contentData; |
| 178 | + } |
| 179 | + |
| 180 | + private static void CheckResult(Stream destination, byte firstValue) |
| 181 | + { |
| 182 | + destination.Position = 0; |
| 183 | + var destinationData = new byte[destination.Length]; |
| 184 | + int read = destination.Read(destinationData, 0, destinationData.Length); |
| 185 | + |
| 186 | + Assert.Equal(destinationData.Length, read); |
| 187 | + Assert.Equal(firstValue, destinationData[0]); |
| 188 | + |
| 189 | + for (int i = 1; i < read; i++) |
| 190 | + { |
| 191 | + Assert.True((destinationData[i] == (destinationData[i - 1] + 1)) || |
| 192 | + ((destinationData[i] == 0) && (destinationData[i - 1] != 0))); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private class MockByteArrayContent : ByteArrayContent |
| 197 | + { |
| 198 | + public int CopyToCount { get; private set; } |
| 199 | + |
| 200 | + public MockByteArrayContent(byte[] content, int offset, int count) |
| 201 | + : base(content, offset, count) |
| 202 | + { |
| 203 | + } |
| 204 | + |
| 205 | + protected override void SerializeToStream(Stream stream) |
| 206 | + { |
| 207 | + CopyToCount++; |
| 208 | + base.CopyTo(stream); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + #endregion |
| 213 | + } |
| 214 | +} |
0 commit comments