44using System ;
55using System . Collections . Generic ;
66using System . ComponentModel . DataAnnotations ;
7+ using System . Net . Http ;
8+ using System . Text . Json . Serialization ;
79using Microsoft . AspNetCore . Builder ;
810using Microsoft . AspNetCore . Http ;
11+ using Microsoft . AspNetCore . Hosting ;
912using Microsoft . Extensions . DependencyInjection ;
1013using Microsoft . Extensions . Validation ;
1114
1215var builder = WebApplication . CreateSlimBuilder ( ) ;
1316
1417builder . 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
1624var app = builder . Build ( ) ;
1725
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
47115return 100 ;
48116
@@ -163,4 +231,23 @@ public record AddressResponse(string Message, Address Address);
163231
164232public 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