forked from nanoframework/System.Net.Http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpContentTest.cs
More file actions
126 lines (106 loc) · 4.39 KB
/
HttpContentTest.cs
File metadata and controls
126 lines (106 loc) · 4.39 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
//
// 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;
using System.Reflection;
namespace HttpUnitTests
{
[TestClass]
public class HttpContentTest
{
[TestMethod]
public void Dispose_BufferContentThenDisposeContent_BufferedStreamGetsDisposed()
{
MockContent content = new MockContent();
content.LoadIntoBuffer();
Type type = typeof(HttpContent);
FieldInfo bufferedContentField = type.GetField("_buffer", BindingFlags.Instance | BindingFlags.NonPublic);
Assert.IsNotNull(bufferedContentField, "_buffer field shouldn't be null");
MemoryStream bufferedContentStream = bufferedContentField.GetValue(content) as MemoryStream;
Assert.IsNotNull(bufferedContentStream, "bufferedContentStream field shouldn't be null");
content.Dispose();
// The following line will throw an ObjectDisposedException if the buffered-stream was correctly disposed.
Assert.ThrowsException(typeof(ObjectDisposedException),
() =>
{
_ = bufferedContentStream.Length.ToString();
});
}
[TestMethod]
public void LoadIntoBuffer_ContentLengthSmallerThanActualData_ActualDataLargerThanMaxSize_ThrowsException()
{
Assert.SkipTest("Skipping test because of missing implementation in HttpContent");
// TODO
// need to implement support for seeakable streams
BufferTestConfig[] bufferTests = new BufferTestConfig[]
{
new BufferTestConfig(1, 100, 99, 1),
new BufferTestConfig(1, 100, 50, 99),
new BufferTestConfig(1, 100, 98, 98),
new BufferTestConfig(1, 100, 99, 99),
new BufferTestConfig(1, 100, 99, 98),
new BufferTestConfig(3, 50, 100, 149),
new BufferTestConfig(3, 50, 149, 149)
};
foreach (var testConfig in bufferTests)
{
Assert.IsTrue((testConfig.MaxSize >= 1 && testConfig.MaxSize <= (testConfig.NumberOfWrites * testConfig.SizeOfEachWrite) - 1), "Config values out of range.");
Assert.ThrowsException(typeof(HttpRequestException),
() =>
{
LieAboutLengthContent c = new(
testConfig.NumberOfWrites,
testConfig.SizeOfEachWrite,
testConfig.ReportedLength);
c.LoadIntoBuffer();
});
}
}
private class BufferTestConfig
{
public int NumberOfWrites { get; set; }
public int SizeOfEachWrite { get; set; }
public int ReportedLength { get; set; }
public int MaxSize { get; set; }
public BufferTestConfig(
int numberOfWrites,
int sizeOfEachWrite,
int reportedLength,
int maxSize1)
{
NumberOfWrites = numberOfWrites;
SizeOfEachWrite = sizeOfEachWrite;
ReportedLength = reportedLength;
MaxSize = maxSize1;
}
}
private sealed class LieAboutLengthContent : HttpContent
{
private readonly int _numberOfWrites, _sizeOfEachWrite, _reportedLength;
public LieAboutLengthContent(int numberOfWrites, int sizeOfEachWrite, int reportedLength)
{
_numberOfWrites = numberOfWrites;
_sizeOfEachWrite = sizeOfEachWrite;
_reportedLength = reportedLength;
}
protected override void SerializeToStream(Stream stream)
{
byte[] bytes = new byte[_sizeOfEachWrite];
for (int i = 0; i < _numberOfWrites; i++)
{
stream.Write(bytes, 0, bytes.Length);
}
}
protected override bool TryComputeLength(out long length)
{
length = _reportedLength;
return true;
}
}
}
}