forked from nanoframework/System.Net.Http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteArrayContentTest.cs
More file actions
183 lines (150 loc) · 6.02 KB
/
ByteArrayContentTest.cs
File metadata and controls
183 lines (150 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//
using nanoFramework.TestFramework;
using System;
using System.IO;
using System.Net.Http;
namespace HttpUnitTests
{
[TestClass]
public class ByteArrayContentTest
{
[TestMethod]
public void Ctor_NullSourceArray_ThrowsArgumentNullException()
{
Assert.ThrowsException(typeof(ArgumentNullException), () => new ByteArrayContent(null));
}
[TestMethod]
public void Ctor_NullSourceArrayWithRange_ThrowsArgumentNullException()
{
Assert.ThrowsException(typeof(ArgumentNullException), () => new ByteArrayContent(null, 0, 1));
}
[TestMethod]
public void Ctor_EmptySourceArrayWithRange_ThrowsArgumentOutOfRangeException()
{
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[0], 0, 1));
}
[TestMethod]
public void Ctor_StartIndexTooBig_ThrowsArgumentOufOfRangeException()
{
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[5], 5, 1));
}
[TestMethod]
public void Ctor_StartIndexNegative_ThrowsArgumentOutOfRangeException()
{
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[5], -1, 1));
}
[TestMethod]
public void Ctor_LengthTooBig_ThrowsArgumentOutOfRangeException()
{
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[5], 1, 5));
}
[TestMethod]
public void Ctor_LengthPlusOffsetCauseIntOverflow_ThrowsArgumentOutOfRangeException()
{
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[5], 1, int.MaxValue));
}
[TestMethod]
public void Ctor_LengthNegative_ThrowsArgumentOutOfRangeException()
{
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => new ByteArrayContent(new byte[5], 0, -1));
}
[TestMethod]
public void ContentLength_UseWholeSourceArray_LengthMatchesArrayLength()
{
var contentData = new byte[10];
var content = new ByteArrayContent(contentData);
Assert.AreEqual(contentData.Length, content.Headers.ContentLength);
}
[TestMethod]
public void ContentLength_UsePartialSourceArray_LengthMatchesArrayLength()
{
var contentData = new byte[10];
var content = new ByteArrayContent(contentData, 5, 3);
Assert.AreEqual(3, content.Headers.ContentLength);
}
[TestMethod]
public void ReadAsStreamAsync_EmptySourceArray_Succeed()
{
var content = new ByteArrayContent(new byte[0]);
using Stream stream = content.ReadAsStream();
Assert.AreEqual(0, stream.Length);
}
[TestMethod]
public void ReadAsStream_Call_MemoryStreamWrappingByteArrayReturned()
{
var contentData = new byte[10];
var content = new ByteArrayContent(contentData, 5, 3);
Stream stream = content.ReadAsStream();
Assert.IsFalse(stream.CanWrite);
Assert.AreEqual(3, stream.Length);
}
[TestMethod]
public void CopyTo_NullDestination_ThrowsArgumentNullException()
{
byte[] contentData = CreateSourceArray();
var content = new ByteArrayContent(contentData);
Assert.ThrowsException(typeof(ArgumentNullException),
() =>
{
content.CopyTo(null);
});
}
[TestMethod]
public void CopyTo_UseWholeSourceArray_WholeContentCopied()
{
byte[] contentData = CreateSourceArray();
var content = new ByteArrayContent(contentData);
using var destination = new MemoryStream();
content.CopyTo(destination);
Assert.AreEqual(contentData.Length, destination.Length);
CheckResult(destination, 0);
}
[TestMethod]
public void CopyTo_UsePartialSourceArray_PartialContentCopied()
{
byte[] contentData = CreateSourceArray();
var content = new ByteArrayContent(contentData, 3, 5);
using var destination = new MemoryStream();
content.CopyTo(destination);
Assert.AreEqual(5, destination.Length);
CheckResult(destination, 3);
}
[TestMethod]
public void CopyTo_UseEmptySourceArray_NothingCopied()
{
var contentData = new byte[0];
var content = new ByteArrayContent(contentData, 0, 0);
using var destination = new MemoryStream();
content.CopyTo(destination);
Assert.AreEqual(0, destination.Length);
}
#region Helper methods
private static byte[] CreateSourceArray()
{
var contentData = new byte[10];
for (int i = 0; i < contentData.Length; i++)
{
contentData[i] = (byte)(i % 256);
}
return contentData;
}
private static void CheckResult(Stream destination, byte firstValue)
{
destination.Position = 0;
var destinationData = new byte[destination.Length];
int read = destination.Read(destinationData, 0, destinationData.Length);
Assert.AreEqual(destinationData.Length, read);
Assert.AreEqual(firstValue, destinationData[0]);
for (int i = 1; i < read; i++)
{
Assert.IsTrue((destinationData[i] == (destinationData[i - 1] + 1)) ||
((destinationData[i] == 0) && (destinationData[i - 1] != 0)));
}
}
#endregion
}
}