forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebHostBufferPolicySelectorTest.cs
More file actions
137 lines (116 loc) · 5.35 KB
/
Copy pathWebHostBufferPolicySelectorTest.cs
File metadata and controls
137 lines (116 loc) · 5.35 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
// 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.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Text;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.WebHost
{
public class WebHostBufferPolicySelectorTest
{
private const string testString = "testString";
public static TheoryDataSet<HttpContent, bool> OutputBufferingTestData
{
get
{
Mock<Stream> mockStream = new Mock<Stream>() { CallBase = true };
return new TheoryDataSet<HttpContent, bool>()
{
// Known length HttpContents other than OC should not buffer
{ new StringContent(testString), false },
{ new ByteArrayContent(Encoding.UTF8.GetBytes(testString)), false },
{ new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(testString))), false },
// StreamContent (unknown length) should not buffer
{ new StreamContent(mockStream.Object), false },
// PushStreamContent (unknown length) should not buffer
{ new PushStreamContent((stream, headers, context) => {}), false },
// ObjectContent (unknown length) should buffer
{ new ObjectContent<string>(testString, new XmlMediaTypeFormatter()), true }
};
}
}
public static TheoryDataSet<HttpContent> OutputBufferingTestData_NoExpectedResult
{
get
{
var dataSet = new TheoryDataSet<HttpContent>();
foreach (var item in OutputBufferingTestData)
{
dataSet.Add((HttpContent)item[0]);
}
return dataSet;
}
}
[Fact]
void UseBufferedInputStream_Returns_True()
{
// Arrange
Mock<HttpContextBase> mockContext = new Mock<HttpContextBase>() { CallBase = true };
// Act & Assert
Assert.True(new WebHostBufferPolicySelector().UseBufferedInputStream(mockContext.Object));
}
[Fact]
void UseBufferedInputStream_ThrowsOnNull()
{
WebHostBufferPolicySelector selector = new WebHostBufferPolicySelector();
Assert.ThrowsArgumentNull(() => selector.UseBufferedInputStream(null), "hostContext");
}
[Fact]
void UseBufferedInputStream_Can_Be_Overridden()
{
// Arrange
Mock<WebHostBufferPolicySelector> mockSelector = new Mock<WebHostBufferPolicySelector>();
mockSelector.Setup((w) => w.UseBufferedInputStream(It.IsAny<HttpContextBase>())).Returns(false);
Mock<HttpContextBase> mockContext = new Mock<HttpContextBase>() { CallBase = true };
// Act & Assert
Assert.False(mockSelector.Object.UseBufferedInputStream(mockContext.Object));
}
[Fact]
public void UseBufferedOutputStream_ThrowsOnNull()
{
WebHostBufferPolicySelector selector = new WebHostBufferPolicySelector();
Assert.ThrowsArgumentNull(() => selector.UseBufferedOutputStream(null), "response");
}
[Theory]
[PropertyData("OutputBufferingTestData")]
public void UseBufferedOutputStream_ReturnsCorrectValue(HttpContent content, bool expectedResult)
{
// Arrange
WebHostBufferPolicySelector selector = new WebHostBufferPolicySelector();
HttpResponseMessage response = new HttpResponseMessage();
response.Content = content;
// Act
bool actualResult = selector.UseBufferedOutputStream(response);
// Assert
Assert.Equal(expectedResult, actualResult);
}
[Theory]
[PropertyData("OutputBufferingTestData_NoExpectedResult")]
public void UseBufferedOutputStream_CausesContentLengthHeaderToBeSet(HttpContent content)
{
// Arrange & Act
WebHostBufferPolicySelector selector = new WebHostBufferPolicySelector();
HttpResponseMessage response = new HttpResponseMessage();
response.Content = content;
selector.UseBufferedOutputStream(response);
IEnumerable<string> contentLengthEnumerable;
bool isContentLengthInHeaders = content.Headers.TryGetValues("Content-Length", out contentLengthEnumerable);
string[] contentLengthStrings = isContentLengthInHeaders ? contentLengthEnumerable.ToArray() : new string[0];
long? contentLength = content.Headers.ContentLength;
// Assert
if (contentLength.HasValue && contentLength.Value >= 0)
{
// Setting the header is HttpContentHeader's responsibility, but we assert
// it has happened here because it is UseBufferedOutputStream's responsibility
// to cause that to happen. HttpControllerHandler relies on this.
Assert.True(isContentLengthInHeaders);
Assert.Equal(contentLength.Value, long.Parse(contentLengthStrings[0]));
}
}
}
}