|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using Microsoft.AspNetCore.Components.Authorization; |
| 5 | +using Microsoft.AspNetCore.Components.Endpoints.Tests.TestComponents; |
5 | 6 | using Microsoft.AspNetCore.Hosting; |
6 | 7 | using Microsoft.AspNetCore.Http; |
7 | 8 | using Microsoft.AspNetCore.Routing; |
@@ -51,6 +52,49 @@ public async Task Invoker_RejectsPostRequestsWithNonFormDataContentTypesAsync() |
51 | 52 | Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode); |
52 | 53 | } |
53 | 54 |
|
| 55 | + [Fact] |
| 56 | + public async Task Invoker_HandlesHeadRequestAsync() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + var services = new ServiceCollection().AddRazorComponents() |
| 60 | + .Services.AddAntiforgery() |
| 61 | + .AddSingleton<IConfiguration>(new ConfigurationBuilder().Build()) |
| 62 | + .AddSingleton<IWebHostEnvironment>(new TestWebHostEnvironment()) |
| 63 | + .BuildServiceProvider(); |
| 64 | + |
| 65 | + var invoker = new RazorComponentEndpointInvoker( |
| 66 | + new EndpointHtmlRenderer( |
| 67 | + services, |
| 68 | + NullLoggerFactory.Instance), |
| 69 | + NullLogger<RazorComponentEndpointInvoker>.Instance); |
| 70 | + |
| 71 | + var context = new DefaultHttpContext(); |
| 72 | + context.SetEndpoint(new RouteEndpoint( |
| 73 | + ctx => Task.CompletedTask, |
| 74 | + RoutePatternFactory.Parse("/"), |
| 75 | + 0, |
| 76 | + new EndpointMetadataCollection( |
| 77 | + new ComponentTypeMetadata(typeof(SimpleComponent)), |
| 78 | + new RootComponentMetadata(typeof(SimpleComponent)), |
| 79 | + new ConfiguredRenderModesMetadata(Array.Empty<IComponentRenderMode>())), |
| 80 | + "test")); |
| 81 | + context.Request.Method = "HEAD"; |
| 82 | + context.Request.Scheme = "https"; |
| 83 | + context.Request.Host = new HostString("localhost"); |
| 84 | + context.Request.Path = "/"; |
| 85 | + context.Response.Body = new MemoryStream(); |
| 86 | + context.RequestServices = services; |
| 87 | + |
| 88 | + // Act |
| 89 | + await invoker.Render(context); |
| 90 | + |
| 91 | + // Assert |
| 92 | + // HEAD requests should execute the full request like GET, returning 200 OK with headers. |
| 93 | + // The HTTP server (Kestrel) handles suppressing the response body for HEAD requests. |
| 94 | + Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode); |
| 95 | + Assert.Equal("text/html; charset=utf-8", context.Response.ContentType); |
| 96 | + } |
| 97 | + |
54 | 98 | private class TestWebHostEnvironment : IWebHostEnvironment |
55 | 99 | { |
56 | 100 | public string WebRootPath { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } |
|
0 commit comments