forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushStreamContentTest.cs
More file actions
262 lines (225 loc) · 9.8 KB
/
Copy pathPushStreamContentTest.cs
File metadata and controls
262 lines (225 loc) · 9.8 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.TestCommon;
using Moq;
using Moq.Protected;
namespace System.Net.Http
{
public class PushStreamContentTest
{
[Fact]
public void Constructor_ThrowsOnNullAction()
{
Action<Stream, HttpContent, TransportContext> action = null;
Assert.ThrowsArgumentNull(() => new PushStreamContent(action), "onStreamAvailable");
}
[Fact]
public void Constructor_SetsDefaultMediaType()
{
Action<Stream, HttpContent, TransportContext> streamAction = new MockStreamAction().Action;
PushStreamContent content = new PushStreamContent(streamAction);
Assert.Equal(MediaTypeConstants.ApplicationOctetStreamMediaType, content.Headers.ContentType);
}
[Fact]
public void Constructor_SetsMediaTypeFromString()
{
Action<Stream, HttpContent, TransportContext> streamAction = new MockStreamAction().Action;
PushStreamContent content = new PushStreamContent(streamAction, "text/xml");
Assert.Equal(MediaTypeConstants.TextXmlMediaType, content.Headers.ContentType);
}
[Fact]
public void Constructor_SetsMediaType()
{
Action<Stream, HttpContent, TransportContext> streamAction = new MockStreamAction().Action;
PushStreamContent content = new PushStreamContent(streamAction, MediaTypeConstants.TextXmlMediaType);
Assert.Equal(MediaTypeConstants.TextXmlMediaType, content.Headers.ContentType);
}
[Fact]
public async Task SerializeToStreamAsync_CallsAction()
{
// Arrange
MemoryStream outputStream = new MemoryStream();
MockStreamAction streamAction = new MockStreamAction(close: true);
PushStreamContent content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)streamAction.Action);
// Act
await content.CopyToAsync(outputStream);
// Assert
Assert.True(streamAction.WasInvoked);
Assert.Same(content, streamAction.Content);
Assert.IsType<PushStreamContent.CompleteTaskOnCloseStream>(streamAction.OutputStream);
// We don't close the underlying stream
Assert.True(outputStream.CanRead);
}
[Fact]
public async Task SerializeToStreamAsync_CompletesTaskOnActionException()
{
// Arrange
MemoryStream outputStream = new MemoryStream();
MockStreamAction streamAction = new MockStreamAction(throwException: true);
PushStreamContent content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)streamAction.Action);
// Act & Assert
await Assert.ThrowsAsync<ApplicationException>(() => content.CopyToAsync(outputStream));
Assert.True(streamAction.WasInvoked);
Assert.IsType<PushStreamContent.CompleteTaskOnCloseStream>(streamAction.OutputStream);
Assert.True(outputStream.CanRead);
}
#if NETFX_CORE
[Fact]
public async Task CompleteTaskOnCloseStream_Dispose_CompletesTaskButDoNotDisposeInnerStream()
{
// Arrange
Mock<Stream> mockInnerStream = new Mock<Stream>() { CallBase = true };
TaskCompletionSource<bool> serializeToStreamTask = new TaskCompletionSource<bool>();
MockCompleteTaskOnCloseStream mockStream = new MockCompleteTaskOnCloseStream(mockInnerStream.Object, serializeToStreamTask);
// Act
mockStream.Dispose();
// Assert
mockInnerStream.Protected().Verify("Dispose", Times.Never(), true);
Assert.Equal(TaskStatus.RanToCompletion, serializeToStreamTask.Task.Status);
Assert.True(await serializeToStreamTask.Task);
}
#else
[Fact]
public async Task CompleteTaskOnCloseStream_Dispose_CompletesTaskButDoNotCloseInnerStream()
{
// Arrange
Mock<Stream> mockInnerStream = new Mock<Stream>() { CallBase = true };
TaskCompletionSource<bool> serializeToStreamTask = new TaskCompletionSource<bool>();
MockCompleteTaskOnCloseStream mockStream = new MockCompleteTaskOnCloseStream(mockInnerStream.Object, serializeToStreamTask);
// Act
mockStream.Dispose();
// Assert
mockInnerStream.Protected().Verify("Dispose", Times.Never(), true);
mockInnerStream.Verify(s => s.Close(), Times.Never());
Assert.Equal(TaskStatus.RanToCompletion, serializeToStreamTask.Task.Status);
Assert.True(await serializeToStreamTask.Task);
}
[Fact]
public async Task NonClosingDelegatingStream_Close_CompletesTaskButDoNotCloseInnerStream()
{
// Arrange
Mock<Stream> mockInnerStream = new Mock<Stream>() { CallBase = true };
TaskCompletionSource<bool> serializeToStreamTask = new TaskCompletionSource<bool>();
MockCompleteTaskOnCloseStream mockStream = new MockCompleteTaskOnCloseStream(mockInnerStream.Object, serializeToStreamTask);
// Act
mockStream.Close();
// Assert
mockInnerStream.Protected().Verify("Dispose", Times.Never(), true);
mockInnerStream.Verify(s => s.Close(), Times.Never());
Assert.Equal(TaskStatus.RanToCompletion, serializeToStreamTask.Task.Status);
Assert.True(await serializeToStreamTask.Task);
}
#endif
[Fact]
public async Task PushStreamContentWithAsyncOnStreamAvailableHandler_ExceptionsInOnStreamAvailable_AreCaught()
{
// Arrange
bool faulted = false;
Exception exception = new ApplicationException();
PushStreamContent content = new PushStreamContent(async (s, c, tc) =>
{
await Task.FromResult(42);
throw exception;
});
MemoryStream stream = new MemoryStream();
try
{
// Act
await content.CopyToAsync(stream);
}
catch (ApplicationException e)
{
Assert.Same(exception, e);
faulted = true;
}
// Assert
Assert.True(faulted);
}
[Fact]
public async Task PushStream_HttpContentIntegrationTest()
{
// Arrange
var expected = "Hello, world!";
using (var client = new MockHttpClient())
{
// We mock the client, so this doesn't actually hit the web. This client will just echo back
// the body content we give it.
using (var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:30000/"))
{
request.Content = new PushStreamContent((stream, content, context) =>
{
using (var writer = new StreamWriter(stream))
{
writer.Write(expected);
}
}, "text/plain");
// Act
using (var response = await client.SendAsync(request, CancellationToken.None))
{
// Assert
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
Assert.Equal(expected, responseText);
}
}
}
}
private class MockStreamAction
{
bool _close;
bool _throwException;
public MockStreamAction(bool close = false, bool throwException = false)
{
_close = close;
_throwException = throwException;
}
public bool WasInvoked { get; private set; }
public Stream OutputStream { get; private set; }
public HttpContent Content { get; private set; }
public TransportContext TransportContext { get; private set; }
public void Action(Stream stream, HttpContent content, TransportContext context)
{
WasInvoked = true;
OutputStream = stream;
Content = content;
TransportContext = context;
if (_close)
{
#if NETFX_CORE
stream.Dispose();
#else
stream.Close();
#endif
}
if (_throwException)
{
throw new ApplicationException("Action threw exception!");
}
}
}
internal class MockCompleteTaskOnCloseStream : PushStreamContent.CompleteTaskOnCloseStream
{
public MockCompleteTaskOnCloseStream(Stream innerStream, TaskCompletionSource<bool> serializeToStreamTask)
: base(innerStream, serializeToStreamTask)
{
}
}
private class MockHttpClient : HttpClient
{
public override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, Threading.CancellationToken cancellationToken)
{
var stream = new MemoryStream();
await request.Content.CopyToAsync(stream);
stream.Position = 0;
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(stream),
};
}
}
}
}