Skip to content

Commit b1901fb

Browse files
committed
Add ReadOnlyStream wrapper and implement
Added ReadOnlyStream.cs wrapper to allow for making any stream read-only. Implemented ReadOnlyStream in HttpContent, and StreamContent. Added using statements to test streams. Modified and re-nabled ReadAsStream_Call_MemoryStreamWrappingByteArrayReturned now that ReadOnlyStream is available and implemented. Removed unused using statements in HttpUtilityTest and added using statements for streams.
1 parent b74058a commit b1901fb

File tree

8 files changed

+164
-58
lines changed

8 files changed

+164
-58
lines changed

Tests/HttpUnitTests/ByteArrayContentTest.cs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,19 @@ public void ContentLength_UsePartialSourceArray_LengthMatchesArrayLength()
8484
public void ReadAsStreamAsync_EmptySourceArray_Succeed()
8585
{
8686
var content = new ByteArrayContent(new byte[0]);
87-
Stream stream = content.ReadAsStream();
87+
using Stream stream = content.ReadAsStream();
8888
Assert.AreEqual(0, stream.Length);
8989
}
9090

9191
[TestMethod]
9292
public void ReadAsStream_Call_MemoryStreamWrappingByteArrayReturned()
9393
{
94-
Assert.SkipTest("Read only MemoryStream not implemented yet");
95-
96-
// TODO need to fix edge case in stream reader
97-
// (stream reader does not have read only stream properly implemented)
98-
9994
var contentData = new byte[10];
100-
var content = new MockByteArrayContent(contentData, 5, 3);
95+
var content = new ByteArrayContent(contentData, 5, 3);
10196

10297
Stream stream = content.ReadAsStream();
10398
Assert.IsFalse(stream.CanWrite);
10499
Assert.AreEqual(3, stream.Length);
105-
Assert.AreEqual(0, content.CopyToCount);
106100
}
107101

108102
[TestMethod]
@@ -124,7 +118,7 @@ public void CopyTo_UseWholeSourceArray_WholeContentCopied()
124118
byte[] contentData = CreateSourceArray();
125119
var content = new ByteArrayContent(contentData);
126120

127-
var destination = new MemoryStream();
121+
using var destination = new MemoryStream();
128122
content.CopyTo(destination);
129123

130124
Assert.AreEqual(contentData.Length, destination.Length);
@@ -137,7 +131,7 @@ public void CopyTo_UsePartialSourceArray_PartialContentCopied()
137131
byte[] contentData = CreateSourceArray();
138132
var content = new ByteArrayContent(contentData, 3, 5);
139133

140-
var destination = new MemoryStream();
134+
using var destination = new MemoryStream();
141135
content.CopyTo(destination);
142136

143137
Assert.AreEqual(5, destination.Length);
@@ -150,7 +144,7 @@ public void CopyTo_UseEmptySourceArray_NothingCopied()
150144
var contentData = new byte[0];
151145
var content = new ByteArrayContent(contentData, 0, 0);
152146

153-
var destination = new MemoryStream();
147+
using var destination = new MemoryStream();
154148
content.CopyTo(destination);
155149

156150
Assert.AreEqual(0, destination.Length);
@@ -184,22 +178,6 @@ private static void CheckResult(Stream destination, byte firstValue)
184178
}
185179
}
186180

187-
private class MockByteArrayContent : ByteArrayContent
188-
{
189-
public int CopyToCount { get; private set; }
190-
191-
public MockByteArrayContent(byte[] content, int offset, int count)
192-
: base(content, offset, count)
193-
{
194-
}
195-
196-
protected override void SerializeToStream(Stream stream)
197-
{
198-
CopyToCount++;
199-
base.CopyTo(stream);
200-
}
201-
}
202-
203181
#endregion
204182
}
205183
}

Tests/HttpUnitTests/HttpUtilityTest.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
//
66

77
using nanoFramework.TestFramework;
8-
using System;
98
using System.IO;
10-
using System.Net.Http;
11-
using System.Reflection;
129
using System.Text;
1310
using System.Web;
1411

@@ -29,11 +26,12 @@ public void UrlDecodeNoThrow()
2926
[TestMethod]
3027
public void UrlDecodeTest()
3128
{
29+
byte[] bIn;
30+
3231
for (char c = char.MinValue; c < '\uD800'; c++)
3332
{
34-
byte[] bIn;
3533
bIn = Encoding.UTF8.GetBytes(c.ToString());
36-
MemoryStream encodedValueBytes = new MemoryStream();
34+
using MemoryStream encodedValueBytes = new MemoryStream();
3735

3836
// build expected result for UrlEncode
3937
for (int i = 0; i < bIn.Length; i++)
@@ -54,12 +52,12 @@ public void UrlDecodeTest()
5452
[TestMethod]
5553
public void UrlEncodeTest()
5654
{
55+
byte[] bIn;
5756
for (char c = char.MinValue; c < '\uD800'; c++)
5857
{
59-
byte[] bIn;
6058
bIn = Encoding.UTF8.GetBytes(c.ToString());
61-
MemoryStream expected = new MemoryStream();
62-
MemoryStream expUnicode = new MemoryStream();
59+
using MemoryStream expected = new MemoryStream();
60+
using MemoryStream expUnicode = new MemoryStream();
6361

6462
// build expected result for UrlEncode
6563
for (int i = 0; i < bIn.Length; i++)

Tests/HttpUnitTests/StreamContentTest.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ public void CopyTo_CallMultipleTimesWithStreamSupportingSeeking_ContentIsSeriali
8484
var source = new MockStream(new byte[10], true, true);
8585
var content = new StreamContent(source);
8686

87-
var destination1 = new MemoryStream();
87+
using var destination1 = new MemoryStream();
8888
content.CopyTo(destination1);
8989
Assert.AreEqual(source.Length, destination1.Length);
9090

91-
var destination2 = new MemoryStream();
91+
using var destination2 = new MemoryStream();
9292
content.CopyTo(destination2);
9393
Assert.AreEqual(source.Length, destination2.Length);
9494
}
@@ -101,11 +101,11 @@ public void CopyTo_CallMultipleTimesWithStreamSupportingSeekingPartiallyConsumed
101101
source.Read(new byte[consumed], 0, consumed);
102102
var content = new StreamContent(source);
103103

104-
var destination1 = new MemoryStream();
104+
using var destination1 = new MemoryStream();
105105
content.CopyTo(destination1);
106106
Assert.AreEqual(source.Length - consumed, destination1.Length);
107107

108-
var destination2 = new MemoryStream();
108+
using var destination2 = new MemoryStream();
109109
content.CopyTo(destination2);
110110
Assert.AreEqual(source.Length - consumed, destination2.Length);
111111
}
@@ -116,14 +116,14 @@ public void CopyTo_CallMultipleTimesWithStreamNotSupportingSeeking_ThrowsInvalid
116116
var source = new MockStream(new byte[10], false, true); // doesn't support seeking.
117117
var content = new StreamContent(source);
118118

119-
var destination1 = new MemoryStream();
119+
using var destination1 = new MemoryStream();
120120
content.CopyTo(destination1);
121121

122122
// Use hardcoded expected length, since source.Length would throw (source stream gets disposed if non-seekable).
123123
Assert.AreEqual(10, destination1.Length);
124124

125125
// Note that the InvalidOperationException is thrown in CopyToAsync(). It is not thrown inside the task.
126-
var destination2 = new MemoryStream();
126+
using var destination2 = new MemoryStream();
127127
Assert.ThrowsException(typeof(InvalidOperationException),
128128
() =>
129129
{
@@ -141,12 +141,12 @@ public void CopyTo_CallMultipleTimesWithStreamNotSupportingSeekingButBufferedStr
141141
// multiple times, even though the stream doesn't support seeking.
142142
content.LoadIntoBuffer();
143143

144-
var destination1 = new MemoryStream();
144+
using var destination1 = new MemoryStream();
145145
content.CopyTo(destination1);
146146
// Use hardcoded expected length, since source.Length would throw (source stream gets disposed if non-seekable)
147147
Assert.AreEqual(10, destination1.Length);
148148

149-
var destination2 = new MemoryStream();
149+
using var destination2 = new MemoryStream();
150150
content.CopyTo(destination2);
151151
Assert.AreEqual(10, destination2.Length);
152152
}
@@ -163,12 +163,12 @@ public void CopyTo_CallMultipleTimesWithStreamNotSupportingSeekingButBufferedStr
163163
// multiple times, even though the stream doesn't support seeking.
164164
content.LoadIntoBuffer();
165165

166-
var destination1 = new MemoryStream();
166+
using var destination1 = new MemoryStream();
167167
content.CopyTo(destination1);
168168
// Use hardcoded expected length, since source.Length would throw (source stream gets disposed if non-seekable).
169169
Assert.AreEqual(10 - consumed, destination1.Length);
170170

171-
var destination2 = new MemoryStream();
171+
using var destination2 = new MemoryStream();
172172
content.CopyTo(destination2);
173173
Assert.AreEqual(10 - consumed, destination2.Length);
174174
}
@@ -185,7 +185,7 @@ public void CopyTo_NoLoadIntoBuffer_NotBuffered()
185185

186186
var content = new StreamContent(sourceStream);
187187

188-
var destination1 = new MemoryStream();
188+
using var destination1 = new MemoryStream();
189189
content.CopyTo(destination1);
190190

191191
Assert.AreEqual(10, destination1.Length);
@@ -195,7 +195,7 @@ public void CopyTo_NoLoadIntoBuffer_NotBuffered()
195195
var replacedSourceContent = new byte[10] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
196196
Array.Copy(replacedSourceContent, sourceContent, 10);
197197

198-
var destination2 = new MemoryStream();
198+
using var destination2 = new MemoryStream();
199199
content.CopyTo(destination2);
200200
Assert.AreEqual(10, destination2.Length);
201201
CollectionAssert.AreEqual(replacedSourceContent, destination2.ToArray());
@@ -216,7 +216,7 @@ public void CopyTo_LoadIntoBuffer_Buffered()
216216
// buffer so changing the source stream doesn't change the Content
217217
content.LoadIntoBuffer();
218218

219-
var destination1 = new MemoryStream();
219+
using var destination1 = new MemoryStream();
220220
content.CopyTo(destination1);
221221

222222
Assert.AreEqual(10, destination1.Length);
@@ -226,7 +226,7 @@ public void CopyTo_LoadIntoBuffer_Buffered()
226226
var replacedSourceContent = new byte[10] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
227227
Array.Copy(replacedSourceContent, sourceContent, 10);
228228

229-
var destination2 = new MemoryStream();
229+
using var destination2 = new MemoryStream();
230230
content.CopyTo(destination2);
231231
Assert.AreEqual(10, destination2.Length);
232232
CollectionAssert.AreEqual(initialSourceContent, destination2.ToArray());
@@ -238,7 +238,8 @@ public void ContentReadStream_GetProperty_ReturnOriginalStream()
238238
var source = new MockStream(new byte[10]);
239239
var content = new StreamContent(source);
240240

241-
Stream stream = content.ReadAsStream();
241+
using Stream stream = content.ReadAsStream();
242+
242243
Assert.IsFalse(stream.CanWrite);
243244
Assert.AreEqual(source.Length, stream.Length);
244245
Assert.AreEqual(0, source.ReadCount);
@@ -250,9 +251,8 @@ public void ContentReadStream_GetProperty_LoadIntoBuffer_ReturnOriginalStream()
250251
{
251252
var source = new MockStream(new byte[10]);
252253
var content = new StreamContent(source);
253-
content.LoadIntoBuffer();
254254

255-
Stream stream = content.ReadAsStream();
255+
using Stream stream = content.ReadAsStream();
256256
Assert.IsFalse(stream.CanWrite);
257257
Assert.AreEqual(source.Length, stream.Length);
258258
Assert.AreEqual(0, source.ReadCount);
@@ -267,7 +267,7 @@ public void ContentReadStream_GetPropertyPartiallyConsumed_ReturnOriginalStream(
267267
source.Read(new byte[consumed], 0, consumed);
268268
var content = new StreamContent(source);
269269

270-
Stream stream = content.ReadAsStream();
270+
using Stream stream = content.ReadAsStream();
271271
Assert.IsFalse(stream.CanWrite);
272272
Assert.AreEqual(source.Length, stream.Length);
273273
Assert.AreEqual(1, source.ReadCount);
@@ -286,9 +286,8 @@ public void ContentReadStream_CheckResultProperties_ValuesRepresentReadOnlyStrea
286286
}
287287

288288
var source = new MockStream(data);
289-
290289
var content = new StreamContent(source);
291-
Stream contentReadStream = content.ReadAsStream();
290+
using Stream contentReadStream = content.ReadAsStream();
292291

293292
// The following checks verify that the stream returned passes all read-related properties to the
294293
// underlying MockStream and throws when using write-related members.

Tests/HttpUnitTests/StringContentTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void Ctor_EmptyString_Accept()
2727
{
2828
// Consider empty strings like null strings (null and empty strings should be treated equally).
2929
var content = new StringContent(string.Empty);
30-
Stream result = content.ReadAsStream();
30+
using Stream result = content.ReadAsStream();
3131
Assert.AreEqual(0, result.Length);
3232
}
3333

nanoFramework.System.Net.Http/Http/HttpContent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ protected virtual Stream CreateContentReadStream()
221221
LoadIntoBuffer();
222222

223223
_buffer.Seek(0, SeekOrigin.Begin);
224-
return _buffer;
224+
return new ReadOnlyStream(_buffer);
225225
}
226226
}
227227
}

0 commit comments

Comments
 (0)