Skip to content

Commit 0f8b667

Browse files
authored
Fix and Enhance Uri and HttpContent (#439)
***NO_CI***
1 parent 588dd2a commit 0f8b667

15 files changed

+945
-902
lines changed

Tests/HttpUnitTests/ByteArrayContentTest.cs

Lines changed: 60 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System;
99
using System.IO;
1010
using System.Net.Http;
11-
using System.Text;
1211

1312
namespace 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
}

Tests/HttpUnitTests/HttpContentTest.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,22 @@ public class HttpContentTest
1818
[TestMethod]
1919
public void Dispose_BufferContentThenDisposeContent_BufferedStreamGetsDisposed()
2020
{
21-
Assert.SkipTest("Skipping test because of issue in Test Framework");
22-
23-
// TODO
24-
// test is being reported as failing, despite it's successful, issue lyes in the TestFramework
25-
2621
MockContent content = new MockContent();
2722
content.LoadIntoBuffer();
2823

2924
Type type = typeof(HttpContent);
3025

3126
FieldInfo bufferedContentField = type.GetField("_buffer", BindingFlags.Instance | BindingFlags.NonPublic);
3227

33-
Assert.NotNull(bufferedContentField, "_buffer field shouldn't be null");
28+
Assert.IsNotNull(bufferedContentField, "_buffer field shouldn't be null");
3429

3530
MemoryStream bufferedContentStream = bufferedContentField.GetValue(content) as MemoryStream;
36-
Assert.NotNull(bufferedContentStream, "bufferedContentStream field shouldn't be null");
31+
Assert.IsNotNull(bufferedContentStream, "bufferedContentStream field shouldn't be null");
3732

3833
content.Dispose();
3934

4035
// The following line will throw an ObjectDisposedException if the buffered-stream was correctly disposed.
41-
Assert.Throws(typeof(ObjectDisposedException),
36+
Assert.ThrowsException(typeof(ObjectDisposedException),
4237
() =>
4338
{
4439
_ = bufferedContentStream.Length.ToString();
@@ -65,9 +60,9 @@ public void LoadIntoBuffer_ContentLengthSmallerThanActualData_ActualDataLargerTh
6560

6661
foreach (var testConfig in bufferTests)
6762
{
68-
Assert.True((testConfig.MaxSize >= 1 && testConfig.MaxSize <= (testConfig.NumberOfWrites * testConfig.SizeOfEachWrite) - 1), "Config values out of range.");
63+
Assert.IsTrue((testConfig.MaxSize >= 1 && testConfig.MaxSize <= (testConfig.NumberOfWrites * testConfig.SizeOfEachWrite) - 1), "Config values out of range.");
6964

70-
Assert.Throws(typeof(HttpRequestException),
65+
Assert.ThrowsException(typeof(HttpRequestException),
7166
() =>
7267
{
7368
LieAboutLengthContent c = new(

Tests/HttpUnitTests/HttpUtilityTest.cs

Lines changed: 37 additions & 13 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

@@ -23,18 +20,44 @@ public void UrlDecodeNoThrow()
2320
{
2421
string str = "../../&amp;param2=%CURRREV%";
2522

26-
Assert.Equal(str, HttpUtility.UrlDecode(str));
23+
Assert.AreEqual(str, HttpUtility.UrlDecode(str));
24+
}
25+
26+
[TestMethod]
27+
public void UrlDecodeTest()
28+
{
29+
byte[] bIn;
30+
31+
for (char c = char.MinValue; c < '\uD800'; c++)
32+
{
33+
bIn = Encoding.UTF8.GetBytes(c.ToString());
34+
using MemoryStream encodedValueBytes = new MemoryStream();
35+
36+
// build expected result for UrlEncode
37+
for (int i = 0; i < bIn.Length; i++)
38+
{
39+
UrlEncodeChar((char)bIn[i], encodedValueBytes, false);
40+
}
41+
42+
byte[] bOut = encodedValueBytes.ToArray();
43+
string encodedValue = Encoding.UTF8.GetString(bOut, 0, bOut.Length);
44+
45+
string decodedValue = HttpUtility.UrlDecode(encodedValue);
46+
47+
Assert.AreEqual(c.ToString(), decodedValue,
48+
$"Expecting UrlEncode of '{c}' ({(int)c}) as [{c}] got {decodedValue}");
49+
}
2750
}
2851

2952
[TestMethod]
3053
public void UrlEncodeTest()
3154
{
32-
for (char c = char.MinValue; c < char.MaxValue; c++)
55+
byte[] bIn;
56+
for (char c = char.MinValue; c < '\uD800'; c++)
3357
{
34-
byte[] bIn;
3558
bIn = Encoding.UTF8.GetBytes(c.ToString());
36-
MemoryStream expected = new MemoryStream();
37-
MemoryStream expUnicode = new MemoryStream();
59+
using MemoryStream expected = new MemoryStream();
60+
using MemoryStream expUnicode = new MemoryStream();
3861

3962
// build expected result for UrlEncode
4063
for (int i = 0; i < bIn.Length; i++)
@@ -47,10 +70,11 @@ public void UrlEncodeTest()
4770

4871
byte[] bOut = expected.ToArray();
4972

50-
Assert.Equal(
51-
Encoding.UTF8.GetString(bOut, 0, bOut.Length),
52-
HttpUtility.UrlEncode(c.ToString()),
53-
$"Expecting UrlEncode of '{c}' ({(int)c}) as [{Encoding.UTF8.GetString(bOut, 0, bOut.Length)}] got {HttpUtility.UrlEncode(c.ToString())}");
73+
string expectedResult = Encoding.UTF8.GetString(bOut, 0, bOut.Length);
74+
string actualResult = HttpUtility.UrlEncode(c.ToString());
75+
76+
Assert.AreEqual(expectedResult, actualResult,
77+
$"Expecting UrlEncode of '{c}' ({(int)c}) as [{expectedResult}] got {actualResult}");
5478
}
5579
}
5680

@@ -113,6 +137,6 @@ static void UrlEncodeChar(char c, Stream result, bool isUnicode)
113137
}
114138

115139
static char[] hexChars = "0123456789ABCDEF".ToCharArray();
116-
const string notEncoded = "!()*-._";
140+
const string notEncoded = "~-._";
117141
}
118142
}

0 commit comments

Comments
 (0)