spelling, inline outs, redundant braces (#252)

This commit is contained in:
Simon Cropp 2018-09-28 13:24:42 +10:00 committed by Chris Ross
parent 47e093bb24
commit 8dcc011324
10 changed files with 31 additions and 40 deletions

View File

@ -62,9 +62,8 @@ namespace Microsoft.AspNetCore.StaticFiles
/// <returns></returns> /// <returns></returns>
public Task Invoke(HttpContext context) public Task Invoke(HttpContext context)
{ {
PathString subpath;
if (Helpers.IsGetOrHeadMethod(context.Request.Method) if (Helpers.IsGetOrHeadMethod(context.Request.Method)
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath)) && Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out var subpath))
{ {
var dirContents = _fileProvider.GetDirectoryContents(subpath.Value); var dirContents = _fileProvider.GetDirectoryContents(subpath.Value);
if (dirContents.Exists) if (dirContents.Exists)

View File

@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Builder
: base(sharedOptions) : base(sharedOptions)
{ {
// Prioritized list // Prioritized list
DefaultFileNames = new List<string>() DefaultFileNames = new List<string>
{ {
"default.htm", "default.htm",
"default.html", "default.html",

View File

@ -79,11 +79,9 @@ namespace Microsoft.AspNetCore.StaticFiles
public Task Invoke(HttpContext context) public Task Invoke(HttpContext context)
{ {
// Check if the URL matches any expected paths // Check if the URL matches any expected paths
PathString subpath;
IDirectoryContents contents;
if (Helpers.IsGetOrHeadMethod(context.Request.Method) if (Helpers.IsGetOrHeadMethod(context.Request.Method)
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath) && Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out var subpath)
&& TryGetDirectoryInfo(subpath, out contents)) && TryGetDirectoryInfo(subpath, out var contents))
{ {
// If the path matches a directory but does not end in a slash, redirect to add the slash. // If the path matches a directory but does not end in a slash, redirect to add the slash.
// This prevents relative links from breaking. // This prevents relative links from breaking.

View File

@ -282,7 +282,7 @@ namespace Microsoft.AspNetCore.StaticFiles
// it is not returned for 304, 412, and 416 // it is not returned for 304, 412, and 416
_response.ContentLength = _length; _response.ContentLength = _length;
} }
_options.OnPrepareResponse(new StaticFileResponseContext() _options.OnPrepareResponse(new StaticFileResponseContext
{ {
Context = _context, Context = _context,
File = _fileInfo, File = _fileInfo,
@ -360,8 +360,7 @@ namespace Microsoft.AspNetCore.StaticFiles
return; return;
} }
long start, length; _responseHeaders.ContentRange = ComputeContentRange(_range, out var start, out var length);
_responseHeaders.ContentRange = ComputeContentRange(_range, out start, out length);
_response.ContentLength = length; _response.ContentLength = length;
ApplyResponseHeaders(Constants.Status206PartialContent); ApplyResponseHeaders(Constants.Status206PartialContent);

View File

@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.StaticFiles
using (var server = builder.Start(TestUrlHelper.GetTestUrl(ServerType.Kestrel))) using (var server = builder.Start(TestUrlHelper.GetTestUrl(ServerType.Kestrel)))
{ {
using (var client = new HttpClient() { BaseAddress = new Uri(server.GetAddress()) }) using (var client = new HttpClient { BaseAddress = new Uri(server.GetAddress()) })
{ {
var response = await client.GetAsync("TestDocument.txt"); var response = await client.GetAsync("TestDocument.txt");
@ -55,14 +55,14 @@ namespace Microsoft.AspNetCore.StaticFiles
using (var server = builder.Start(TestUrlHelper.GetTestUrl(ServerType.Kestrel))) using (var server = builder.Start(TestUrlHelper.GetTestUrl(ServerType.Kestrel)))
{ {
using (var client = new HttpClient() { BaseAddress = new Uri(server.GetAddress()) }) using (var client = new HttpClient { BaseAddress = new Uri(server.GetAddress()) })
{ {
var last = File.GetLastWriteTimeUtc(Path.Combine(AppContext.BaseDirectory, "TestDocument.txt")); var last = File.GetLastWriteTimeUtc(Path.Combine(AppContext.BaseDirectory, "TestDocument.txt"));
var response = await client.GetAsync("TestDocument.txt"); var response = await client.GetAsync("TestDocument.txt");
var trimed = new DateTimeOffset(last.Year, last.Month, last.Day, last.Hour, last.Minute, last.Second, TimeSpan.Zero).ToUniversalTime(); var trimmed = new DateTimeOffset(last.Year, last.Month, last.Day, last.Hour, last.Minute, last.Second, TimeSpan.Zero).ToUniversalTime();
Assert.Equal(response.Content.Headers.LastModified.Value, trimed); Assert.Equal(response.Content.Headers.LastModified.Value, trimmed);
} }
} }
} }
@ -92,7 +92,7 @@ namespace Microsoft.AspNetCore.StaticFiles
.ConfigureServices(services => services.AddSingleton(LoggerFactory)) .ConfigureServices(services => services.AddSingleton(LoggerFactory))
.UseKestrel() .UseKestrel()
.UseWebRoot(Path.Combine(AppContext.BaseDirectory, baseDir)) .UseWebRoot(Path.Combine(AppContext.BaseDirectory, baseDir))
.Configure(app => app.UseStaticFiles(new StaticFileOptions() .Configure(app => app.UseStaticFiles(new StaticFileOptions
{ {
RequestPath = new PathString(baseUrl), RequestPath = new PathString(baseUrl),
})); }));
@ -101,7 +101,7 @@ namespace Microsoft.AspNetCore.StaticFiles
{ {
var hostingEnvironment = server.Services.GetService<IHostingEnvironment>(); var hostingEnvironment = server.Services.GetService<IHostingEnvironment>();
using (var client = new HttpClient() { BaseAddress = new Uri(server.GetAddress()) }) using (var client = new HttpClient { BaseAddress = new Uri(server.GetAddress()) })
{ {
var fileInfo = hostingEnvironment.WebRootFileProvider.GetFileInfo(Path.GetFileName(requestUrl)); var fileInfo = hostingEnvironment.WebRootFileProvider.GetFileInfo(Path.GetFileName(requestUrl));
var response = await client.GetAsync(requestUrl); var response = await client.GetAsync(requestUrl);
@ -130,7 +130,7 @@ namespace Microsoft.AspNetCore.StaticFiles
.ConfigureServices(services => services.AddSingleton(LoggerFactory)) .ConfigureServices(services => services.AddSingleton(LoggerFactory))
.UseKestrel() .UseKestrel()
.UseWebRoot(Path.Combine(AppContext.BaseDirectory, baseDir)) .UseWebRoot(Path.Combine(AppContext.BaseDirectory, baseDir))
.Configure(app => app.UseStaticFiles(new StaticFileOptions() .Configure(app => app.UseStaticFiles(new StaticFileOptions
{ {
RequestPath = new PathString(baseUrl), RequestPath = new PathString(baseUrl),
})); }));
@ -139,7 +139,7 @@ namespace Microsoft.AspNetCore.StaticFiles
{ {
var hostingEnvironment = server.Services.GetService<IHostingEnvironment>(); var hostingEnvironment = server.Services.GetService<IHostingEnvironment>();
using (var client = new HttpClient() { BaseAddress = new Uri(server.GetAddress()) }) using (var client = new HttpClient { BaseAddress = new Uri(server.GetAddress()) })
{ {
var fileInfo = hostingEnvironment.WebRootFileProvider.GetFileInfo(Path.GetFileName(requestUrl)); var fileInfo = hostingEnvironment.WebRootFileProvider.GetFileInfo(Path.GetFileName(requestUrl));
var request = new HttpRequestMessage(HttpMethod.Head, requestUrl); var request = new HttpRequestMessage(HttpMethod.Head, requestUrl);
@ -181,7 +181,7 @@ namespace Microsoft.AspNetCore.StaticFiles
{ {
var interval = TimeSpan.FromSeconds(15); var interval = TimeSpan.FromSeconds(15);
var requestReceived = new ManualResetEvent(false); var requestReceived = new ManualResetEvent(false);
var requestCacelled = new ManualResetEvent(false); var requestCancelled = new ManualResetEvent(false);
var responseComplete = new ManualResetEvent(false); var responseComplete = new ManualResetEvent(false);
Exception exception = null; Exception exception = null;
var builder = new WebHostBuilder() var builder = new WebHostBuilder()
@ -194,7 +194,7 @@ namespace Microsoft.AspNetCore.StaticFiles
try try
{ {
requestReceived.Set(); requestReceived.Set();
Assert.True(requestCacelled.WaitOne(interval), "not cancelled"); Assert.True(requestCancelled.WaitOne(interval), "not cancelled");
Assert.True(context.RequestAborted.WaitHandle.WaitOne(interval), "not aborted"); Assert.True(context.RequestAborted.WaitHandle.WaitOne(interval), "not aborted");
await next(); await next();
} }
@ -224,7 +224,7 @@ namespace Microsoft.AspNetCore.StaticFiles
socket.LingerState = new LingerOption(true, 0); socket.LingerState = new LingerOption(true, 0);
socket.Dispose(); socket.Dispose();
requestCacelled.Set(); requestCancelled.Set();
Assert.True(responseComplete.WaitOne(interval), "not completed"); Assert.True(responseComplete.WaitOne(interval), "not completed");
Assert.Null(exception); Assert.Null(exception);

View File

@ -69,7 +69,7 @@ namespace Microsoft.AspNetCore.StaticFiles
[Theory] [Theory]
[MemberData(nameof(SupportedMethods))] [MemberData(nameof(SupportedMethods))]
public async Task IfMatchShouldBeServedForAstrisk(HttpMethod method) public async Task IfMatchShouldBeServedForAsterisk(HttpMethod method)
{ {
TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer()); TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
var req = new HttpRequestMessage(method, "http://localhost/SubFolder/extra.xml"); var req = new HttpRequestMessage(method, "http://localhost/SubFolder/extra.xml");
@ -230,7 +230,7 @@ namespace Microsoft.AspNetCore.StaticFiles
DateTimeOffset lastModified = resp1.Content.Headers.LastModified.Value; DateTimeOffset lastModified = resp1.Content.Headers.LastModified.Value;
DateTimeOffset pastDate = lastModified.AddHours(-1); DateTimeOffset pastDate = lastModified.AddHours(-1);
DateTimeOffset furtureDate = lastModified.AddHours(1); DateTimeOffset futureDate = lastModified.AddHours(1);
HttpResponseMessage resp2 = await server HttpResponseMessage resp2 = await server
.CreateRequest("/SubFolder/extra.xml") .CreateRequest("/SubFolder/extra.xml")
@ -247,7 +247,7 @@ namespace Microsoft.AspNetCore.StaticFiles
HttpResponseMessage resp4 = await server HttpResponseMessage resp4 = await server
.CreateRequest("/SubFolder/extra.xml") .CreateRequest("/SubFolder/extra.xml")
.AddHeader("If-None-Match", "\"fake\"") .AddHeader("If-None-Match", "\"fake\"")
.And(req => req.Headers.IfModifiedSince = furtureDate) .And(req => req.Headers.IfModifiedSince = futureDate)
.SendAsync(method.Method); .SendAsync(method.Method);
Assert.Equal(HttpStatusCode.OK, resp2.StatusCode); Assert.Equal(HttpStatusCode.OK, resp2.StatusCode);
@ -322,7 +322,7 @@ namespace Microsoft.AspNetCore.StaticFiles
[Theory] [Theory]
[MemberData(nameof(SupportedMethods))] [MemberData(nameof(SupportedMethods))]
public async Task SuppportsIfModifiedDateFormats(HttpMethod method) public async Task SupportsIfModifiedDateFormats(HttpMethod method)
{ {
TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer()); TestServer server = StaticFilesTestServer.Create(app => app.UseFileServer());
HttpResponseMessage res1 = await server HttpResponseMessage res1 = await server

View File

@ -16,11 +16,10 @@ namespace Microsoft.AspNetCore.StaticFiles
} }
[Fact] [Fact]
public void KnownExtensionsReturnTrye() public void KnownExtensionsReturnType()
{ {
var provider = new FileExtensionContentTypeProvider(); var provider = new FileExtensionContentTypeProvider();
string contentType; Assert.True(provider.TryGetContentType("known.txt", out var contentType));
Assert.True(provider.TryGetContentType("known.txt", out contentType));
Assert.Equal("text/plain", contentType); Assert.Equal("text/plain", contentType);
} }
@ -36,8 +35,7 @@ namespace Microsoft.AspNetCore.StaticFiles
public void DashedExtensionsShouldBeMatched() public void DashedExtensionsShouldBeMatched()
{ {
var provider = new FileExtensionContentTypeProvider(); var provider = new FileExtensionContentTypeProvider();
string contentType; Assert.True(provider.TryGetContentType("known.dvr-ms", out var contentType));
Assert.True(provider.TryGetContentType("known.dvr-ms", out contentType));
Assert.Equal("video/x-ms-dvr", contentType); Assert.Equal("video/x-ms-dvr", contentType);
} }
@ -45,8 +43,7 @@ namespace Microsoft.AspNetCore.StaticFiles
public void BothSlashFormatsAreUnderstood() public void BothSlashFormatsAreUnderstood()
{ {
var provider = new FileExtensionContentTypeProvider(); var provider = new FileExtensionContentTypeProvider();
string contentType; Assert.True(provider.TryGetContentType(@"/first/example.txt", out var contentType));
Assert.True(provider.TryGetContentType(@"/first/example.txt", out contentType));
Assert.Equal("text/plain", contentType); Assert.Equal("text/plain", contentType);
Assert.True(provider.TryGetContentType(@"\second\example.txt", out contentType)); Assert.True(provider.TryGetContentType(@"\second\example.txt", out contentType));
Assert.Equal("text/plain", contentType); Assert.Equal("text/plain", contentType);
@ -56,8 +53,7 @@ namespace Microsoft.AspNetCore.StaticFiles
public void DotsInDirectoryAreIgnored() public void DotsInDirectoryAreIgnored()
{ {
var provider = new FileExtensionContentTypeProvider(); var provider = new FileExtensionContentTypeProvider();
string contentType; Assert.True(provider.TryGetContentType(@"/first.css/example.txt", out var contentType));
Assert.True(provider.TryGetContentType(@"/first.css/example.txt", out contentType));
Assert.Equal("text/plain", contentType); Assert.Equal("text/plain", contentType);
Assert.True(provider.TryGetContentType(@"\second.css\example.txt", out contentType)); Assert.True(provider.TryGetContentType(@"\second.css\example.txt", out contentType));
Assert.Equal("text/plain", contentType); Assert.Equal("text/plain", contentType);

View File

@ -169,12 +169,12 @@ namespace Microsoft.AspNetCore.StaticFiles
private async Task PostDirectory_PassesThrough(string baseUrl, string baseDir, string requestUrl) private async Task PostDirectory_PassesThrough(string baseUrl, string baseDir, string requestUrl)
{ {
using (var fileProvder = new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, baseDir))) using (var fileProvider = new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, baseDir)))
{ {
var server = StaticFilesTestServer.Create(app => app.UseDefaultFiles(new DefaultFilesOptions var server = StaticFilesTestServer.Create(app => app.UseDefaultFiles(new DefaultFilesOptions
{ {
RequestPath = new PathString(baseUrl), RequestPath = new PathString(baseUrl),
FileProvider = fileProvder FileProvider = fileProvider
})); }));
var response = await server.CreateRequest(requestUrl).GetAsync(); var response = await server.CreateRequest(requestUrl).GetAsync();

View File

@ -70,8 +70,7 @@ namespace Microsoft.AspNetCore.StaticFiles
public IFileInfo GetFileInfo(string subpath) public IFileInfo GetFileInfo(string subpath)
{ {
IFileInfo result; if (_files.TryGetValue(subpath, out var result))
if (_files.TryGetValue(subpath, out result))
{ {
return result; return result;
} }

View File

@ -101,9 +101,9 @@ namespace Microsoft.AspNetCore.StaticFiles
var response = await server.CreateRequest("TestDocument.txt").GetAsync(); var response = await server.CreateRequest("TestDocument.txt").GetAsync();
var last = fileInfo.LastModified; var last = fileInfo.LastModified;
var trimed = new DateTimeOffset(last.Year, last.Month, last.Day, last.Hour, last.Minute, last.Second, last.Offset).ToUniversalTime(); var trimmed = new DateTimeOffset(last.Year, last.Month, last.Day, last.Hour, last.Minute, last.Second, last.Offset).ToUniversalTime();
Assert.Equal(response.Content.Headers.LastModified.Value, trimed); Assert.Equal(response.Content.Headers.LastModified.Value, trimmed);
} }
} }