forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressWriteAsyncResultTest.cs
More file actions
100 lines (84 loc) · 4.36 KB
/
Copy pathProgressWriteAsyncResultTest.cs
File metadata and controls
100 lines (84 loc) · 4.36 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
// 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.Mocks;
using System.Text;
using Microsoft.TestCommon;
using Moq;
namespace System.Net.Http.Handlers
{
public class ProgressWriteAsyncResultTest
{
static readonly byte[] sampleData = Encoding.UTF8.GetBytes("Hello World! Hello World! Hello World! Hello World! Hello World!");
[Fact]
public void Constructor_BeginWriteOnInnerStream()
{
// Arrange
Mock<Stream> mockInnerStream = new Mock<Stream>();
ProgressStream progressStream = ProgressStreamTest.CreateProgressStream();
// Act
IAsyncResult result = new ProgressWriteAsyncResult(
mockInnerStream.Object, progressStream, sampleData, 2, 4, null, null);
// Assert
mockInnerStream.Verify(s => s.BeginWrite(sampleData, 2, 4, It.IsAny<AsyncCallback>(), It.IsAny<object>()),
Times.Once());
}
[Fact]
public void Constructor_CompletesSynchronouslyIfInnerStreamCompletesSynchronously()
{
// Arrange
Mock<Stream> mockInnerStream = new Mock<Stream>();
object userState = new object();
IAsyncResult mockIAsyncResult = MockCompletedAsyncResult.Create(true, userState);
mockInnerStream.Setup(s => s.BeginWrite(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<AsyncCallback>(), It.IsAny<object>()))
.Returns(mockIAsyncResult);
ProgressStream progressStream = ProgressStreamTest.CreateProgressStream();
// Act
IAsyncResult result = new ProgressWriteAsyncResult(
mockInnerStream.Object, progressStream, sampleData, 2, 4, null, userState);
// Assert
Assert.True(result.IsCompleted);
Assert.True(result.CompletedSynchronously);
Assert.Same(userState, result.AsyncState);
}
[Fact]
public void Constructor_CompletesWithExceptionIfInnerStreamThrows()
{
// Arrange
Mock<Stream> mockInnerStream = new Mock<Stream>();
mockInnerStream.Setup(s => s.BeginWrite(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<AsyncCallback>(), It.IsAny<object>()))
.Throws<ApplicationException>();
ProgressStream progressStream = ProgressStreamTest.CreateProgressStream();
// Act
IAsyncResult result = new ProgressWriteAsyncResult(
mockInnerStream.Object, progressStream, sampleData, 2, 2, null, null);
// Assert
Assert.True(result.IsCompleted);
Assert.Throws<ApplicationException>(() => ProgressWriteAsyncResult.End(result));
}
[Theory]
[InlineData(0, 10)]
[InlineData(10, 1)]
public void Constructor_ReportsBytesWritten(int offset, int count)
{
// Arrange
Mock<Stream> mockInnerStream = new Mock<Stream>();
object userState = new object();
IAsyncResult mockIAsyncResult = MockCompletedAsyncResult.Create(true, userState);
mockInnerStream.Setup(s => s.BeginWrite(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<AsyncCallback>(), It.IsAny<object>()))
.Returns(mockIAsyncResult);
MockProgressEventHandler mockProgressHandler;
ProgressMessageHandler progressMessageHandler = MockProgressEventHandler.CreateProgressMessageHandler(out mockProgressHandler, sendProgress: true);
HttpRequestMessage request = new HttpRequestMessage();
ProgressStream progressStream = ProgressStreamTest.CreateProgressStream(progressMessageHandler: progressMessageHandler, request: request);
// Act
IAsyncResult result = new ProgressWriteAsyncResult(
mockInnerStream.Object, progressStream, sampleData, offset, count, null, userState);
// Assert
Assert.True(mockProgressHandler.WasInvoked);
Assert.Same(request, mockProgressHandler.Sender);
Assert.Equal(count, mockProgressHandler.EventArgs.BytesTransferred);
Assert.Same(userState, mockProgressHandler.EventArgs.UserState);
}
}
}