diff --git a/src/Microsoft.AspNet.StaticFiles/DefaultFilesExtensions.cs b/src/Microsoft.AspNet.StaticFiles/DefaultFilesExtensions.cs index 2f29be25ac..0ea6c3a8f4 100644 --- a/src/Microsoft.AspNet.StaticFiles/DefaultFilesExtensions.cs +++ b/src/Microsoft.AspNet.StaticFiles/DefaultFilesExtensions.cs @@ -1,7 +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; using Microsoft.AspNet.Http; using Microsoft.AspNet.StaticFiles; @@ -17,7 +16,7 @@ namespace Microsoft.AspNet.Builder /// /// /// - public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder builder) + public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder) { return builder.UseDefaultFiles(new DefaultFilesOptions()); } @@ -28,9 +27,9 @@ namespace Microsoft.AspNet.Builder /// /// The relative request path and physical path. /// - public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder builder, string requestPath) + public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath) { - return UseDefaultFiles(builder, new DefaultFilesOptions() { RequestPath = new PathString(requestPath) }); + return builder.UseDefaultFiles(new DefaultFilesOptions() { RequestPath = new PathString(requestPath) }); } /// @@ -39,14 +38,9 @@ namespace Microsoft.AspNet.Builder /// /// /// - public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder builder, DefaultFilesOptions options) + public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder, [NotNull] DefaultFilesOptions options) { - if (builder == null) - { - throw new ArgumentNullException("builder"); - } - - return builder.Use(next => new DefaultFilesMiddleware(next, options).Invoke); + return builder.UseMiddleware(options); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs b/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs index 96f6198b07..4cf577c429 100644 --- a/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs +++ b/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.FileSystems; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; namespace Microsoft.AspNet.StaticFiles @@ -26,19 +27,11 @@ namespace Microsoft.AspNet.StaticFiles /// /// The next middleware in the pipeline. /// The configuration options for this middleware. - public DefaultFilesMiddleware(RequestDelegate next, DefaultFilesOptions options) + public DefaultFilesMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] DefaultFilesOptions options) { - if (next == null) - { - throw new ArgumentNullException("next"); - } - if (options == null) - { - throw new ArgumentNullException("options"); - } if (options.FileSystem == null) { - options.FileSystem = new PhysicalFileSystem("." + options.RequestPath.Value); + options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath)); } _next = next; diff --git a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserExtensions.cs b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserExtensions.cs index dbd83c0970..fe32f39028 100644 --- a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserExtensions.cs +++ b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserExtensions.cs @@ -1,7 +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; using Microsoft.AspNet.Http; using Microsoft.AspNet.StaticFiles; @@ -17,7 +16,7 @@ namespace Microsoft.AspNet.Builder /// /// /// - public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder builder) + public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder) { return builder.UseDirectoryBrowser(new DirectoryBrowserOptions()); } @@ -28,9 +27,9 @@ namespace Microsoft.AspNet.Builder /// /// The relative request path and physical path. /// - public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder builder, string requestPath) + public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath) { - return UseDirectoryBrowser(builder, new DirectoryBrowserOptions() { RequestPath = new PathString(requestPath) }); + return builder.UseDirectoryBrowser(new DirectoryBrowserOptions() { RequestPath = new PathString(requestPath) }); } /// @@ -39,14 +38,9 @@ namespace Microsoft.AspNet.Builder /// /// /// - public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder builder, DirectoryBrowserOptions options) + public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder, [NotNull] DirectoryBrowserOptions options) { - if (builder == null) - { - throw new ArgumentNullException("builder"); - } - - return builder.Use(next => new DirectoryBrowserMiddleware(next, options).Invoke); + return builder.UseMiddleware(options); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs index ba4b336163..ba61682631 100644 --- a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs +++ b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.FileSystems; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; namespace Microsoft.AspNet.StaticFiles @@ -24,23 +25,15 @@ namespace Microsoft.AspNet.StaticFiles /// /// The next middleware in the pipeline. /// The configuration for this middleware. - public DirectoryBrowserMiddleware(RequestDelegate next, DirectoryBrowserOptions options) + public DirectoryBrowserMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] DirectoryBrowserOptions options) { - if (next == null) - { - throw new ArgumentNullException("next"); - } - if (options == null) - { - throw new ArgumentNullException("options"); - } if (options.Formatter == null) { throw new ArgumentException(Resources.Args_NoFormatter); } if (options.FileSystem == null) { - options.FileSystem = new PhysicalFileSystem("." + options.RequestPath.Value); + options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath)); } _next = next; diff --git a/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs b/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs index d7fe812218..9de67fc531 100644 --- a/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs +++ b/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs @@ -18,9 +18,9 @@ namespace Microsoft.AspNet.Builder /// /// /// - public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder) + public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder) { - return UseFileServer(builder, new FileServerOptions()); + return builder.UseFileServer(new FileServerOptions()); } /// @@ -29,9 +29,9 @@ namespace Microsoft.AspNet.Builder /// /// Should directory browsing be enabled? /// - public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder, bool enableDirectoryBrowsing) + public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, bool enableDirectoryBrowsing) { - return UseFileServer(builder, new FileServerOptions() { EnableDirectoryBrowsing = enableDirectoryBrowsing }); + return builder.UseFileServer(new FileServerOptions() { EnableDirectoryBrowsing = enableDirectoryBrowsing }); } /// @@ -40,9 +40,9 @@ namespace Microsoft.AspNet.Builder /// /// The relative request path and physical path. /// - public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder, string requestPath) + public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath) { - return UseFileServer(builder, new FileServerOptions() { RequestPath = new PathString(requestPath) }); + return builder.UseFileServer(new FileServerOptions() { RequestPath = new PathString(requestPath) }); } /// @@ -51,7 +51,7 @@ namespace Microsoft.AspNet.Builder /// /// /// - public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder, FileServerOptions options) + public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] FileServerOptions options) { if (options == null) { diff --git a/src/Microsoft.AspNet.StaticFiles/Helpers.cs b/src/Microsoft.AspNet.StaticFiles/Helpers.cs index eeb867b07d..efee663776 100644 --- a/src/Microsoft.AspNet.StaticFiles/Helpers.cs +++ b/src/Microsoft.AspNet.StaticFiles/Helpers.cs @@ -3,6 +3,7 @@ using System; using System.Globalization; +using System.IO; using Microsoft.AspNet.Http; namespace Microsoft.AspNet.StaticFiles @@ -49,5 +50,10 @@ namespace Microsoft.AspNet.StaticFiles { return DateTime.TryParseExact(dateString, Constants.HttpDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate); } + + internal static string ResolveRootPath(string webRoot, PathString path) + { + return Path.GetFullPath(Path.Combine(webRoot, path.Value ?? string.Empty)); + } } } diff --git a/src/Microsoft.AspNet.StaticFiles/IHostingEnvironment.cs b/src/Microsoft.AspNet.StaticFiles/IHostingEnvironment.cs new file mode 100644 index 0000000000..d510d0ba23 --- /dev/null +++ b/src/Microsoft.AspNet.StaticFiles/IHostingEnvironment.cs @@ -0,0 +1,15 @@ +// 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 Microsoft.Framework.Runtime; + +namespace Microsoft.AspNet.Hosting +{ + [AssemblyNeutral] + public interface IHostingEnvironment + { + string EnvironmentName { get; } + + string WebRoot { get; } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.StaticFiles/NotNullAttribute.cs b/src/Microsoft.AspNet.StaticFiles/NotNullAttribute.cs new file mode 100644 index 0000000000..bc879f17b6 --- /dev/null +++ b/src/Microsoft.AspNet.StaticFiles/NotNullAttribute.cs @@ -0,0 +1,12 @@ +// 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; + +namespace Microsoft.AspNet.StaticFiles +{ + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] + internal sealed class NotNullAttribute : Attribute + { + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.StaticFiles/SendFileExtensions.cs b/src/Microsoft.AspNet.StaticFiles/SendFileExtensions.cs index 2248ea2053..5ec72f4c68 100644 --- a/src/Microsoft.AspNet.StaticFiles/SendFileExtensions.cs +++ b/src/Microsoft.AspNet.StaticFiles/SendFileExtensions.cs @@ -17,13 +17,8 @@ namespace Microsoft.AspNet.StaticFiles /// /// /// - public static IApplicationBuilder UseSendFileFallback(this IApplicationBuilder builder) + public static IApplicationBuilder UseSendFileFallback([NotNull] this IApplicationBuilder builder) { - if (builder == null) - { - throw new ArgumentNullException("builder"); - } - /* TODO builder.GetItem(typeof(ISendFile)) // Check for advertised support @@ -35,7 +30,7 @@ namespace Microsoft.AspNet.StaticFiles // Otherwise, insert a fallback SendFile middleware and advertise support SetSendFileCapability(builder.Properties); */ - return builder.Use(next => new SendFileMiddleware(next).Invoke); + return builder.UseMiddleware(); } private static bool IsSendFileSupported(IDictionary properties) diff --git a/src/Microsoft.AspNet.StaticFiles/SendFileMiddleware.cs b/src/Microsoft.AspNet.StaticFiles/SendFileMiddleware.cs index 92b15fc92b..8351dcd3a3 100644 --- a/src/Microsoft.AspNet.StaticFiles/SendFileMiddleware.cs +++ b/src/Microsoft.AspNet.StaticFiles/SendFileMiddleware.cs @@ -25,13 +25,8 @@ namespace Microsoft.AspNet.StaticFiles /// Creates a new instance of the SendFileMiddleware. /// /// The next middleware in the pipeline. - public SendFileMiddleware(RequestDelegate next) + public SendFileMiddleware([NotNull] RequestDelegate next) { - if (next == null) - { - throw new ArgumentNullException("next"); - } - _next = next; } diff --git a/src/Microsoft.AspNet.StaticFiles/SendFileResponseExtensions.cs b/src/Microsoft.AspNet.StaticFiles/SendFileResponseExtensions.cs index 41edeae38b..8207505b4a 100644 --- a/src/Microsoft.AspNet.StaticFiles/SendFileResponseExtensions.cs +++ b/src/Microsoft.AspNet.StaticFiles/SendFileResponseExtensions.cs @@ -19,12 +19,8 @@ namespace Microsoft.AspNet.StaticFiles /// /// /// True if sendfile.SendAsync is defined in the environment. - public static bool SupportsSendFile(this HttpResponse response) + public static bool SupportsSendFile([NotNull] this HttpResponse response) { - if (response == null) - { - throw new ArgumentNullException("response"); - } return response.HttpContext.GetFeature() != null; } @@ -34,12 +30,8 @@ namespace Microsoft.AspNet.StaticFiles /// /// /// - public static Task SendFileAsync(this HttpResponse response, string fileName) + public static Task SendFileAsync([NotNull] this HttpResponse response, [NotNull] string fileName) { - if (response == null) - { - throw new ArgumentNullException("response"); - } return response.SendFileAsync(fileName, 0, null, CancellationToken.None); } @@ -52,12 +44,8 @@ namespace Microsoft.AspNet.StaticFiles /// The number of types to send, or null to send the remainder of the file. /// /// - public static Task SendFileAsync(this HttpResponse response, string fileName, long offset, long? count, CancellationToken cancellationToken) + public static Task SendFileAsync([NotNull] this HttpResponse response, [NotNull] string fileName, long offset, long? count, CancellationToken cancellationToken) { - if (response == null) - { - throw new ArgumentNullException("response"); - } var sendFile = response.HttpContext.GetFeature(); if (sendFile == null) { diff --git a/src/Microsoft.AspNet.StaticFiles/StaticFileExtensions.cs b/src/Microsoft.AspNet.StaticFiles/StaticFileExtensions.cs index a514870f08..0a95e1f36e 100644 --- a/src/Microsoft.AspNet.StaticFiles/StaticFileExtensions.cs +++ b/src/Microsoft.AspNet.StaticFiles/StaticFileExtensions.cs @@ -1,7 +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; using Microsoft.AspNet.Http; using Microsoft.AspNet.StaticFiles; @@ -17,9 +16,9 @@ namespace Microsoft.AspNet.Builder /// /// /// - public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder builder) + public static IApplicationBuilder UseStaticFiles([NotNull] this IApplicationBuilder builder) { - return UseStaticFiles(builder, new StaticFileOptions()); + return builder.UseStaticFiles(new StaticFileOptions()); } /// @@ -28,9 +27,9 @@ namespace Microsoft.AspNet.Builder /// /// The relative request path and physical path. /// - public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder builder, string requestPath) + public static IApplicationBuilder UseStaticFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath) { - return UseStaticFiles(builder, new StaticFileOptions() { RequestPath = new PathString(requestPath) }); + return builder.UseStaticFiles(new StaticFileOptions() { RequestPath = new PathString(requestPath) }); } /// @@ -39,13 +38,9 @@ namespace Microsoft.AspNet.Builder /// /// /// - public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder builder, StaticFileOptions options) + public static IApplicationBuilder UseStaticFiles([NotNull] this IApplicationBuilder builder, [NotNull] StaticFileOptions options) { - if (builder == null) - { - throw new ArgumentNullException("builder"); - } - return builder.Use(next => new StaticFileMiddleware(next, options).Invoke); + return builder.UseMiddleware(options); } } } diff --git a/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs b/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs index 7a9a3e8967..e55ae49e6a 100644 --- a/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs +++ b/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs @@ -5,6 +5,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.FileSystems; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; namespace Microsoft.AspNet.StaticFiles @@ -23,23 +24,15 @@ namespace Microsoft.AspNet.StaticFiles /// /// The next middleware in the pipeline. /// The configuration options. - public StaticFileMiddleware(RequestDelegate next, StaticFileOptions options) + public StaticFileMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] StaticFileOptions options) { - if (next == null) - { - throw new ArgumentNullException("next"); - } - if (options == null) - { - throw new ArgumentNullException("options"); - } if (options.ContentTypeProvider == null) { throw new ArgumentException(Resources.Args_NoContentTypeProvider); } if (options.FileSystem == null) { - options.FileSystem = new PhysicalFileSystem("." + options.RequestPath.Value); + options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath)); } _next = next; diff --git a/src/Microsoft.AspNet.StaticFiles/project.json b/src/Microsoft.AspNet.StaticFiles/project.json index 5ad8ca201e..be56a85d13 100644 --- a/src/Microsoft.AspNet.StaticFiles/project.json +++ b/src/Microsoft.AspNet.StaticFiles/project.json @@ -3,7 +3,8 @@ "dependencies": { "Microsoft.AspNet.FileSystems": "1.0.0-*", "Microsoft.AspNet.Http": "1.0.0-*", - "Microsoft.AspNet.HttpFeature": "1.0.0-*" + "Microsoft.AspNet.HttpFeature": "1.0.0-*", + "Microsoft.AspNet.RequestContainer": "1.0.0-*" }, "frameworks": { "aspnet50": {}, diff --git a/test/Microsoft.AspNet.StaticFiles.Tests/DefaultFilesMiddlewareTests.cs b/test/Microsoft.AspNet.StaticFiles.Tests/DefaultFilesMiddlewareTests.cs index 5cd549d8ba..bc41dfbfc3 100644 --- a/test/Microsoft.AspNet.StaticFiles.Tests/DefaultFilesMiddlewareTests.cs +++ b/test/Microsoft.AspNet.StaticFiles.Tests/DefaultFilesMiddlewareTests.cs @@ -20,8 +20,6 @@ namespace Microsoft.AspNet.StaticFiles [Fact] public async Task NullArguments() { - Assert.Throws(() => TestServer.Create(app => app.UseDefaultFiles((DefaultFilesOptions)null))); - // No exception, default provided TestServer.Create(app => app.UseDefaultFiles(new DefaultFilesOptions() { FileSystem = null })); @@ -32,11 +30,11 @@ namespace Microsoft.AspNet.StaticFiles } [Theory] - [InlineData("", @"", "/missing.dir")] + [InlineData("", @".", "/missing.dir")] [InlineData("", @".", "/missing.dir/")] [InlineData("/subdir", @".", "/subdir/missing.dir")] - [InlineData("/subdir", @"", "/subdir/missing.dir/")] - [InlineData("", @"\", "/missing.dir")] + [InlineData("/subdir", @".", "/subdir/missing.dir/")] + [InlineData("", @".\", "/missing.dir")] public async Task NoMatch_PassesThrough(string baseUrl, string baseDir, string requestUrl) { TestServer server = TestServer.Create(app => @@ -44,7 +42,7 @@ namespace Microsoft.AspNet.StaticFiles app.UseDefaultFiles(new DefaultFilesOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) }); app.Run(context => context.Response.WriteAsync(context.Request.Path.Value)); }); @@ -55,10 +53,8 @@ namespace Microsoft.AspNet.StaticFiles } [Theory] - [InlineData("", @"", "/SubFolder/")] [InlineData("", @".", "/SubFolder/")] [InlineData("", @".\", "/SubFolder/")] - [InlineData("", @"SubFolder", "/")] [InlineData("", @".\SubFolder", "/")] public async Task FoundDirectoryWithDefaultFile_PathModified(string baseUrl, string baseDir, string requestUrl) { @@ -67,7 +63,7 @@ namespace Microsoft.AspNet.StaticFiles app.UseDefaultFiles(new DefaultFilesOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) }); app.Run(context => context.Response.WriteAsync(context.Request.Path.Value)); }); @@ -78,18 +74,15 @@ namespace Microsoft.AspNet.StaticFiles } [Theory] - [InlineData("", @"", "/SubFolder", "")] [InlineData("", @".", "/SubFolder", "")] [InlineData("", @".\", "/SubFolder", "")] [InlineData("", @".\", "/SubFolder", "?a=b")] - [InlineData("", @".\", "/SubFolder", "?a=b")] - [InlineData("", @".\", "/SubFolder", "?a=b")] public async Task NearMatch_RedirectAddSlash(string baseUrl, string baseDir, string requestUrl, string queryString) { TestServer server = TestServer.Create(app => app.UseDefaultFiles(new DefaultFilesOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) })); HttpResponseMessage response = await server.CreateRequest(requestUrl + queryString).GetAsync(); @@ -99,8 +92,8 @@ namespace Microsoft.AspNet.StaticFiles } [Theory] - [InlineData("/SubFolder", @"\", "/SubFolder/")] - [InlineData("/SubFolder", @"", "/somedir/")] + [InlineData("/SubFolder", @".\", "/SubFolder/")] + [InlineData("/SubFolder", @".", "/somedir/")] [InlineData("", @".\SubFolder", "/")] [InlineData("", @".\SubFolder\", "/")] public async Task PostDirectory_PassesThrough(string baseUrl, string baseDir, string requestUrl) @@ -108,7 +101,7 @@ namespace Microsoft.AspNet.StaticFiles TestServer server = TestServer.Create(app => app.UseDefaultFiles(new DefaultFilesOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) })); HttpResponseMessage response = await server.CreateRequest(requestUrl).GetAsync(); diff --git a/test/Microsoft.AspNet.StaticFiles.Tests/DirectoryBrowserMiddlewareTests.cs b/test/Microsoft.AspNet.StaticFiles.Tests/DirectoryBrowserMiddlewareTests.cs index 77042807ab..4f6888be41 100644 --- a/test/Microsoft.AspNet.StaticFiles.Tests/DirectoryBrowserMiddlewareTests.cs +++ b/test/Microsoft.AspNet.StaticFiles.Tests/DirectoryBrowserMiddlewareTests.cs @@ -1,8 +1,10 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. using System; +using System.IO; using System.Net; using System.Net.Http; +using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.FileSystems; @@ -17,8 +19,7 @@ namespace Microsoft.AspNet.StaticFiles [Fact] public async Task NullArguments() { - Assert.Throws(() => TestServer.Create(app => app.UseDirectoryBrowser((DirectoryBrowserOptions)null))); - Assert.Throws(() => TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions() { Formatter = null }))); + Assert.Throws(() => TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions() { Formatter = null }))); // No exception, default provided TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions() { FileSystem = null })); @@ -30,36 +31,34 @@ namespace Microsoft.AspNet.StaticFiles } [Theory] - [InlineData("", @"", "/missing.dir")] + [InlineData("", @".", "/missing.dir")] [InlineData("", @".", "/missing.dir/")] [InlineData("/subdir", @".", "/subdir/missing.dir")] - [InlineData("/subdir", @"", "/subdir/missing.dir/")] - [InlineData("", @"\", "/missing.dir")] + [InlineData("/subdir", @".", "/subdir/missing.dir/")] + [InlineData("", @".\", "/missing.dir")] public async Task NoMatch_PassesThrough(string baseUrl, string baseDir, string requestUrl) { TestServer server = TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) })); HttpResponseMessage response = await server.CreateRequest(requestUrl).GetAsync(); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); } [Theory] - [InlineData("", @"", "/")] [InlineData("", @".", "/")] - [InlineData("", @"", "/SubFolder/")] [InlineData("", @".", "/SubFolder/")] - [InlineData("/somedir", @"", "/somedir/")] - [InlineData("/somedir", @"\", "/somedir/")] + [InlineData("/somedir", @".", "/somedir/")] + [InlineData("/somedir", @".\", "/somedir/")] [InlineData("/somedir", @".", "/somedir/subfolder/")] public async Task FoundDirectory_Served(string baseUrl, string baseDir, string requestUrl) { TestServer server = TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) })); HttpResponseMessage response = await server.CreateRequest(requestUrl).GetAsync(); @@ -70,20 +69,18 @@ namespace Microsoft.AspNet.StaticFiles } [Theory] - [InlineData("", @"", "/SubFolder", "")] [InlineData("", @".", "/SubFolder", "")] - [InlineData("/somedir", @"", "/somedir", "")] + [InlineData("/somedir", @".", "/somedir", "")] [InlineData("/somedir", @".", "/somedir/subfolder", "")] - [InlineData("", @"", "/SubFolder", "?a=b")] [InlineData("", @".", "/SubFolder", "?a=b")] - [InlineData("/somedir", @"", "/somedir", "?a=b")] + [InlineData("/somedir", @".", "/somedir", "?a=b")] [InlineData("/somedir", @".", "/somedir/subfolder", "?a=b")] public async Task NearMatch_RedirectAddSlash(string baseUrl, string baseDir, string requestUrl, string queryString) { TestServer server = TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) })); HttpResponseMessage response = await server.CreateRequest(requestUrl + queryString).GetAsync(); @@ -93,36 +90,32 @@ namespace Microsoft.AspNet.StaticFiles } [Theory] - [InlineData("", @"", "/")] [InlineData("", @".", "/")] - [InlineData("", @"", "/SubFolder/")] [InlineData("", @".", "/SubFolder/")] - [InlineData("/somedir", @"", "/somedir/")] + [InlineData("/somedir", @".", "/somedir/")] [InlineData("/somedir", @".", "/somedir/subfolder/")] public async Task PostDirectory_PassesThrough(string baseUrl, string baseDir, string requestUrl) { TestServer server = TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) })); HttpResponseMessage response = await server.CreateRequest(requestUrl).PostAsync(); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); } [Theory] - [InlineData("", @"", "/")] [InlineData("", @".", "/")] - [InlineData("", @"", "/SubFolder/")] [InlineData("", @".", "/SubFolder/")] - [InlineData("/somedir", @"", "/somedir/")] + [InlineData("/somedir", @".", "/somedir/")] [InlineData("/somedir", @".", "/somedir/subfolder/")] public async Task HeadDirectory_HeadersButNotBodyServed(string baseUrl, string baseDir, string requestUrl) { TestServer server = TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) })); HttpResponseMessage response = await server.CreateRequest(requestUrl).SendAsync("HEAD"); diff --git a/test/Microsoft.AspNet.StaticFiles.Tests/StaticFileMiddlewareTests.cs b/test/Microsoft.AspNet.StaticFiles.Tests/StaticFileMiddlewareTests.cs index 1983b435ff..51f754633f 100644 --- a/test/Microsoft.AspNet.StaticFiles.Tests/StaticFileMiddlewareTests.cs +++ b/test/Microsoft.AspNet.StaticFiles.Tests/StaticFileMiddlewareTests.cs @@ -4,6 +4,7 @@ using System; using System.IO; using System.Net; using System.Net.Http; +using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.FileSystems; @@ -18,8 +19,7 @@ namespace Microsoft.AspNet.StaticFiles [Fact] public async Task NullArguments() { - Assert.Throws(() => TestServer.Create(app => app.UseStaticFiles((StaticFileOptions)null))); - Assert.Throws(() => TestServer.Create(app => app.UseStaticFiles(new StaticFileOptions() { ContentTypeProvider = null }))); + Assert.Throws(() => TestServer.Create(app => app.UseStaticFiles(new StaticFileOptions() { ContentTypeProvider = null }))); // No exception, default provided TestServer.Create(app => app.UseStaticFiles(new StaticFileOptions() { FileSystem = null })); @@ -33,20 +33,20 @@ namespace Microsoft.AspNet.StaticFiles [Fact] public void GivenDirDoesntExist_Throw() { - Assert.Throws(() => TestServer.Create(app => app.UseStaticFiles("/ThisDirDoesntExist"))); + Assert.Throws(() => TestServer.Create(app => app.UseStaticFiles("/ThisDirDoesntExist"))); } [Theory] [InlineData("", @".", "/missing.file")] [InlineData("/subdir", @".", "/subdir/missing.file")] - [InlineData("/missing.file", @"\", "/missing.file")] - [InlineData("", @"\", "/xunit.xml")] + [InlineData("/missing.file", @".\", "/missing.file")] + [InlineData("", @".\", "/xunit.xml")] public async Task NoMatch_PassesThrough(string baseUrl, string baseDir, string requestUrl) { TestServer server = TestServer.Create(app => app.UseStaticFiles(new StaticFileOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) })); HttpResponseMessage response = await server.CreateRequest(requestUrl).GetAsync(); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); @@ -64,7 +64,7 @@ namespace Microsoft.AspNet.StaticFiles TestServer server = TestServer.Create(app => app.UseStaticFiles(new StaticFileOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) })); HttpResponseMessage response = await server.CreateRequest(requestUrl).GetAsync(); @@ -86,7 +86,7 @@ namespace Microsoft.AspNet.StaticFiles TestServer server = TestServer.Create(app => app.UseStaticFiles(new StaticFileOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) })); HttpResponseMessage response = await server.CreateRequest(requestUrl).PostAsync(); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); @@ -104,7 +104,7 @@ namespace Microsoft.AspNet.StaticFiles TestServer server = TestServer.Create(app => app.UseStaticFiles(new StaticFileOptions() { RequestPath = new PathString(baseUrl), - FileSystem = new PhysicalFileSystem(baseDir) + FileSystem = new PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, baseDir)) })); HttpResponseMessage response = await server.CreateRequest(requestUrl).SendAsync("HEAD");