#8 - Use IHostingEnvironment.WebRoot as the default static files root.
This commit is contained in:
parent
acc5c31f28
commit
ea3ea90b7a
|
|
@ -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
|
|||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <returns></returns>
|
||||
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
|
|||
/// <param name="builder"></param>
|
||||
/// <param name="requestPath">The relative request path and physical path.</param>
|
||||
/// <returns></returns>
|
||||
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) });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -39,14 +38,9 @@ namespace Microsoft.AspNet.Builder
|
|||
/// <param name="builder"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
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<DefaultFilesMiddleware>(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
|||
/// </summary>
|
||||
/// <param name="next">The next middleware in the pipeline.</param>
|
||||
/// <param name="options">The configuration options for this middleware.</param>
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -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
|
|||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <returns></returns>
|
||||
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
|
|||
/// <param name="builder"></param>
|
||||
/// <param name="requestPath">The relative request path and physical path.</param>
|
||||
/// <returns></returns>
|
||||
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) });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -39,14 +38,9 @@ namespace Microsoft.AspNet.Builder
|
|||
/// <param name="builder"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
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<DirectoryBrowserMiddleware>(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
|||
/// </summary>
|
||||
/// <param name="next">The next middleware in the pipeline.</param>
|
||||
/// <param name="options">The configuration for this middleware.</param>
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -18,9 +18,9 @@ namespace Microsoft.AspNet.Builder
|
|||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <returns></returns>
|
||||
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());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -29,9 +29,9 @@ namespace Microsoft.AspNet.Builder
|
|||
/// <param name="builder"></param>
|
||||
/// <param name="enableDirectoryBrowsing">Should directory browsing be enabled?</param>
|
||||
/// <returns></returns>
|
||||
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 });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -40,9 +40,9 @@ namespace Microsoft.AspNet.Builder
|
|||
/// <param name="builder"></param>
|
||||
/// <param name="requestPath">The relative request path and physical path.</param>
|
||||
/// <returns></returns>
|
||||
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) });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -51,7 +51,7 @@ namespace Microsoft.AspNet.Builder
|
|||
/// <param name="builder"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseFileServer(this IApplicationBuilder builder, FileServerOptions options)
|
||||
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] FileServerOptions options)
|
||||
{
|
||||
if (options == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -17,13 +17,8 @@ namespace Microsoft.AspNet.StaticFiles
|
|||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <returns></returns>
|
||||
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<SendFileMiddleware>();
|
||||
}
|
||||
|
||||
private static bool IsSendFileSupported(IDictionary<string, object> properties)
|
||||
|
|
|
|||
|
|
@ -25,13 +25,8 @@ namespace Microsoft.AspNet.StaticFiles
|
|||
/// Creates a new instance of the SendFileMiddleware.
|
||||
/// </summary>
|
||||
/// <param name="next">The next middleware in the pipeline.</param>
|
||||
public SendFileMiddleware(RequestDelegate next)
|
||||
public SendFileMiddleware([NotNull] RequestDelegate next)
|
||||
{
|
||||
if (next == null)
|
||||
{
|
||||
throw new ArgumentNullException("next");
|
||||
}
|
||||
|
||||
_next = next;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,12 +19,8 @@ namespace Microsoft.AspNet.StaticFiles
|
|||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
/// <returns>True if sendfile.SendAsync is defined in the environment.</returns>
|
||||
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<IHttpSendFileFeature>() != null;
|
||||
}
|
||||
|
||||
|
|
@ -34,12 +30,8 @@ namespace Microsoft.AspNet.StaticFiles
|
|||
/// <param name="response"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
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
|
|||
/// <param name="count">The number of types to send, or null to send the remainder of the file.</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
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<IHttpSendFileFeature>();
|
||||
if (sendFile == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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
|
|||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <returns></returns>
|
||||
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());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -28,9 +27,9 @@ namespace Microsoft.AspNet.Builder
|
|||
/// <param name="builder"></param>
|
||||
/// <param name="requestPath">The relative request path and physical path.</param>
|
||||
/// <returns></returns>
|
||||
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) });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -39,13 +38,9 @@ namespace Microsoft.AspNet.Builder
|
|||
/// <param name="builder"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
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<StaticFileMiddleware>(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
|||
/// </summary>
|
||||
/// <param name="next">The next middleware in the pipeline.</param>
|
||||
/// <param name="options">The configuration options.</param>
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -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": {},
|
||||
|
|
|
|||
|
|
@ -20,8 +20,6 @@ namespace Microsoft.AspNet.StaticFiles
|
|||
[Fact]
|
||||
public async Task NullArguments()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => 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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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<ArgumentNullException>(() => TestServer.Create(app => app.UseDirectoryBrowser((DirectoryBrowserOptions)null)));
|
||||
Assert.Throws<ArgumentException>(() => TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions() { Formatter = null })));
|
||||
Assert.Throws<TargetInvocationException>(() => 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");
|
||||
|
||||
|
|
|
|||
|
|
@ -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<ArgumentNullException>(() => TestServer.Create(app => app.UseStaticFiles((StaticFileOptions)null)));
|
||||
Assert.Throws<ArgumentException>(() => TestServer.Create(app => app.UseStaticFiles(new StaticFileOptions() { ContentTypeProvider = null })));
|
||||
Assert.Throws<TargetInvocationException>(() => 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<DirectoryNotFoundException>(() => TestServer.Create(app => app.UseStaticFiles("/ThisDirDoesntExist")));
|
||||
Assert.Throws<TargetInvocationException>(() => 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");
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue