File tree 1 file changed +31
-0
lines changed
src/Http/Routing/test/UnitTests/Builder
1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -257,6 +257,37 @@ public async Task RequestFilters_CanAssertOnEmptyResult()
257
257
Assert . Same ( Results . Empty , response ) ;
258
258
}
259
259
260
+ [ Fact ]
261
+ public async Task RequestFilters_ReturnValue_SerializeJson ( )
262
+ {
263
+ var builder = new DefaultEndpointRouteBuilder ( new ApplicationBuilder ( EmptyServiceProvider . Instance ) ) ;
264
+ var httpContext = new DefaultHttpContext ( ) ;
265
+ var responseBodyStream = new MemoryStream ( ) ;
266
+ httpContext . Response . Body = responseBodyStream ;
267
+
268
+ var @delegate = ( HttpContext context ) => Task . CompletedTask ;
269
+
270
+ var endpointBuilder = builder . Map ( "/" , @delegate )
271
+ . AddEndpointFilterFactory ( filterFactory : ( routeHandlerContext , next ) =>
272
+ {
273
+ return async invocationContext =>
274
+ {
275
+ await next ( invocationContext ) ;
276
+ return new MyCoolType ( Name : "Jason" ) ; // serialized as JSON
277
+ } ;
278
+ } ) ;
279
+
280
+ var dataSource = GetBuilderEndpointDataSource ( builder ) ;
281
+ var endpoint = Assert . Single ( dataSource . Endpoints ) ;
282
+
283
+ await endpoint . RequestDelegate ! ( httpContext ) ;
284
+
285
+ var responseBody = Encoding . UTF8 . GetString ( responseBodyStream . ToArray ( ) ) ;
286
+ Assert . Equal ( @"{""name"":""Jason""}" , responseBody ) ;
287
+ }
288
+
289
+ private record struct MyCoolType ( string Name ) ;
290
+
260
291
private sealed class HttpContextArgFilter : IEndpointFilter
261
292
{
262
293
private readonly string _name ;
You can’t perform that action at this time.
0 commit comments