-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add shared test infra for RDF and RDG #46838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
src/Http/Http.Extensions/test/RequestDelegateGenerator/CompileTimeGeneratorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| using Microsoft.CodeAnalysis; | ||
| namespace Microsoft.AspNetCore.Http.Generators.Tests; | ||
|
|
||
| public class CompileTimeGeneratorTests : RequestDelegateGeneratorTests | ||
| { | ||
| protected override bool IsGeneratorEnabled { get; } = true; | ||
|
|
||
| [Fact] | ||
| public async Task MapAction_UnknownParameter_EmitsDiagnostic_NoSource() | ||
| { | ||
| // This will eventually be handled by the EndpointParameterSource.JsonBodyOrService. | ||
| // All parameters should theoretically be handleable with enough "Or"s in the future | ||
| // we'll remove this test and diagnostic. | ||
| var source = """ | ||
| app.MapGet("/", (IServiceProvider provider) => "Hello world!"); | ||
| """; | ||
| var expectedBody = "Hello world!"; | ||
| var (generatorRunResult, compilation) = await RunGeneratorAsync(source); | ||
|
|
||
| // Emits diagnostic but generates no source | ||
| var result = Assert.IsType<GeneratorRunResult>(generatorRunResult); | ||
| var diagnostic = Assert.Single(result.Diagnostics); | ||
| Assert.Equal(DiagnosticDescriptors.GetUnableToResolveParameterDescriptor("provider").Id, diagnostic.Id); | ||
| Assert.Empty(result.GeneratedSources); | ||
|
|
||
| // Falls back to runtime-generated endpoint | ||
| var endpoint = GetEndpointFromCompilation(compilation, false); | ||
|
|
||
| var httpContext = CreateHttpContext(); | ||
| await endpoint.RequestDelegate(httpContext); | ||
| await VerifyResponseBodyAsync(httpContext, expectedBody); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task MapGet_WithRequestDelegate_DoesNotGenerateSources() | ||
| { | ||
| var (generatorRunResult, compilation) = await RunGeneratorAsync(""" | ||
| app.MapGet("/hello", (HttpContext context) => Task.CompletedTask); | ||
| """); | ||
| var results = Assert.IsType<GeneratorRunResult>(generatorRunResult); | ||
| Assert.Empty(GetStaticEndpoints(results, GeneratorSteps.EndpointModelStep)); | ||
|
|
||
| var endpoint = GetEndpointFromCompilation(compilation, false); | ||
|
|
||
| var httpContext = CreateHttpContext(); | ||
| await endpoint.RequestDelegate(httpContext); | ||
| await VerifyResponseBodyAsync(httpContext, ""); | ||
| } | ||
|
|
||
| // Todo: Move this to a shared test that checks metadata once that is supported | ||
| // in the source generator. | ||
| [Theory] | ||
| [InlineData(@"app.MapGet(""/"", () => Console.WriteLine(""Returns void""));", null)] | ||
| [InlineData(@"app.MapGet(""/"", () => TypedResults.Ok(""Alright!""));", null)] | ||
| [InlineData(@"app.MapGet(""/"", () => Results.NotFound(""Oops!""));", null)] | ||
| [InlineData(@"app.MapGet(""/"", () => Task.FromResult(new Todo() { Name = ""Test Item""}));", "application/json")] | ||
| [InlineData(@"app.MapGet(""/"", () => ""Hello world!"");", "text/plain")] | ||
| public async Task MapAction_ProducesCorrectContentType(string source, string expectedContentType) | ||
| { | ||
| var (result, compilation) = await RunGeneratorAsync(source); | ||
|
|
||
| VerifyStaticEndpointModel(result, endpointModel => | ||
| { | ||
| Assert.Equal("/", endpointModel.RoutePattern); | ||
| Assert.Equal("MapGet", endpointModel.HttpMethod); | ||
| Assert.Equal(expectedContentType, endpointModel.Response.ContentType); | ||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task MapAction_ExplicitRouteParamWithInvalidName_SimpleReturn() | ||
| { | ||
| var source = $$"""app.MapGet("/{routeValue}", ([FromRoute(Name = "invalidName" )] string parameterName) => parameterName);"""; | ||
| var (_, compilation) = await RunGeneratorAsync(source); | ||
| var endpoint = GetEndpointFromCompilation(compilation); | ||
|
|
||
| var httpContext = CreateHttpContext(); | ||
|
|
||
| var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => endpoint.RequestDelegate(httpContext)); | ||
| Assert.Equal("'invalidName' is not a route parameter.", exception.Message); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RDF would just try to read
context.Request.RouteValues["invalidName"]even though it's not part of the pattern, right? I know this PR is just about tests and not fixing behavior, but do we agree that we should align behavior here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RDF throws the exception earlier when the endpoint is being constructed (ref). We could do something similar in RDG, but that would mean presenting the exception at compile-time which would require us to have a way to parse the route patterns in the generator. #46342 tracks doing this work.