|  | 
|  | 1 | +// Licensed to the .NET Foundation under one or more agreements. | 
|  | 2 | +// The .NET Foundation licenses this file to you under the MIT license. | 
|  | 3 | + | 
|  | 4 | +using Microsoft.AspNetCore.Http; | 
|  | 5 | +using Microsoft.AspNetCore.InternalTesting; | 
|  | 6 | + | 
|  | 7 | +namespace Microsoft.AspNetCore.Routing.Internal; | 
|  | 8 | + | 
|  | 9 | +public partial class RequestDelegateFactoryTests : LoggedTest | 
|  | 10 | +{ | 
|  | 11 | +    public static object[][] ValueTypeReturningDelegates => | 
|  | 12 | +    [ | 
|  | 13 | +        [(Func<HttpContext, int>)((HttpContext httpContext) => 42)], | 
|  | 14 | +        [(Func<HttpContext, char>)((HttpContext httpContext) => 'b')], | 
|  | 15 | +        [(Func<HttpContext, bool>)((HttpContext httpContext) => true)], | 
|  | 16 | +        [(Func<HttpContext, float>)((HttpContext httpContext) => 4.2f)], | 
|  | 17 | +        [(Func<HttpContext, double>)((HttpContext httpContext) => 4.2)], | 
|  | 18 | +        [(Func<HttpContext, decimal>)((HttpContext httpContext) => 4.2m)], | 
|  | 19 | +        [(Func<HttpContext, long>)((HttpContext httpContext) => 42)], | 
|  | 20 | +        [(Func<HttpContext, short>)((HttpContext httpContext) => 42)], | 
|  | 21 | +        [(Func<HttpContext, byte>)((HttpContext httpContext) => 42)], | 
|  | 22 | +        [(Func<HttpContext, uint>)((HttpContext httpContext) => 42)], | 
|  | 23 | +        [(Func<HttpContext, ulong>)((HttpContext httpContext) => 42)], | 
|  | 24 | +        [(Func<HttpContext, ushort>)((HttpContext httpContext) => 42)], | 
|  | 25 | +        [(Func<HttpContext, sbyte>)((HttpContext httpContext) => 42)] | 
|  | 26 | +    ]; | 
|  | 27 | + | 
|  | 28 | +    [Theory] | 
|  | 29 | +    [MemberData(nameof(ValueTypeReturningDelegates))] | 
|  | 30 | +    public void Create_WithEndpointFilterOnBuiltInValueTypeReturningDelegate_Works(Delegate @delegate) | 
|  | 31 | +    { | 
|  | 32 | +        var invokeCount = 0; | 
|  | 33 | + | 
|  | 34 | +        RequestDelegateFactoryOptions options = new() | 
|  | 35 | +        { | 
|  | 36 | +            EndpointBuilder = CreateEndpointBuilderFromFilterFactories( | 
|  | 37 | +            [ | 
|  | 38 | +                (routeHandlerContext, next) => | 
|  | 39 | +                { | 
|  | 40 | +                    invokeCount++; | 
|  | 41 | +                    return next; | 
|  | 42 | +                }, | 
|  | 43 | +                (routeHandlerContext, next) => | 
|  | 44 | +                { | 
|  | 45 | +                    invokeCount++; | 
|  | 46 | +                    return next; | 
|  | 47 | +                }, | 
|  | 48 | +            ]), | 
|  | 49 | +        }; | 
|  | 50 | + | 
|  | 51 | +        var result = RequestDelegateFactory.Create(@delegate, options); | 
|  | 52 | +        Assert.Equal(2, invokeCount); | 
|  | 53 | +    } | 
|  | 54 | + | 
|  | 55 | +    public static object[][] NullableValueTypeReturningDelegates => | 
|  | 56 | +    [ | 
|  | 57 | +        [(Func<HttpContext, int?>)((HttpContext httpContext) => 42)], | 
|  | 58 | +        [(Func<HttpContext, char?>)((HttpContext httpContext) => 'b')], | 
|  | 59 | +        [(Func<HttpContext, bool?>)((HttpContext httpContext) => true)], | 
|  | 60 | +        [(Func<HttpContext, float?>)((HttpContext httpContext) => 4.2f)], | 
|  | 61 | +        [(Func<HttpContext, double?>)((HttpContext httpContext) => 4.2)], | 
|  | 62 | +        [(Func<HttpContext, decimal?>)((HttpContext httpContext) => 4.2m)], | 
|  | 63 | +        [(Func<HttpContext, long?>)((HttpContext httpContext) => 42)], | 
|  | 64 | +        [(Func<HttpContext, short?>)((HttpContext httpContext) => 42)], | 
|  | 65 | +        [(Func<HttpContext, byte?>)((HttpContext httpContext) => 42)], | 
|  | 66 | +        [(Func<HttpContext, uint?>)((HttpContext httpContext) => 42)], | 
|  | 67 | +        [(Func<HttpContext, ulong?>)((HttpContext httpContext) => 42)], | 
|  | 68 | +        [(Func<HttpContext, ushort?>)((HttpContext httpContext) => 42)], | 
|  | 69 | +        [(Func<HttpContext, sbyte?>)((HttpContext httpContext) => 42)] | 
|  | 70 | +    ]; | 
|  | 71 | + | 
|  | 72 | +    [Theory] | 
|  | 73 | +    [MemberData(nameof(NullableValueTypeReturningDelegates))] | 
|  | 74 | +    public void Create_WithEndpointFilterOnNullableBuiltInValueTypeReturningDelegate_Works(Delegate @delegate) | 
|  | 75 | +    { | 
|  | 76 | +        var invokeCount = 0; | 
|  | 77 | + | 
|  | 78 | +        RequestDelegateFactoryOptions options = new() | 
|  | 79 | +        { | 
|  | 80 | +            EndpointBuilder = CreateEndpointBuilderFromFilterFactories( | 
|  | 81 | +            [ | 
|  | 82 | +                (routeHandlerContext, next) => | 
|  | 83 | +                { | 
|  | 84 | +                    invokeCount++; | 
|  | 85 | +                    return next; | 
|  | 86 | +                }, | 
|  | 87 | +                (routeHandlerContext, next) => | 
|  | 88 | +                { | 
|  | 89 | +                    invokeCount++; | 
|  | 90 | +                    return next; | 
|  | 91 | +                }, | 
|  | 92 | +            ]), | 
|  | 93 | +        }; | 
|  | 94 | + | 
|  | 95 | +        var result = RequestDelegateFactory.Create(@delegate, options); | 
|  | 96 | +        Assert.Equal(2, invokeCount); | 
|  | 97 | +    } | 
|  | 98 | +} | 
0 commit comments