|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +using Microsoft.CodeAnalysis; |
| 4 | +namespace Microsoft.AspNetCore.Http.Generators.Tests; |
| 5 | + |
| 6 | +public class CompileTimeGeneratorTests : RequestDelegateGeneratorTests |
| 7 | +{ |
| 8 | + protected override bool IsGeneratorEnabled { get; } = true; |
| 9 | + |
| 10 | + [Fact] |
| 11 | + public async Task MapAction_UnknownParameter_EmitsDiagnostic_NoSource() |
| 12 | + { |
| 13 | + // This will eventually be handled by the EndpointParameterSource.JsonBodyOrService. |
| 14 | + // All parameters should theoretically be handleable with enough "Or"s in the future |
| 15 | + // we'll remove this test and diagnostic. |
| 16 | + var source = """ |
| 17 | +app.MapGet("/", (IServiceProvider provider) => "Hello world!"); |
| 18 | +"""; |
| 19 | + var expectedBody = "Hello world!"; |
| 20 | + var (generatorRunResult, compilation) = await RunGeneratorAsync(source); |
| 21 | + |
| 22 | + // Emits diagnostic but generates no source |
| 23 | + var result = Assert.IsType<GeneratorRunResult>(generatorRunResult); |
| 24 | + var diagnostic = Assert.Single(result.Diagnostics); |
| 25 | + Assert.Equal(DiagnosticDescriptors.GetUnableToResolveParameterDescriptor("provider").Id, diagnostic.Id); |
| 26 | + Assert.Empty(result.GeneratedSources); |
| 27 | + |
| 28 | + // Falls back to runtime-generated endpoint |
| 29 | + var endpoint = GetEndpointFromCompilation(compilation, false); |
| 30 | + |
| 31 | + var httpContext = CreateHttpContext(); |
| 32 | + await endpoint.RequestDelegate(httpContext); |
| 33 | + await VerifyResponseBodyAsync(httpContext, expectedBody); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public async Task MapGet_WithRequestDelegate_DoesNotGenerateSources() |
| 38 | + { |
| 39 | + var (generatorRunResult, compilation) = await RunGeneratorAsync(""" |
| 40 | +app.MapGet("/hello", (HttpContext context) => Task.CompletedTask); |
| 41 | +"""); |
| 42 | + var results = Assert.IsType<GeneratorRunResult>(generatorRunResult); |
| 43 | + Assert.Empty(GetStaticEndpoints(results, GeneratorSteps.EndpointModelStep)); |
| 44 | + |
| 45 | + var endpoint = GetEndpointFromCompilation(compilation, false); |
| 46 | + |
| 47 | + var httpContext = CreateHttpContext(); |
| 48 | + await endpoint.RequestDelegate(httpContext); |
| 49 | + await VerifyResponseBodyAsync(httpContext, ""); |
| 50 | + } |
| 51 | + |
| 52 | + // Todo: Move this to a shared test that checks metadata once that is supported |
| 53 | + // in the source generator. |
| 54 | + [Theory] |
| 55 | + [InlineData(@"app.MapGet(""/"", () => Console.WriteLine(""Returns void""));", null)] |
| 56 | + [InlineData(@"app.MapGet(""/"", () => TypedResults.Ok(""Alright!""));", null)] |
| 57 | + [InlineData(@"app.MapGet(""/"", () => Results.NotFound(""Oops!""));", null)] |
| 58 | + [InlineData(@"app.MapGet(""/"", () => Task.FromResult(new Todo() { Name = ""Test Item""}));", "application/json")] |
| 59 | + [InlineData(@"app.MapGet(""/"", () => ""Hello world!"");", "text/plain")] |
| 60 | + public async Task MapAction_ProducesCorrectContentType(string source, string expectedContentType) |
| 61 | + { |
| 62 | + var (result, compilation) = await RunGeneratorAsync(source); |
| 63 | + |
| 64 | + VerifyStaticEndpointModel(result, endpointModel => |
| 65 | + { |
| 66 | + Assert.Equal("/", endpointModel.RoutePattern); |
| 67 | + Assert.Equal("MapGet", endpointModel.HttpMethod); |
| 68 | + Assert.Equal(expectedContentType, endpointModel.Response.ContentType); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public async Task MapAction_ExplicitRouteParamWithInvalidName_SimpleReturn() |
| 74 | + { |
| 75 | + var source = $$"""app.MapGet("/{routeValue}", ([FromRoute(Name = "invalidName" )] string parameterName) => parameterName);"""; |
| 76 | + var (_, compilation) = await RunGeneratorAsync(source); |
| 77 | + var endpoint = GetEndpointFromCompilation(compilation); |
| 78 | + |
| 79 | + var httpContext = CreateHttpContext(); |
| 80 | + |
| 81 | + var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => endpoint.RequestDelegate(httpContext)); |
| 82 | + Assert.Equal("'invalidName' is not a route parameter.", exception.Message); |
| 83 | + } |
| 84 | +} |
0 commit comments