public async Task ContentResult_WritesDataCorrectly_ForDifferentContentSizes(string content, string contentType) { // Arrange var contentResult = new ContentResult { Content = content, ContentType = contentType }; var httpContext = GetHttpContext(); var memoryStream = new MemoryStream(); httpContext.Response.Body = memoryStream; var actionContext = GetActionContext(httpContext); var encoding = MediaTypeHeaderValue.Parse(contentType).Encoding; // Act await contentResult.ExecuteResultAsync(actionContext); // Assert memoryStream.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(memoryStream, encoding); var actualContent = await streamReader.ReadToEndAsync(); Assert.Equal(content, actualContent); }
public async Task ContentResult_ExecuteResultAsync_SetContentTypeAndEncoding_OnResponse( MediaTypeHeaderValue contentType, string content, string responseContentType, string expectedContentType, byte[] expectedContentData) { // Arrange var contentResult = new ContentResult { Content = content, ContentType = contentType?.ToString() }; var httpContext = GetHttpContext(); var memoryStream = new MemoryStream(); httpContext.Response.Body = memoryStream; httpContext.Response.ContentType = responseContentType; var actionContext = GetActionContext(httpContext); // Act await contentResult.ExecuteResultAsync(actionContext); // Assert var finalResponseContentType = httpContext.Response.ContentType; Assert.Equal(expectedContentType, finalResponseContentType); Assert.Equal(expectedContentData, memoryStream.ToArray()); Assert.Equal(expectedContentData.Length, httpContext.Response.ContentLength); }
public async Task ContentResult_DisablesResponseBuffering_IfBufferingFeatureAvailable() { // Arrange var data = "Test Content"; var contentResult = new ContentResult { Content = data, ContentType = new MediaTypeHeaderValue("text/plain") { Encoding = Encoding.ASCII }.ToString() }; var httpContext = GetHttpContext(); httpContext.Features.Set<IHttpBufferingFeature>(new TestBufferingFeature()); var memoryStream = new MemoryStream(); httpContext.Response.Body = memoryStream; var actionContext = GetActionContext(httpContext); // Act await contentResult.ExecuteResultAsync(actionContext); // Assert Assert.Equal("text/plain; charset=us-ascii", httpContext.Response.ContentType); Assert.Equal(Encoding.ASCII.GetString(memoryStream.ToArray()), data); var bufferingFeature = (TestBufferingFeature)httpContext.Features.Get<IHttpBufferingFeature>(); Assert.True(bufferingFeature.DisableResponseBufferingInvoked); }
public async Task ContentResult_DisablesResponseBuffering_IfBufferingFeatureAvailable() { // Arrange var data = "Test Content"; var contentResult = new ContentResult { Content = data, ContentType = new MediaTypeHeaderValue("text/plain") { Encoding = Encoding.ASCII }.ToString() }; var httpContext = GetHttpContext(); httpContext.Features.Set <IHttpBufferingFeature>(new TestBufferingFeature()); var memoryStream = new MemoryStream(); httpContext.Response.Body = memoryStream; var actionContext = GetActionContext(httpContext); // Act await contentResult.ExecuteResultAsync(actionContext); // Assert Assert.Equal("text/plain; charset=us-ascii", httpContext.Response.ContentType); Assert.Equal(Encoding.ASCII.GetString(memoryStream.ToArray()), data); var bufferingFeature = (TestBufferingFeature)httpContext.Features.Get <IHttpBufferingFeature>(); Assert.True(bufferingFeature.DisableResponseBufferingInvoked); }
public async Task ContentResult_ExecuteResultAsync_Response_NullContent_SetsContentTypeAndEncoding() { // Arrange var contentResult = new ContentResult { Content = null, ContentType = new MediaTypeHeaderValue("text/plain") { Encoding = Encoding.Unicode }.ToString() }; var httpContext = GetHttpContext(); var actionContext = GetActionContext(httpContext); // Act await contentResult.ExecuteResultAsync(actionContext); // Assert MediaTypeAssert.Equal("text/plain; charset=utf-16", httpContext.Response.ContentType); }
public async Task ContentResult_Response_NullContent_SetsContentTypeAndEncoding() { // Arrange var contentResult = new ContentResult { Content = null, ContentType = new MediaTypeHeaderValue("text/plain") { Encoding = Encoding.UTF7 }.ToString() }; var httpContext = GetHttpContext(); var actionContext = GetActionContext(httpContext); // Act await contentResult.ExecuteResultAsync(actionContext); // Assert MediaTypeAssert.Equal("text/plain; charset=utf-7", httpContext.Response.ContentType); }
public async Task ContentResult_ExecuteResultAsync_SetContentTypeAndEncoding_OnResponse( MediaTypeHeaderValue contentType, string content, string responseContentType, string expectedContentType, byte[] expectedContentData) { // Arrange var contentResult = new ContentResult { Content = content, ContentType = contentType?.ToString() }; var httpContext = GetHttpContext(); var memoryStream = new MemoryStream(); httpContext.Response.Body = memoryStream; httpContext.Response.ContentType = responseContentType; var actionContext = GetActionContext(httpContext); // Act await contentResult.ExecuteResultAsync(actionContext); // Assert var finalResponseContentType = httpContext.Response.ContentType; Assert.Equal(expectedContentType, finalResponseContentType); Assert.Equal(expectedContentData, memoryStream.ToArray()); }