From 82be0d3d4abd6c52002f27356454ce14096b33cf Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 24 Dec 2014 07:10:20 -0800 Subject: [PATCH] Reacting to FileSystem changes part 3 --- .../AssemblyInfo.cs | 6 + .../StaticFileContext.cs | 2 +- .../StaticFileContextTest.cs | 131 ++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.AspNet.StaticFiles/AssemblyInfo.cs create mode 100644 test/Microsoft.AspNet.StaticFiles.Tests/StaticFileContextTest.cs diff --git a/src/Microsoft.AspNet.StaticFiles/AssemblyInfo.cs b/src/Microsoft.AspNet.StaticFiles/AssemblyInfo.cs new file mode 100644 index 0000000000..5c669e1ed7 --- /dev/null +++ b/src/Microsoft.AspNet.StaticFiles/AssemblyInfo.cs @@ -0,0 +1,6 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Microsoft.AspNet.StaticFiles.Tests")] diff --git a/src/Microsoft.AspNet.StaticFiles/StaticFileContext.cs b/src/Microsoft.AspNet.StaticFiles/StaticFileContext.cs index cb5a40f38b..37326059e5 100644 --- a/src/Microsoft.AspNet.StaticFiles/StaticFileContext.cs +++ b/src/Microsoft.AspNet.StaticFiles/StaticFileContext.cs @@ -58,7 +58,7 @@ namespace Microsoft.AspNet.StaticFiles _contentType = null; _fileInfo = null; _length = 0; - _lastModified = new DateTime(); + _lastModified = new DateTimeOffset(); _etag = null; _etagQuoted = null; _lastModifiedString = null; diff --git a/test/Microsoft.AspNet.StaticFiles.Tests/StaticFileContextTest.cs b/test/Microsoft.AspNet.StaticFiles.Tests/StaticFileContextTest.cs new file mode 100644 index 0000000000..34695b9f57 --- /dev/null +++ b/test/Microsoft.AspNet.StaticFiles.Tests/StaticFileContextTest.cs @@ -0,0 +1,131 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.IO; +using Microsoft.AspNet.FileSystems; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.PipelineCore; +using Microsoft.Framework.Expiration.Interfaces; +using Microsoft.Framework.Logging; +using Xunit; + +namespace Microsoft.AspNet.StaticFiles +{ + public class StaticFileContextTest + { + [Fact] + public void LookupFileInfo_ReturnsFalse_IfFileDoesNotExist() + { + // Arrange + var options = new StaticFileOptions(); + options.FileSystem = new TestFileSystem(); + var context = new StaticFileContext(new DefaultHttpContext(), options, PathString.Empty, NullLogger.Instance); + + // Act + var validateResult = context.ValidatePath(); + var lookupResult = context.LookupFileInfo(); + + // Assert + Assert.True(validateResult); + Assert.False(lookupResult); + } + + [Fact] + public void LookupFileInfo_ReturnsTrue_IfFileExists() + { + // Arrange + var options = new StaticFileOptions(); + var fileSystem = new TestFileSystem(); + fileSystem.AddFile("/foo.txt", new TestFileInfo + { + LastModified = new DateTimeOffset(2014, 1, 2, 3, 4, 5, TimeSpan.Zero) + }); + options.FileSystem = fileSystem; + var pathString = new PathString("/test"); + var httpContext = new DefaultHttpContext(); + httpContext.Request.Path = new PathString("/test/foo.txt"); + var context = new StaticFileContext(httpContext, options, pathString, NullLogger.Instance); + + // Act + context.ValidatePath(); + var result = context.LookupFileInfo(); + + // Assert + Assert.True(result); + } + + private sealed class TestFileSystem : IFileSystem + { + private readonly Dictionary _files = new Dictionary(StringComparer.Ordinal); + + public void AddFile(string path, IFileInfo fileInfo) + { + _files[path] = fileInfo; + } + + public IDirectoryContents GetDirectoryContents(string subpath) + { + return new NotFoundDirectoryContents(); + } + + public IFileInfo GetFileInfo(string subpath) + { + IFileInfo result; + if (_files.TryGetValue(subpath, out result)) + { + return result; + } + + return new NotFoundFileInfo(subpath); + } + + public IExpirationTrigger Watch(string filter) + { + throw new NotSupportedException(); + } + } + + private sealed class TestFileInfo : IFileInfo + { + public bool Exists + { + get { return true; } + } + + public bool IsDirectory + { + get { return false; } + } + + public bool IsReadOnly + { + get { return false; } + } + + public DateTimeOffset LastModified { get; set; } + + public long Length { get; set; } + + public string Name { get; set; } + + public string PhysicalPath { get; set; } + + public Stream CreateReadStream() + { + throw new NotImplementedException(); + } + + public void Delete() + { + throw new NotImplementedException(); + } + + public void WriteContent(byte[] content) + { + throw new NotImplementedException(); + } + } + } +} \ No newline at end of file