Esempio n. 1
0
        public void ContentDispositionHeaderIsEncodedCorrectlyForUnicodeCharacters()
        {
            // Arrange
            Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(
                MockBehavior.Strict
                );

            mockControllerContext
            .SetupSet(c => c.HttpContext.Response.ContentType = "application/my-type")
            .Verifiable();
            mockControllerContext
            .Setup(
                c =>
                c.HttpContext.Response.AddHeader(
                    "Content-Disposition",
                    @"attachment; filename*=UTF-8''ABCXYZabcxyz012789!%40%23$%25%5E&%2A%28%29-%3D_+.:~%CE%94"
                    )
                )
            .Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type")
            {
                FileDownloadName = "ABCXYZabcxyz012789!@#$%^&*()-=_+.:~Δ"
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.True(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
Esempio n. 2
0
        public void ExecuteResultSetsContentDispositionIfSpecified()
        {
            // Arrange
            Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(
                MockBehavior.Strict
                );

            mockControllerContext
            .SetupSet(c => c.HttpContext.Response.ContentType = "application/my-type")
            .Verifiable();
            mockControllerContext
            .Setup(
                c =>
                c.HttpContext.Response.AddHeader(
                    "Content-Disposition",
                    "attachment; filename=filename.ext"
                    )
                )
            .Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type")
            {
                FileDownloadName = "filename.ext"
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.True(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
Esempio n. 3
0
        public void ContentDispositionHeaderIsEncodedCorrectly()
        {
            // See comment in FileResult.cs detailing how the FileDownloadName should be encoded.

            // Arrange
            Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(
                MockBehavior.Strict
                );

            mockControllerContext
            .SetupSet(c => c.HttpContext.Response.ContentType = "application/my-type")
            .Verifiable();
            mockControllerContext
            .Setup(
                c =>
                c.HttpContext.Response.AddHeader(
                    "Content-Disposition",
                    @"attachment; filename=""some\\file"""
                    )
                )
            .Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type")
            {
                FileDownloadName = @"some\file"
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.True(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
Esempio n. 4
0
        public void ConstructorSetsContentTypeProperty() {
            // Act
            FileResult result = new EmptyFileResult("someContentType");

            // Assert
            Assert.AreEqual("someContentType", result.ContentType);
        }
Esempio n. 5
0
        public async Task ContentDispositionHeader_IsEncodedCorrectly_ForUnicodeCharacters()
        {
            // Arrange
            var httpContext = new Mock <HttpContext>();

            httpContext.SetupSet(c => c.Response.ContentType = "application/my-type").Verifiable();
            httpContext
            .Setup(c => c.Response.Headers.Set(
                       "Content-Disposition",
                       @"attachment; filename*=UTF-8''ABCXYZabcxyz012789!%40%23$%25%5E&%2A%28%29-%3D_+.:~%CE%94"))
            .Verifiable();

            var actionContext = CreateActionContext(httpContext.Object);

            var result = new EmptyFileResult("application/my-type")
            {
                FileDownloadName = "ABCXYZabcxyz012789!@#$%^&*()-=_+.:~Δ"
            };

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            Assert.True(result.WasWriteFileCalled);
            httpContext.Verify();
        }
Esempio n. 6
0
        public async Task ContentDispositionHeader_IsEncodedCorrectly()
        {
            // See comment in FileResult.cs detailing how the FileDownloadName should be encoded.

            // Arrange
            var httpContext = new Mock <HttpContext>();

            httpContext.SetupSet(c => c.Response.ContentType = "application/my-type").Verifiable();
            httpContext
            .Setup(c => c.Response.Headers.Set("Content-Disposition", @"attachment; filename=""some\\file"""))
            .Verifiable();

            var actionContext = CreateActionContext(httpContext.Object);

            var result = new EmptyFileResult("application/my-type")
            {
                FileDownloadName = @"some\file"
            };

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            Assert.True(result.WasWriteFileCalled);
            httpContext.Verify();
        }
Esempio n. 7
0
        public void Constructor_SetsContentType()
        {
            // Act
            var result = new EmptyFileResult("text/plain");

            // Assert
            Assert.Equal("text/plain", result.ContentType);
        }
Esempio n. 8
0
        public void FileDownloadNameProperty()
        {
            // Arrange
            FileResult result = new EmptyFileResult();

            // Act & assert
            MemberHelper.TestStringProperty(result, "FileDownloadName", String.Empty, false /* testDefaultValue */, true /* allowNullAndEmpty */);
        }
        public void ConstructorSetsContentTypeProperty()
        {
            // Act
            FileResult result = new EmptyFileResult("someContentType");

            // Assert
            Assert.Equal("someContentType", result.ContentType);
        }
Esempio n. 10
0
        public void FileDownloadNameProperty()
        {
            // Arrange
            FileResult result = new EmptyFileResult();

            // Act & assert
            MemberHelper.TestStringProperty(result, "FileDownloadName", String.Empty);
        }
Esempio n. 11
0
        public void Constructor_SetsContentType()
        {
            // Act
            var result = new EmptyFileResult("text/plain");

            // Assert
            Assert.Equal("text/plain", result.ContentType.ToString());
        }
Esempio n. 12
0
        public void ExecuteResultThrowsIfContextIsNull()
        {
            // Arrange
            FileResult result = new EmptyFileResult();

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { result.ExecuteResult(null); }, "context");
        }
Esempio n. 13
0
 public Task ExecuteAsync(ActionContext context, EmptyFileResult result)
 {
     SetHeadersAndLog(
         context,
         result,
         fileLength: 0L,
         enableRangeProcessing: true,
         lastModified: result.LastModified);
     result.WasWriteFileCalled = true;
     return(Task.FromResult(0));
 }
Esempio n. 14
0
        public void ExecuteResultThrowsIfContextIsNull()
        {
            // Arrange
            FileResult result = new EmptyFileResult();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                result.ExecuteResult(null);
            }, "context");
        }
Esempio n. 15
0
        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));
        }
Esempio n. 16
0
        public async Task SetsAcceptRangeHeader()
        {
            // Arrange
            var httpContext   = GetHttpContext();
            var actionContext = CreateActionContext(httpContext);

            var result = new EmptyFileResult("application/my-type");

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            Assert.Equal("bytes", httpContext.Response.Headers[HeaderNames.AcceptRanges]);
        }
Esempio n. 17
0
        public void ExecuteResultDoesNotSetContentDispositionIfNotSpecified() {
            // Arrange
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, "application/my-type").Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type");

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.IsTrue(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
Esempio n. 18
0
        public void ExecuteResultDoesNotSetContentDispositionIfNotSpecified()
        {
            // Arrange
            Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>();

            mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentType = "application/my-type").Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type");

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.True(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
Esempio n. 19
0
        public void ExecuteResultSetsContentDispositionIfSpecified() {
            // Arrange
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, "application/my-type").Verifiable();
            mockControllerContext.Expect(c => c.HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=filename.ext")).Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type") {
                FileDownloadName = "filename.ext"
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.IsTrue(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
Esempio n. 20
0
        public void ContentDispositionHeaderIsEncodedCorrectlyForUnicodeCharacters() {
            // Arrange
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, "application/my-type").Verifiable();
            mockControllerContext.Expect(c => c.HttpContext.Response.AddHeader("Content-Disposition", @"attachment; filename*=UTF-8''ABCXYZabcxyz012789!%40%23$%25%5E&%2A%28%29-%3D_+.:~%CE%94")).Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type") {
                FileDownloadName = "ABCXYZabcxyz012789!@#$%^&*()-=_+.:~Δ"
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.IsTrue(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
Esempio n. 21
0
        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();
        }
Esempio n. 22
0
        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();
        }
Esempio n. 23
0
        public async Task ExecuteResultAsync_SetsContentDisposition_IfSpecified()
        {
            // Arrange
            var httpContext   = GetHttpContext();
            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"]);
        }
Esempio n. 24
0
        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);
        }
Esempio n. 25
0
        public void ContentDispositionHeaderIsEncodedCorrectly() {
            // See comment in FileResult.cs detailing how the FileDownloadName should be encoded.

            // Arrange
            Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict);
            mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, "application/my-type").Verifiable();
            mockControllerContext.Expect(c => c.HttpContext.Response.AddHeader("Content-Disposition", @"attachment; filename=""some\\file""")).Verifiable();

            EmptyFileResult result = new EmptyFileResult("application/my-type") {
                FileDownloadName = @"some\file"
            };

            // Act
            result.ExecuteResult(mockControllerContext.Object);

            // Assert
            Assert.IsTrue(result.WasWriteFileCalled);
            mockControllerContext.Verify();
        }
Esempio n. 26
0
        public async Task ContentDispositionHeader_IsEncodedCorrectly_ForUnicodeCharacters()
        {
            // Arrange
            var httpContext   = GetHttpContext();
            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"]);
        }
Esempio n. 27
0
        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"]);
        }
Esempio n. 28
0
        public async Task IfUnmodifiedSinceComparison_OnlyUsesWholeSeconds(DateTimeOffset ifUnmodifiedSince, int expectedStatusCode)
        {
            // Arrange
            var httpContext = GetHttpContext();

            httpContext.Request.Headers[HeaderNames.IfUnmodifiedSince] = HeaderUtilities.FormatDate(ifUnmodifiedSince);
            var actionContext = CreateActionContext(httpContext);
            // Represents 4/9/2018 11:24:22 AM +00:00
            // Ticks rounded down to seconds: 636588698620000000
            var ticks  = 636588698625969382;
            var result = new EmptyFileResult("application/test")
            {
                LastModified = new DateTimeOffset(ticks, TimeSpan.Zero)
            };

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            Assert.Equal(expectedStatusCode, httpContext.Response.StatusCode);
        }
Esempio n. 29
0
        public async Task ContentDispositionHeader_IsEncodedCorrectly()
        {
            // See comment in FileResult.cs detailing how the FileDownloadName should be encoded.

            // Arrange
            var httpContext   = GetHttpContext();
            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"]);
        }
Esempio n. 30
0
        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"]);
        }
Esempio n. 31
0
        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();
        }
Esempio n. 32
0
        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);
        }
Esempio n. 33
0
 public Task ExecuteAsync(ActionContext context, EmptyFileResult result)
 {
     SetHeadersAndLog(context, result);
     result.WasWriteFileCalled = true;
     return Task.FromResult(0);
 }
Esempio n. 34
0
        public void FileDownloadNameProperty() {
            // Arrange
            FileResult result = new EmptyFileResult();

            // Act & assert
            MemberHelper.TestStringProperty(result, "FileDownloadName", String.Empty, false /* testDefaultValue */, true /* allowNullAndEmpty */);
        }
Esempio n. 35
0
        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"]);
        }
Esempio n. 36
0
        public void ExecuteResultThrowsIfContextIsNull()
        {
            // Arrange
            FileResult result = new EmptyFileResult();

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { result.ExecuteResult(null); }, "context");
        }
Esempio n. 37
0
        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));
        }
Esempio n. 38
0
        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);
        }
Esempio n. 39
0
 public Task ExecuteAsync(ActionContext context, EmptyFileResult result)
 {
     SetHeadersAndLog(context, result, 0L, true);
     result.WasWriteFileCalled = true;
     return(Task.FromResult(0));
 }
Esempio n. 40
0
        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);
        }
Esempio n. 41
0
        public void FileDownloadNameProperty()
        {
            // Arrange
            FileResult result = new EmptyFileResult();

            // Act & assert
            MemberHelper.TestStringProperty(result, "FileDownloadName", String.Empty);
        }
Esempio n. 42
0
        public void ExecuteResultThrowsIfContextIsNull() {
            // Arrange
            FileResult result = new EmptyFileResult();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    result.ExecuteResult(null);
                }, "context");
        }