public async Task ContentDispositionHeader_IsEncodedCorrectly_ForUnicodeCharacters() { // Arrange var httpContext = new DefaultHttpContext(); var actionContext = CreateActionContext(httpContext); var result = new EmptyFileResult("application/my-type") { FileDownloadName = "ABCXYZabcxyz012789!@#$%^&*()-=_+.:~Δ" }; // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.True(result.WasWriteFileCalled); Assert.Equal("application/my-type", httpContext.Response.Headers["Content-Type"]); Assert.Equal(@"attachment; filename=""ABCXYZabcxyz012789!@#$%^&*()-=_+.:~_""; filename*=UTF-8''ABCXYZabcxyz012789!%40#$%25^&%2A%28%29-%3D_+.%3A~%CE%94", httpContext.Response.Headers["Content-Disposition"]); }
public async Task ContentDispositionHeader_IsEncodedCorrectly() { // See comment in FileResult.cs detailing how the FileDownloadName should be encoded. // Arrange var httpContext = new DefaultHttpContext(); var actionContext = CreateActionContext(httpContext); var result = new EmptyFileResult("application/my-type") { FileDownloadName = @"some\file" }; // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.True(result.WasWriteFileCalled); Assert.Equal("application/my-type", httpContext.Response.Headers["Content-Type"]); Assert.Equal(@"attachment; filename=""some\\file""; filename*=UTF-8''some%5Cfile", httpContext.Response.Headers["Content-Disposition"]); }
public async Task ExecuteResultAsync_DoesNotSetContentDisposition_IfNotSpecified() { // Arrange var provider = new ServiceCollection() .AddSingleton <ILoggerFactory>(NullLoggerFactory.Instance) .AddSingleton <EmptyFileResultExecutor>() .BuildServiceProvider(); var httpContext = new DefaultHttpContext(); httpContext.RequestServices = provider; var actionContext = CreateActionContext(httpContext); var result = new EmptyFileResult("application/my-type"); // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.True(result.WasWriteFileCalled); Assert.Equal("application/my-type", httpContext.Response.ContentType); Assert.Equal(Stream.Null, httpContext.Response.Body); }
public async Task ExecuteResultAsync_SetsContentDisposition_IfSpecified() { // Arrange var httpContext = new Mock <HttpContext>(MockBehavior.Strict); httpContext.SetupSet(c => c.Response.ContentType = "application/my-type").Verifiable(); httpContext .Setup(c => c.Response.Headers.Set("Content-Disposition", "attachment; filename=filename.ext")) .Verifiable(); var actionContext = CreateActionContext(httpContext.Object); var result = new EmptyFileResult("application/my-type") { FileDownloadName = "filename.ext" }; // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.True(result.WasWriteFileCalled); httpContext.Verify(); }
public async Task ExecuteResultAsync_SetsContentDisposition_IfSpecified() { // Arrange var httpContext = new DefaultHttpContext(); var actionContext = CreateActionContext(httpContext); var result = new EmptyFileResult("application/my-type") { FileDownloadName = "filename.ext" }; // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.True(result.WasWriteFileCalled); Assert.Equal("application/my-type", httpContext.Response.ContentType); Assert.Equal("attachment; filename=filename.ext; filename*=UTF-8''filename.ext", httpContext.Response.Headers["Content-Disposition"]); }
public async Task ExecuteResultAsync_DoesNotSetContentDisposition_IfNotSpecified() { // Arrange var httpContext = new Mock<HttpContext>(MockBehavior.Strict); httpContext.SetupSet(c => c.Response.ContentType = "application/my-type").Verifiable(); httpContext.Setup(c => c.Response.Body).Returns(Stream.Null); var actionContext = CreateActionContext(httpContext.Object); var result = new EmptyFileResult("application/my-type"); // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.True(result.WasWriteFileCalled); httpContext.Verify(); }
public async Task ExecuteResultAsync_LogsInformation_IfCanResolveLoggerFactory() { // Arrange var httpContext = new DefaultHttpContext(); var services = new ServiceCollection(); var loggerSink = new TestSink(); services.AddSingleton<ILoggerFactory>(new TestLoggerFactory(loggerSink, true)); httpContext.RequestServices = services.BuildServiceProvider(); var actionContext = CreateActionContext(httpContext); var result = new EmptyFileResult("application/my-type"); // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.Equal(1, loggerSink.Writes.Count); }
public async Task ExecuteResultAsync_ThrowsException_IfCannotResolveLoggerFactory() { // Arrange var httpContext = new DefaultHttpContext(); httpContext.RequestServices = new ServiceCollection().BuildServiceProvider(); var actionContext = CreateActionContext(httpContext); var result = new EmptyFileResult("application/my-type"); // Act & Assert await Assert.ThrowsAsync<InvalidOperationException>(() => result.ExecuteResultAsync(actionContext)); }
public async Task ExecuteResultAsync_DoesNotSetContentDisposition_IfNotSpecified() { // Arrange var provider = new ServiceCollection() .AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance) .AddSingleton<EmptyFileResultExecutor>() .BuildServiceProvider(); var httpContext = new DefaultHttpContext(); httpContext.RequestServices = provider; var actionContext = CreateActionContext(httpContext); var result = new EmptyFileResult("application/my-type"); // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.True(result.WasWriteFileCalled); Assert.Equal("application/my-type", httpContext.Response.ContentType); Assert.Equal(Stream.Null, httpContext.Response.Body); }