From c7fb5c981819987c6b19c2dec2a66a50ac41178b Mon Sep 17 00:00:00 2001 From: Wei Lin Date: Sun, 2 Jun 2024 22:30:34 +0800 Subject: [PATCH] Test: asp net core api AOT --- .../TestAspNetCoreApiAot/Program.cs | 45 +++++++++++++++++++ .../Properties/launchSettings.json | 15 +++++++ .../TestAspNetCoreApiAot.csproj | 15 +++++++ .../TestAspNetCoreApiAot.http | 11 +++++ .../appsettings.Development.json | 8 ++++ .../TestAspNetCoreApiAot/appsettings.json | 9 ++++ 6 files changed, 103 insertions(+) create mode 100644 tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/Program.cs create mode 100644 tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/Properties/launchSettings.json create mode 100644 tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/TestAspNetCoreApiAot.csproj create mode 100644 tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/TestAspNetCoreApiAot.http create mode 100644 tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/appsettings.Development.json create mode 100644 tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/appsettings.json diff --git a/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/Program.cs b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/Program.cs new file mode 100644 index 0000000..a046a38 --- /dev/null +++ b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/Program.cs @@ -0,0 +1,45 @@ +using System.Text.Json.Serialization; + +namespace TestAspNetCoreApiAot +{ + public class Program + { + public static void Main(string[] args) + { + var builder = WebApplication.CreateSlimBuilder(args); + + builder.Services.ConfigureHttpJsonOptions(options => + { + options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); + }); + builder.Services.AddMiniAuth(); + + var app = builder.Build(); + + var sampleTodos = new Todo[] { + new(1, "Walk the dog"), + new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)), + new(3, "Do the laundry", DateOnly.FromDateTime(DateTime.Now.AddDays(1))), + new(4, "Clean the bathroom"), + new(5, "Clean the car", DateOnly.FromDateTime(DateTime.Now.AddDays(2))) + }; + + var todosApi = app.MapGroup("/todos"); + todosApi.MapGet("/", () => sampleTodos); + todosApi.MapGet("/{id}", (int id) => + sampleTodos.FirstOrDefault(a => a.Id == id) is { } todo + ? Results.Ok(todo) + : Results.NotFound()); + + app.Run(); + } + } + + public record Todo(int Id, string? Title, DateOnly? DueBy = null, bool IsComplete = false); + + [JsonSerializable(typeof(Todo[]))] + internal partial class AppJsonSerializerContext : JsonSerializerContext + { + + } +} diff --git a/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/Properties/launchSettings.json b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/Properties/launchSettings.json new file mode 100644 index 0000000..5548404 --- /dev/null +++ b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/Properties/launchSettings.json @@ -0,0 +1,15 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "todos", + "applicationUrl": "http://localhost:5170", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/TestAspNetCoreApiAot.csproj b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/TestAspNetCoreApiAot.csproj new file mode 100644 index 0000000..710d817 --- /dev/null +++ b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/TestAspNetCoreApiAot.csproj @@ -0,0 +1,15 @@ + + + + net8.0 + enable + enable + true + true + + + + + + + diff --git a/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/TestAspNetCoreApiAot.http b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/TestAspNetCoreApiAot.http new file mode 100644 index 0000000..f9eedc2 --- /dev/null +++ b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/TestAspNetCoreApiAot.http @@ -0,0 +1,11 @@ +@TestAspNetCoreApiAot_HostAddress = http://localhost:5170 + +GET {{TestAspNetCoreApiAot_HostAddress}}/todos/ +Accept: application/json + +### + +GET {{TestAspNetCoreApiAot_HostAddress}}/todos/1 +Accept: application/json + +### diff --git a/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/appsettings.Development.json b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/appsettings.json b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/tests/TestAspNetCoreApiAot/TestAspNetCoreApiAot/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}