-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSimulateNetworkStreamTests.cs
More file actions
191 lines (153 loc) · 5.67 KB
/
SimulateNetworkStreamTests.cs
File metadata and controls
191 lines (153 loc) · 5.67 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// ReSharper disable UnusedVariable
[TestFixture]
public class SimulateNetworkStreamTests
{
[Test]
public async Task WithoutSimulateNetworkStream()
{
#region WithoutSimulateNetworkStream
using var client = new MockHttpClient("sample.html");
using var result = await client.GetAsync(
"https://fake/get",
HttpCompletionOption.ResponseHeadersRead);
await using var stream = await result.Content.ReadAsStreamAsync();
// Stream is seekable (MemoryStream-like behavior)
IsTrue(stream.CanSeek);
// Can reset
stream.Position = 0;
#endregion
}
[Test]
public async Task WithSimulateNetworkStream()
{
#region WithSimulateNetworkStream
using var client = new MockHttpClient("sample.html");
client.SimulateNetworkStream = true;
var result = await client.GetAsync(
"https://fake/get",
HttpCompletionOption.ResponseHeadersRead);
var stream = await result.Content.ReadAsStreamAsync();
// Stream is non-seekable (real network stream behavior)
IsFalse(stream.CanSeek);
// stream.Position throws NotSupportedException
// Cannot reset or re-read the stream
#endregion
}
[Test]
[Explicit]
public async Task ResponseHeadersRead_StreamIsNotSeekable()
{
using var client = new MockHttpClient("sample.html");
using var result = await client.GetAsync(
"https://fake/get",
HttpCompletionOption.ResponseHeadersRead);
var content = result.Content;
await using var stream = await content.ReadAsStreamAsync();
Assert.Throws<NotSupportedException>(() => stream.Position = 1);
}
[Test]
[Explicit]
public async Task ResponseHeadersRead_LengthIsNotAvaliable()
{
using var client = new MockHttpClient("sample.html");
using var result = await client.GetAsync(
"https://fake/get",
HttpCompletionOption.ResponseHeadersRead);
var content = result.Content;
await using var stream = await content.ReadAsStreamAsync();
Assert.Throws<NotSupportedException>(() =>
{
// ReSharper disable once UnusedVariable
var x = stream.Length;
});
}
[Test]
public async Task ResponseHeadersRead_WithFile_StreamIsNotSeekable()
{
using var client = new MockHttpClient("sample.html");
client.SimulateNetworkStream = true;
using var result = await client.GetAsync(
"https://fake/get",
HttpCompletionOption.ResponseHeadersRead);
var content = result.Content;
await using var stream = await content.ReadAsStreamAsync();
Assert.Throws<NotSupportedException>(() => stream.Position = 1);
}
[Test]
public async Task ResponseHeadersRead_WithFile_LengthIsNotAvaliable()
{
using var client = new MockHttpClient("sample.html");
client.SimulateNetworkStream = true;
using var result = await client.GetAsync(
"https://fake/get",
HttpCompletionOption.ResponseHeadersRead);
var content = result.Content;
var stream = await content.ReadAsStreamAsync();
Assert.Throws<NotSupportedException>(() =>
{
// ReSharper disable once UnusedVariable
var x = stream.Length;
});
}
#region VerifyWithSimulateNetworkStream
[Test]
public async Task VerifyWithSimulateNetworkStream()
{
using var client = new MockHttpClient("sample.html");
client.SimulateNetworkStream = true;
var response = await client.GetAsync(
"https://fake/api/data",
HttpCompletionOption.ResponseHeadersRead);
await Verify(response);
}
#endregion
[Test]
public async Task ProgressiveReading()
{
#region ProgressiveReading
using var client = new MockHttpClient("sample.html");
client.SimulateNetworkStream = true;
var response = await client.GetAsync(
"https://fake/large-file",
HttpCompletionOption.ResponseHeadersRead);
// Read headers first without loading entire content
response.EnsureSuccessStatusCode();
var contentLength = response.Content.Headers.ContentLength;
// Then read content progressively
await using var stream = await response.Content.ReadAsStreamAsync();
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
// Process chunks as they arrive
ProcessChunk(buffer.AsSpan(0, bytesRead));
}
#endregion
}
// ReSharper disable once UnusedParameter.Local
static void ProcessChunk(Span<byte> span)
{
}
[Test]
public async Task ReadOnceBehavior()
{
#region ReadOnceBehavior
using var client = new MockHttpClient("sample.html");
client.SimulateNetworkStream = true;
using var response = await client.GetAsync(
"https://fake/data",
HttpCompletionOption.ResponseHeadersRead);
await using var stream = await response.Content.ReadAsStreamAsync();
// First read succeeds
using (var reader = new StreamReader(stream))
{
var data1 = await reader.ReadToEndAsync();
}
// Second read returns empty (stream already consumed)
// Throws NotSupportedException
Assert.Throws<NotSupportedException>(() => stream.Position = 0);
// Returns empty
var data2 = await response.Content.ReadAsStringAsync();
#endregion
}
}