88using System ;
99using System . IO ;
1010using System . Net . Http ;
11- using System . Text ;
1211
1312namespace HttpUnitTests
1413{
@@ -18,124 +17,111 @@ public class ByteArrayContentTest
1817 [ TestMethod ]
1918 public void Ctor_NullSourceArray_ThrowsArgumentNullException ( )
2019 {
21- Assert . Throws ( typeof ( ArgumentNullException ) , ( ) => new ByteArrayContent ( null ) ) ;
20+ Assert . ThrowsException ( typeof ( ArgumentNullException ) , ( ) => new ByteArrayContent ( null ) ) ;
2221 }
2322
2423 [ TestMethod ]
2524 public void Ctor_NullSourceArrayWithRange_ThrowsArgumentNullException ( )
2625 {
27- Assert . Throws ( typeof ( ArgumentNullException ) , ( ) => new ByteArrayContent ( null , 0 , 1 ) ) ;
26+ Assert . ThrowsException ( typeof ( ArgumentNullException ) , ( ) => new ByteArrayContent ( null , 0 , 1 ) ) ;
2827 }
2928
3029 [ TestMethod ]
3130 public void Ctor_EmptySourceArrayWithRange_ThrowsArgumentOutOfRangeException ( )
3231 {
33- Assert . Throws ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 0 ] , 0 , 1 ) ) ;
32+ Assert . ThrowsException ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 0 ] , 0 , 1 ) ) ;
3433 }
3534
3635 [ TestMethod ]
3736 public void Ctor_StartIndexTooBig_ThrowsArgumentOufOfRangeException ( )
3837 {
39- Assert . Throws ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 5 , 1 ) ) ;
38+ Assert . ThrowsException ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 5 , 1 ) ) ;
4039 }
4140
4241 [ TestMethod ]
4342 public void Ctor_StartIndexNegative_ThrowsArgumentOutOfRangeException ( )
4443 {
45- Assert . Throws ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , - 1 , 1 ) ) ;
44+ Assert . ThrowsException ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , - 1 , 1 ) ) ;
4645 }
4746
4847 [ TestMethod ]
4948 public void Ctor_LengthTooBig_ThrowsArgumentOutOfRangeException ( )
5049 {
51- Assert . Throws ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 1 , 5 ) ) ;
50+ Assert . ThrowsException ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 1 , 5 ) ) ;
5251 }
5352
5453 [ TestMethod ]
5554 public void Ctor_LengthPlusOffsetCauseIntOverflow_ThrowsArgumentOutOfRangeException ( )
5655 {
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 ) ) ;
5857 }
5958
6059 [ TestMethod ]
6160 public void Ctor_LengthNegative_ThrowsArgumentOutOfRangeException ( )
6261 {
63- Assert . Throws ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 0 , - 1 ) ) ;
62+ Assert . ThrowsException ( typeof ( ArgumentOutOfRangeException ) , ( ) => new ByteArrayContent ( new byte [ 5 ] , 0 , - 1 ) ) ;
6463 }
6564
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 ) ;
8170
82- // // TODO need to fix edge case in ByteArrayContent
71+ Assert . AreEqual ( contentData . Length , content . Headers . ContentLength ) ;
72+ }
8373
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 ) ;
8679
87- // Assert.Equal (3, content.Headers.ContentLength);
88- // }
80+ Assert . AreEqual ( 3 , content . Headers . ContentLength ) ;
81+ }
8982
9083 [ TestMethod ]
9184 public void ReadAsStreamAsync_EmptySourceArray_Succeed ( )
9285 {
9386 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 ) ;
9689 }
9790
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 ) ;
10896
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+ }
114101
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 ) ;
121107
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+ }
128114
129115 [ TestMethod ]
130116 public void CopyTo_UseWholeSourceArray_WholeContentCopied ( )
131117 {
132118 byte [ ] contentData = CreateSourceArray ( ) ;
133119 var content = new ByteArrayContent ( contentData ) ;
134120
135- var destination = new MemoryStream ( ) ;
121+ using var destination = new MemoryStream ( ) ;
136122 content . CopyTo ( destination ) ;
137123
138- Assert . Equal ( contentData . Length , destination . Length ) ;
124+ Assert . AreEqual ( contentData . Length , destination . Length ) ;
139125 CheckResult ( destination , 0 ) ;
140126 }
141127
@@ -145,25 +131,24 @@ public void CopyTo_UsePartialSourceArray_PartialContentCopied()
145131 byte [ ] contentData = CreateSourceArray ( ) ;
146132 var content = new ByteArrayContent ( contentData , 3 , 5 ) ;
147133
148- var destination = new MemoryStream ( ) ;
134+ using var destination = new MemoryStream ( ) ;
149135 content . CopyTo ( destination ) ;
150136
151- Assert . Equal ( 5 , destination . Length ) ;
137+ Assert . AreEqual ( 5 , destination . Length ) ;
152138 CheckResult ( destination , 3 ) ;
153139 }
154140
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 ) ;
161146
162- // var destination = new MemoryStream();
163- // content.CopyTo(destination);
147+ using var destination = new MemoryStream ( ) ;
148+ content . CopyTo ( destination ) ;
164149
165- // Assert.Equal (0, destination.Length);
166- // }
150+ Assert . AreEqual ( 0 , destination . Length ) ;
151+ }
167152
168153 #region Helper methods
169154
@@ -183,32 +168,16 @@ private static void CheckResult(Stream destination, byte firstValue)
183168 var destinationData = new byte [ destination . Length ] ;
184169 int read = destination . Read ( destinationData , 0 , destinationData . Length ) ;
185170
186- Assert . Equal ( destinationData . Length , read ) ;
187- Assert . Equal ( firstValue , destinationData [ 0 ] ) ;
171+ Assert . AreEqual ( destinationData . Length , read ) ;
172+ Assert . AreEqual ( firstValue , destinationData [ 0 ] ) ;
188173
189174 for ( int i = 1 ; i < read ; i ++ )
190175 {
191- Assert . True ( ( destinationData [ i ] == ( destinationData [ i - 1 ] + 1 ) ) ||
176+ Assert . IsTrue ( ( destinationData [ i ] == ( destinationData [ i - 1 ] + 1 ) ) ||
192177 ( ( destinationData [ i ] == 0 ) && ( destinationData [ i - 1 ] != 0 ) ) ) ;
193178 }
194179 }
195180
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-
212181 #endregion
213182 }
214183}
0 commit comments