8
8
using System ;
9
9
using System . IO ;
10
10
using System . Net . Http ;
11
- using System . Text ;
12
11
13
12
namespace HttpUnitTests
14
13
{
@@ -18,124 +17,111 @@ public class ByteArrayContentTest
18
17
[ TestMethod ]
19
18
public void Ctor_NullSourceArray_ThrowsArgumentNullException ( )
20
19
{
21
- Assert . Throws ( typeof ( ArgumentNullException ) , ( ) => new ByteArrayContent ( null ) ) ;
20
+ Assert . ThrowsException ( typeof ( ArgumentNullException ) , ( ) => new ByteArrayContent ( null ) ) ;
22
21
}
23
22
24
23
[ TestMethod ]
25
24
public void Ctor_NullSourceArrayWithRange_ThrowsArgumentNullException ( )
26
25
{
27
- Assert . Throws ( typeof ( ArgumentNullException ) , ( ) => new ByteArrayContent ( null , 0 , 1 ) ) ;
26
+ Assert . ThrowsException ( typeof ( ArgumentNullException ) , ( ) => new ByteArrayContent ( null , 0 , 1 ) ) ;
28
27
}
29
28
30
29
[ TestMethod ]
31
30
public void Ctor_EmptySourceArrayWithRange_ThrowsArgumentOutOfRangeException ( )
32
31
{
33
- Assert . Throws ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 0 ] , 0 , 1 ) ) ;
32
+ Assert . ThrowsException ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 0 ] , 0 , 1 ) ) ;
34
33
}
35
34
36
35
[ TestMethod ]
37
36
public void Ctor_StartIndexTooBig_ThrowsArgumentOufOfRangeException ( )
38
37
{
39
- Assert . Throws ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 5 , 1 ) ) ;
38
+ Assert . ThrowsException ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 5 , 1 ) ) ;
40
39
}
41
40
42
41
[ TestMethod ]
43
42
public void Ctor_StartIndexNegative_ThrowsArgumentOutOfRangeException ( )
44
43
{
45
- Assert . Throws ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , - 1 , 1 ) ) ;
44
+ Assert . ThrowsException ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , - 1 , 1 ) ) ;
46
45
}
47
46
48
47
[ TestMethod ]
49
48
public void Ctor_LengthTooBig_ThrowsArgumentOutOfRangeException ( )
50
49
{
51
- Assert . Throws ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 1 , 5 ) ) ;
50
+ Assert . ThrowsException ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 1 , 5 ) ) ;
52
51
}
53
52
54
53
[ TestMethod ]
55
54
public void Ctor_LengthPlusOffsetCauseIntOverflow_ThrowsArgumentOutOfRangeException ( )
56
55
{
57
- Assert . Throws ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 1 , int . MaxValue ) ) ;
56
+ Assert . ThrowsException ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 1 , int . MaxValue ) ) ;
58
57
}
59
58
60
59
[ TestMethod ]
61
60
public void Ctor_LengthNegative_ThrowsArgumentOutOfRangeException ( )
62
61
{
63
- Assert . Throws ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 0 , - 1 ) ) ;
62
+ Assert . ThrowsException ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 0 , - 1 ) ) ;
64
63
}
65
64
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");
65
+ [ TestMethod ]
66
+ public void ContentLength_UseWholeSourceArray_LengthMatchesArrayLength ( )
67
+ {
68
+ var contentData = new byte [ 10 ] ;
69
+ var content = new ByteArrayContent ( contentData ) ;
81
70
82
- // // TODO need to fix edge case in ByteArrayContent
71
+ Assert . AreEqual ( contentData . Length , content . Headers . ContentLength ) ;
72
+ }
83
73
84
- // var contentData = new byte[10];
85
- // var content = new ByteArrayContent(contentData, 5, 3);
74
+ [ TestMethod ]
75
+ public void ContentLength_UsePartialSourceArray_LengthMatchesArrayLength ( )
76
+ {
77
+ var contentData = new byte [ 10 ] ;
78
+ var content = new ByteArrayContent ( contentData , 5 , 3 ) ;
86
79
87
- // Assert.Equal (3, content.Headers.ContentLength);
88
- // }
80
+ Assert . AreEqual ( 3 , content . Headers . ContentLength ) ;
81
+ }
89
82
90
83
[ TestMethod ]
91
84
public void ReadAsStreamAsync_EmptySourceArray_Succeed ( )
92
85
{
93
86
var content = new ByteArrayContent ( new byte [ 0 ] ) ;
94
- Stream stream = content . ReadAsStream ( ) ;
95
- Assert . Equal ( 0 , stream . Length ) ;
87
+ using Stream stream = content . ReadAsStream ( ) ;
88
+ Assert . AreEqual ( 0 , stream . Length ) ;
96
89
}
97
90
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);
91
+ [ TestMethod ]
92
+ public void ReadAsStream_Call_MemoryStreamWrappingByteArrayReturned ( )
93
+ {
94
+ var contentData = new byte [ 10 ] ;
95
+ var content = new ByteArrayContent ( contentData , 5 , 3 ) ;
108
96
109
- // Stream stream = content.ReadAsStream();
110
- // Assert.False(stream.CanWrite);
111
- // Assert.Equal(3, stream.Length);
112
- // Assert.Equal(0, content.CopyToCount);
113
- //}
97
+ Stream stream = content . ReadAsStream ( ) ;
98
+ Assert . IsFalse ( stream . CanWrite ) ;
99
+ Assert . AreEqual ( 3 , stream . Length ) ;
100
+ }
114
101
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);
102
+ [ TestMethod ]
103
+ public void CopyTo_NullDestination_ThrowsArgumentNullException ( )
104
+ {
105
+ byte [ ] contentData = CreateSourceArray ( ) ;
106
+ var content = new ByteArrayContent ( contentData ) ;
121
107
122
- // Assert.Throws (typeof(ArgumentNullException),
123
- // () =>
124
- // {
125
- // content.CopyTo(null);
126
- // });
127
- // }
108
+ Assert . ThrowsException ( typeof ( ArgumentNullException ) ,
109
+ ( ) =>
110
+ {
111
+ content . CopyTo ( null ) ;
112
+ } ) ;
113
+ }
128
114
129
115
[ TestMethod ]
130
116
public void CopyTo_UseWholeSourceArray_WholeContentCopied ( )
131
117
{
132
118
byte [ ] contentData = CreateSourceArray ( ) ;
133
119
var content = new ByteArrayContent ( contentData ) ;
134
120
135
- var destination = new MemoryStream ( ) ;
121
+ using var destination = new MemoryStream ( ) ;
136
122
content . CopyTo ( destination ) ;
137
123
138
- Assert . Equal ( contentData . Length , destination . Length ) ;
124
+ Assert . AreEqual ( contentData . Length , destination . Length ) ;
139
125
CheckResult ( destination , 0 ) ;
140
126
}
141
127
@@ -145,25 +131,24 @@ public void CopyTo_UsePartialSourceArray_PartialContentCopied()
145
131
byte [ ] contentData = CreateSourceArray ( ) ;
146
132
var content = new ByteArrayContent ( contentData , 3 , 5 ) ;
147
133
148
- var destination = new MemoryStream ( ) ;
134
+ using var destination = new MemoryStream ( ) ;
149
135
content . CopyTo ( destination ) ;
150
136
151
- Assert . Equal ( 5 , destination . Length ) ;
137
+ Assert . AreEqual ( 5 , destination . Length ) ;
152
138
CheckResult ( destination , 3 ) ;
153
139
}
154
140
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);
141
+ [ TestMethod ]
142
+ public void CopyTo_UseEmptySourceArray_NothingCopied ( )
143
+ {
144
+ var contentData = new byte [ 0 ] ;
145
+ var content = new ByteArrayContent ( contentData , 0 , 0 ) ;
161
146
162
- // var destination = new MemoryStream();
163
- // content.CopyTo(destination);
147
+ using var destination = new MemoryStream ( ) ;
148
+ content . CopyTo ( destination ) ;
164
149
165
- // Assert.Equal (0, destination.Length);
166
- // }
150
+ Assert . AreEqual ( 0 , destination . Length ) ;
151
+ }
167
152
168
153
#region Helper methods
169
154
@@ -183,32 +168,16 @@ private static void CheckResult(Stream destination, byte firstValue)
183
168
var destinationData = new byte [ destination . Length ] ;
184
169
int read = destination . Read ( destinationData , 0 , destinationData . Length ) ;
185
170
186
- Assert . Equal ( destinationData . Length , read ) ;
187
- Assert . Equal ( firstValue , destinationData [ 0 ] ) ;
171
+ Assert . AreEqual ( destinationData . Length , read ) ;
172
+ Assert . AreEqual ( firstValue , destinationData [ 0 ] ) ;
188
173
189
174
for ( int i = 1 ; i < read ; i ++ )
190
175
{
191
- Assert . True ( ( destinationData [ i ] == ( destinationData [ i - 1 ] + 1 ) ) ||
176
+ Assert . IsTrue ( ( destinationData [ i ] == ( destinationData [ i - 1 ] + 1 ) ) ||
192
177
( ( destinationData [ i ] == 0 ) && ( destinationData [ i - 1 ] != 0 ) ) ) ;
193
178
}
194
179
}
195
180
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
181
#endregion
213
182
}
214
183
}
0 commit comments