|
1 | 1 | using System.Collections; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.IO; |
| 4 | +using System.Linq; |
3 | 5 | using System.Net; |
4 | 6 | using System.Net.Http; |
| 7 | +using System.Net.Http.Formatting; |
5 | 8 | using System.Net.Http.Headers; |
6 | 9 | using System.Reflection; |
| 10 | +using System.Text; |
7 | 11 | using System.Threading.Tasks; |
| 12 | +using Microsoft.TestCommon; |
8 | 13 | using Moq; |
9 | 14 | using Xunit; |
| 15 | +using Xunit.Extensions; |
10 | 16 | using Assert = Microsoft.TestCommon.AssertEx; |
11 | 17 |
|
12 | 18 | namespace System.Web.Http.WebHost |
@@ -133,5 +139,56 @@ public void SuppressFormsAuthenticationRedirect_RequireSuppressRedirect() { |
133 | 139 | } |
134 | 140 | } |
135 | 141 |
|
| 142 | + [Theory] |
| 143 | + [PropertyData("OutputBufferingTestData")] |
| 144 | + public void IsOutputBufferingNecessary_Returns_Correct_Value(HttpContent content, bool isBuffered) |
| 145 | + { |
| 146 | + // Arrange & Act & Assert |
| 147 | + Assert.Equal(isBuffered, HttpControllerHandler.IsOutputBufferingNecessary(content)); |
| 148 | + } |
| 149 | + |
| 150 | + [Theory] |
| 151 | + [PropertyData("OutputBufferingTestData")] |
| 152 | + public void IsOutputBufferingNecessary_Causes_Content_Length_Header_To_Be_Set(HttpContent content, bool isBuffered) |
| 153 | + { |
| 154 | + // Arrange & Act |
| 155 | + HttpControllerHandler.IsOutputBufferingNecessary(content); |
| 156 | + IEnumerable<string> contentLengthEnumerable; |
| 157 | + bool isContentLengthInHeaders = content.Headers.TryGetValues("Content-Length", out contentLengthEnumerable); |
| 158 | + string[] contentLengthStrings = isContentLengthInHeaders ? contentLengthEnumerable.ToArray() : new string[0]; |
| 159 | + long? contentLength = content.Headers.ContentLength; |
| 160 | + |
| 161 | + // Assert |
| 162 | + if (contentLength.HasValue && contentLength.Value >= 0) |
| 163 | + { |
| 164 | + // Setting the header is HttpContentHeader's responsibility, but we assert |
| 165 | + // it has happened because it is IsOutputBufferingNecessary's responsibility |
| 166 | + // to cause that to happen. HttpControllerHandler relies on this. |
| 167 | + Assert.True(isContentLengthInHeaders); |
| 168 | + Assert.Equal(contentLength.Value, long.Parse(contentLengthStrings[0])); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + public static IEnumerable<object[]> OutputBufferingTestData |
| 173 | + { |
| 174 | + get |
| 175 | + { |
| 176 | + string testString = "testString"; |
| 177 | + Mock<Stream> mockStream = new Mock<Stream>() { CallBase = true }; |
| 178 | + return new TheoryDataSet<HttpContent, bool>() |
| 179 | + { |
| 180 | + // Known length HttpContents other than OC should not buffer |
| 181 | + { new StringContent(testString), false }, |
| 182 | + { new ByteArrayContent(Encoding.UTF8.GetBytes(testString)), false }, |
| 183 | + { new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(testString))), false}, |
| 184 | + |
| 185 | + // StreamContent (unknown length) should not buffer |
| 186 | + { new StreamContent(mockStream.Object), false}, |
| 187 | + |
| 188 | + // ObjectContent (unknown length) should buffer |
| 189 | + { new ObjectContent<string>(testString, new XmlMediaTypeFormatter()), true } |
| 190 | + }; |
| 191 | + } |
| 192 | + } |
136 | 193 | } |
137 | 194 | } |
0 commit comments