|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using System.Diagnostics;
|
| 5 | +using System.Globalization; |
5 | 6 | using System.Net;
|
6 | 7 | using Microsoft.AspNetCore.Builder;
|
7 | 8 | using Microsoft.AspNetCore.Hosting;
|
8 | 9 | using Microsoft.AspNetCore.Http;
|
9 | 10 | using Microsoft.AspNetCore.Http.Features;
|
10 | 11 | using Microsoft.AspNetCore.TestHost;
|
11 | 12 | using Microsoft.AspNetCore.Testing;
|
| 13 | +using Microsoft.Extensions.DependencyInjection; |
12 | 14 | using Microsoft.Extensions.FileProviders;
|
13 | 15 | using Microsoft.Extensions.Hosting;
|
14 | 16 | using Moq;
|
@@ -193,6 +195,86 @@ private async Task FoundFile_Served(string baseUrl, string baseDir, string reque
|
193 | 195 | }
|
194 | 196 | }
|
195 | 197 |
|
| 198 | + [Fact] |
| 199 | + public async Task File_Served_If_Endpoint_With_Null_RequestDelegate_Is_Active() |
| 200 | + { |
| 201 | + using (var fileProvider = new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, "."))) |
| 202 | + { |
| 203 | + using var host = await StaticFilesTestServer.Create(app => |
| 204 | + { |
| 205 | + app.UseRouting(); |
| 206 | + app.Use((ctx, next) => |
| 207 | + { |
| 208 | + ctx.SetEndpoint(new Endpoint(requestDelegate: null, new EndpointMetadataCollection(), "NullRequestDelegateEndpoint")); |
| 209 | + return next(); |
| 210 | + }); |
| 211 | + app.UseStaticFiles(new StaticFileOptions |
| 212 | + { |
| 213 | + RequestPath = new PathString(), |
| 214 | + FileProvider = fileProvider |
| 215 | + }); |
| 216 | + app.UseEndpoints(endpoints => { }); |
| 217 | + }, services => services.AddRouting()); |
| 218 | + using var server = host.GetTestServer(); |
| 219 | + var requestUrl = "/TestDocument.txt"; |
| 220 | + var fileInfo = fileProvider.GetFileInfo(Path.GetFileName(requestUrl)); |
| 221 | + var response = await server.CreateRequest(requestUrl).GetAsync(); |
| 222 | + var responseContent = await response.Content.ReadAsByteArrayAsync(); |
| 223 | + |
| 224 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 225 | + Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString()); |
| 226 | + Assert.True(response.Content.Headers.ContentLength == fileInfo.Length); |
| 227 | + Assert.Equal(response.Content.Headers.ContentLength, responseContent.Length); |
| 228 | + Assert.NotNull(response.Headers.ETag); |
| 229 | + |
| 230 | + using (var stream = fileInfo.CreateReadStream()) |
| 231 | + { |
| 232 | + var fileContents = new byte[stream.Length]; |
| 233 | + stream.Read(fileContents, 0, (int)stream.Length); |
| 234 | + Assert.True(responseContent.SequenceEqual(fileContents)); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + [Fact] |
| 240 | + public async Task File_NotServed_If_Endpoint_With_RequestDelegate_Is_Active() |
| 241 | + { |
| 242 | + var responseText = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture); |
| 243 | + RequestDelegate handler = async (ctx) => |
| 244 | + { |
| 245 | + ctx.Response.ContentType = "text/customfortest+plain"; |
| 246 | + await ctx.Response.WriteAsync(responseText); |
| 247 | + }; |
| 248 | + |
| 249 | + using (var fileProvider = new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, "."))) |
| 250 | + { |
| 251 | + using var host = await StaticFilesTestServer.Create(app => |
| 252 | + { |
| 253 | + app.UseRouting(); |
| 254 | + app.Use((ctx, next) => |
| 255 | + { |
| 256 | + ctx.SetEndpoint(new Endpoint(handler, new EndpointMetadataCollection(), "RequestDelegateEndpoint")); |
| 257 | + return next(); |
| 258 | + }); |
| 259 | + app.UseStaticFiles(new StaticFileOptions |
| 260 | + { |
| 261 | + RequestPath = new PathString(), |
| 262 | + FileProvider = fileProvider |
| 263 | + }); |
| 264 | + app.UseEndpoints(endpoints => { }); |
| 265 | + }, services => services.AddRouting()); |
| 266 | + using var server = host.GetTestServer(); |
| 267 | + var requestUrl = "/TestDocument.txt"; |
| 268 | + |
| 269 | + var response = await server.CreateRequest(requestUrl).GetAsync(); |
| 270 | + var responseContent = await response.Content.ReadAsStringAsync(); |
| 271 | + |
| 272 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 273 | + Assert.Equal("text/customfortest+plain", response.Content.Headers.ContentType.ToString()); |
| 274 | + Assert.Equal(responseText, responseContent); |
| 275 | + } |
| 276 | + } |
| 277 | + |
196 | 278 | [Theory]
|
197 | 279 | [MemberData(nameof(ExistingFiles))]
|
198 | 280 | public async Task HeadFile_HeadersButNotBodyServed(string baseUrl, string baseDir, string requestUrl)
|
|
0 commit comments