public async Task DisablesResponseBuffering_IfBufferingFeatureAvailable() { // Arrange var expectedContentType = "text/foo; charset=us-ascii"; var buffer = new byte[] { 1, 2, 3, 4, 5 }; var httpContext = GetHttpContext(); var bufferingFeature = new TestBufferingFeature(); httpContext.Features.Set <IHttpBufferingFeature>(bufferingFeature); var outStream = new MemoryStream(); httpContext.Response.Body = outStream; var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var result = new FileContentResult(buffer, expectedContentType); // Act await result.ExecuteResultAsync(context); // Assert Assert.Equal(buffer, outStream.ToArray()); Assert.Equal(expectedContentType, httpContext.Response.ContentType); Assert.True(bufferingFeature.DisableResponseBufferingInvoked); }
public async Task WriteFileAsync_PreconditionStateShouldProcess_RangeRequestIgnored_WritesRangeRequested_IfRangeProcessingOn(long?start, long?end, string expectedString, long contentLength) { // Arrange var contentType = "text/plain"; var lastModified = new DateTimeOffset(); var entityTag = new EntityTagHeaderValue("\"Etag\""); var byteArray = Encoding.ASCII.GetBytes("Hello World"); var result = new FileContentResult(byteArray, contentType) { LastModified = lastModified, EntityTag = entityTag }; var httpContext = GetHttpContext(); var requestHeaders = httpContext.Request.GetTypedHeaders(); requestHeaders.Range = new RangeHeaderValue(start, end); requestHeaders.IfMatch = new[] { new EntityTagHeaderValue("\"Etag\""), }; httpContext.Request.Method = HttpMethods.Get; httpContext.Response.Body = new MemoryStream(); var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(actionContext); // Assert start = start ?? 11 - end; end = start + contentLength - 1; var httpResponse = actionContext.HttpContext.Response; httpResponse.Body.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; Assert.Equal(lastModified.ToString("R"), httpResponse.Headers[HeaderNames.LastModified]); Assert.Equal(entityTag.ToString(), httpResponse.Headers[HeaderNames.ETag]); if (AppContext.TryGetSwitch(FileResultExecutorBase.EnableRangeProcessingSwitch, out var enableRangeProcessingSwitch) && enableRangeProcessingSwitch) { Assert.Equal(StatusCodes.Status206PartialContent, httpResponse.StatusCode); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); var contentRange = new ContentRangeHeaderValue(start.Value, end.Value, byteArray.Length); Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]); Assert.Equal(contentLength, httpResponse.ContentLength); Assert.Equal(expectedString, body); } else { Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode); Assert.Equal(11, httpResponse.ContentLength); Assert.Equal("Hello World", body); } }
public async Task WriteFileAsync_IfRangeHeaderValid_WritesRangeRequest() { // Arrange var contentType = "text/plain"; var lastModified = DateTimeOffset.MinValue; var entityTag = new EntityTagHeaderValue("\"Etag\""); var byteArray = Encoding.ASCII.GetBytes("Hello World"); var result = new FileContentResult(byteArray, contentType) { LastModified = lastModified, EntityTag = entityTag, EnableRangeProcessing = true, }; var httpContext = GetHttpContext(); var requestHeaders = httpContext.Request.GetTypedHeaders(); requestHeaders.IfMatch = new[] { new EntityTagHeaderValue("\"Etag\""), }; requestHeaders.Range = new RangeHeaderValue(0, 4); requestHeaders.IfRange = new RangeConditionHeaderValue(DateTimeOffset.MinValue); httpContext.Request.Method = HttpMethods.Get; httpContext.Response.Body = new MemoryStream(); var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(actionContext); // Assert var httpResponse = actionContext.HttpContext.Response; httpResponse.Body.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; Assert.Equal(lastModified.ToString("R"), httpResponse.Headers[HeaderNames.LastModified]); Assert.Equal(entityTag.ToString(), httpResponse.Headers[HeaderNames.ETag]); if (result.EnableRangeProcessing) { Assert.Equal(StatusCodes.Status206PartialContent, httpResponse.StatusCode); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); var contentRange = new ContentRangeHeaderValue(0, 4, byteArray.Length); Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]); Assert.Equal(5, httpResponse.ContentLength); Assert.Equal("Hello", body); } else { Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode); Assert.Equal(11, httpResponse.ContentLength); Assert.Equal("Hello World", body); } }
public async Task WriteFileAsync_PreconditionStateUnspecified_RangeRequestedNotSatisfiable(string rangeString) { // Arrange var contentType = "text/plain"; var lastModified = new DateTimeOffset(); var entityTag = new EntityTagHeaderValue("\"Etag\""); var byteArray = Encoding.ASCII.GetBytes("Hello World"); var result = new FileContentResult(byteArray, contentType) { LastModified = lastModified, EntityTag = entityTag }; var httpContext = GetHttpContext(); httpContext.Request.Headers[HeaderNames.Range] = rangeString; httpContext.Request.Method = HttpMethods.Get; httpContext.Response.Body = new MemoryStream(); var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(actionContext); // Assert var httpResponse = actionContext.HttpContext.Response; httpResponse.Body.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; var contentRange = new ContentRangeHeaderValue(byteArray.Length); Assert.Equal(lastModified.ToString("R"), httpResponse.Headers[HeaderNames.LastModified]); Assert.Equal(entityTag.ToString(), httpResponse.Headers[HeaderNames.ETag]); if (AppContext.TryGetSwitch(FileResultExecutorBase.EnableRangeProcessingSwitch, out var enableRangeProcessingSwitch) && enableRangeProcessingSwitch) { Assert.Equal(StatusCodes.Status416RangeNotSatisfiable, httpResponse.StatusCode); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]); Assert.Empty(body); } else { Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode); Assert.Equal("Hello World", body); } }
public async Task WriteFileAsync_NotModified_RangeRequestedIgnored() { // Arrange var contentType = "text/plain"; var lastModified = new DateTimeOffset(); var entityTag = new EntityTagHeaderValue("\"Etag\""); var byteArray = Encoding.ASCII.GetBytes("Hello World"); var result = new FileContentResult(byteArray, contentType) { LastModified = lastModified, EntityTag = entityTag, EnableRangeProcessing = true, }; var httpContext = GetHttpContext(); var requestHeaders = httpContext.Request.GetTypedHeaders(); requestHeaders.IfNoneMatch = new[] { new EntityTagHeaderValue("\"Etag\""), }; httpContext.Request.Headers[HeaderNames.Range] = "bytes = 0-6"; httpContext.Request.Method = HttpMethods.Get; httpContext.Response.Body = new MemoryStream(); var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(actionContext); // Assert var httpResponse = actionContext.HttpContext.Response; httpResponse.Body.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode); Assert.Null(httpResponse.ContentLength); Assert.False(httpResponse.Headers.ContainsKey(HeaderNames.ContentType)); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.Empty(body); }
public async Task WriteFileAsync_IfRangeHeaderInvalid_RangeRequestIgnored() { // Arrange var contentType = "text/plain"; var lastModified = DateTimeOffset.MinValue.AddDays(1); var entityTag = new EntityTagHeaderValue("\"Etag\""); var byteArray = Encoding.ASCII.GetBytes("Hello World"); var result = new FileContentResult(byteArray, contentType) { LastModified = lastModified, EntityTag = entityTag }; var httpContext = GetHttpContext(); var requestHeaders = httpContext.Request.GetTypedHeaders(); requestHeaders.IfMatch = new[] { new EntityTagHeaderValue("\"Etag\""), }; requestHeaders.Range = new RangeHeaderValue(0, 4); requestHeaders.IfRange = new RangeConditionHeaderValue(new EntityTagHeaderValue("\"NotEtag\"")); httpContext.Request.Method = HttpMethods.Get; httpContext.Response.Body = new MemoryStream(); var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(actionContext); // Assert var httpResponse = actionContext.HttpContext.Response; httpResponse.Body.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode); Assert.Equal(lastModified.ToString("R"), httpResponse.Headers[HeaderNames.LastModified]); Assert.Equal(entityTag.ToString(), httpResponse.Headers[HeaderNames.ETag]); Assert.Equal("Hello World", body); }
public async Task WriteFileAsync_CopiesBuffer_ToOutputStream() { // Arrange var buffer = new byte[] { 1, 2, 3, 4, 5 }; var httpContext = GetHttpContext(); var outStream = new MemoryStream(); httpContext.Response.Body = outStream; var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var result = new FileContentResult(buffer, "text/plain"); // Act await result.ExecuteResultAsync(context); // Assert Assert.Equal(buffer, outStream.ToArray()); }
public async Task WriteFileAsync_PreconditionStateUnspecified_RangeRequestIgnored(string rangeString) { // Arrange var contentType = "text/plain"; var lastModified = new DateTimeOffset(); var entityTag = new EntityTagHeaderValue("\"Etag\""); var byteArray = Encoding.ASCII.GetBytes("Hello World"); var result = new FileContentResult(byteArray, contentType) { LastModified = lastModified, EntityTag = entityTag, EnableRangeProcessing = true, }; var httpContext = GetHttpContext(); httpContext.Request.Headers.Range = rangeString; httpContext.Request.Method = HttpMethods.Get; httpContext.Response.Body = new MemoryStream(); var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); // Act await result.ExecuteResultAsync(actionContext); // Assert var httpResponse = actionContext.HttpContext.Response; httpResponse.Body.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; Assert.Empty(httpResponse.Headers.ContentRange); Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode); Assert.Equal(lastModified.ToString("R"), httpResponse.Headers.LastModified); Assert.Equal(entityTag.ToString(), httpResponse.Headers.ETag); Assert.Equal("Hello World", body); }
public async Task ExecuteResultAsync_SetsSuppliedContentTypeAndEncoding() { // Arrange var expectedContentType = "text/foo; charset=us-ascii"; var buffer = new byte[] { 1, 2, 3, 4, 5 }; var httpContext = GetHttpContext(); var outStream = new MemoryStream(); httpContext.Response.Body = outStream; var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var result = new FileContentResult(buffer, expectedContentType); // Act await result.ExecuteResultAsync(context); // Assert Assert.Equal(buffer, outStream.ToArray()); Assert.Equal(expectedContentType, httpContext.Response.ContentType); }
protected override Task ExecuteAsync( HttpContext httpContext, byte[] buffer, string contentType, DateTimeOffset?lastModified = null, EntityTagHeaderValue entityTag = null, bool enableRangeProcessing = false) { var result = new FileContentResult(buffer, contentType) { EntityTag = entityTag, LastModified = lastModified, EnableRangeProcessing = enableRangeProcessing, }; httpContext.RequestServices = new ServiceCollection() .AddSingleton <ILoggerFactory>(NullLoggerFactory.Instance) .AddSingleton <IActionResultExecutor <FileContentResult>, FileContentResultExecutor>() .BuildServiceProvider(); var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); return(result.ExecuteResultAsync(context)); }
public async Task DisablesResponseBuffering_IfBufferingFeatureAvailable() { // Arrange var expectedContentType = "text/foo; charset=us-ascii"; var buffer = new byte[] { 1, 2, 3, 4, 5 }; var httpContext = GetHttpContext(); var bufferingFeature = new TestBufferingFeature(); httpContext.Features.Set<IHttpBufferingFeature>(bufferingFeature); var outStream = new MemoryStream(); httpContext.Response.Body = outStream; var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var result = new FileContentResult(buffer, expectedContentType); // Act await result.ExecuteResultAsync(context); // Assert Assert.Equal(buffer, outStream.ToArray()); Assert.Equal(expectedContentType, httpContext.Response.ContentType); Assert.True(bufferingFeature.DisableResponseBufferingInvoked); }