Skip to content

Commit 0d2bf55

Browse files
committed
Add more assertions to native AoT tests
1 parent 125f729 commit 0d2bf55

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

src/Validation/test/Microsoft.Extensions.Validation.NativeAotTests/BasicMinimalApiWithValidation.cs

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44
using System;
55
using System.Collections.Generic;
66
using System.ComponentModel.DataAnnotations;
7+
using System.Net.Http;
8+
using System.Text.Json.Serialization;
79
using Microsoft.AspNetCore.Builder;
810
using Microsoft.AspNetCore.Http;
11+
using Microsoft.AspNetCore.Hosting;
912
using Microsoft.Extensions.DependencyInjection;
1013
using Microsoft.Extensions.Validation;
1114

1215
var builder = WebApplication.CreateSlimBuilder();
1316

1417
builder.Services.AddValidation();
18+
builder.WebHost.UseUrls("http://localhost:5000");
19+
builder.Services.ConfigureHttpJsonOptions(options =>
20+
{
21+
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
22+
});
1523

1624
var app = builder.Build();
1725

@@ -42,7 +50,67 @@
4250
TypedResults.Ok(new BulkProductResponse("Bulk products created", products.Length)))
4351
.DisableValidation();
4452

45-
app.Run();
53+
await app.StartAsync().ConfigureAwait(false);
54+
55+
try
56+
{
57+
// Create an HTTP client to test the endpoints
58+
using var httpClient = new HttpClient();
59+
httpClient.BaseAddress = new Uri("http://localhost:5000");
60+
61+
// Test 1: Valid ID - should succeed
62+
var response = await httpClient.GetAsync("/customers/123").ConfigureAwait(false);
63+
64+
if (response.IsSuccessStatusCode)
65+
{
66+
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
67+
Console.WriteLine($"Test 1 Success: {content}");
68+
}
69+
else
70+
{
71+
Console.WriteLine($"Test 1 Failed: {response.StatusCode}");
72+
return 1;
73+
}
74+
75+
// Test 2: Invalid ID (0) - should fail validation
76+
try
77+
{
78+
await httpClient.GetAsync("/customers/0").ConfigureAwait(false);
79+
}
80+
catch (HttpRequestException httpEx)
81+
{
82+
// Handle HttpRequestException and assert on the response
83+
if (httpEx.Data.Contains("HttpResponse"))
84+
{
85+
var invalidResponse = (HttpResponseMessage)httpEx.Data["HttpResponse"];
86+
if (invalidResponse.StatusCode == System.Net.HttpStatusCode.BadRequest)
87+
{
88+
var errorContent = await invalidResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
89+
Console.WriteLine($"Test 2 Success (Caught HttpRequestException with validation error): {invalidResponse.StatusCode}");
90+
Console.WriteLine($"Error content: {errorContent}");
91+
}
92+
else
93+
{
94+
Console.WriteLine($"Test 2 Failed: Expected BadRequest in exception but got {invalidResponse.StatusCode}");
95+
return 1;
96+
}
97+
}
98+
else
99+
{
100+
Console.WriteLine($"Test 2 Failed: HttpRequestException without HttpResponse data: {httpEx.Message}");
101+
return 1;
102+
}
103+
}
104+
}
105+
catch (Exception ex)
106+
{
107+
Console.WriteLine($"Error: {ex.Message}");
108+
return 1;
109+
}
110+
finally
111+
{
112+
await app.StopAsync().ConfigureAwait(false);
113+
}
46114

47115
return 100;
48116

@@ -163,4 +231,23 @@ public record AddressResponse(string Message, Address Address);
163231

164232
public record InventoryResponse(int ProductId, string Name);
165233

166-
public record BulkProductResponse(string Message, int Count);
234+
public record BulkProductResponse(string Message, int Count);
235+
236+
[JsonSerializable(typeof(Customer))]
237+
[JsonSerializable(typeof(Product))]
238+
[JsonSerializable(typeof(Address))]
239+
[JsonSerializable(typeof(Order))]
240+
[JsonSerializable(typeof(User))]
241+
[JsonSerializable(typeof(ProductResponse))]
242+
[JsonSerializable(typeof(AddressResponse))]
243+
[JsonSerializable(typeof(InventoryResponse))]
244+
[JsonSerializable(typeof(BulkProductResponse))]
245+
[JsonSerializable(typeof(Product[]))]
246+
[JsonSerializable(typeof(string))]
247+
[JsonSerializable(typeof(int))]
248+
[JsonSerializable(typeof(decimal))]
249+
[JsonSerializable(typeof(DateTime))]
250+
[JsonSerializable(typeof(Microsoft.AspNetCore.Http.HttpValidationProblemDetails))]
251+
internal sealed partial class AppJsonSerializerContext : JsonSerializerContext
252+
{
253+
}

0 commit comments

Comments
 (0)