diff --git a/tests/Giraffe.Tests/EndpointRoutingTests.fs b/tests/Giraffe.Tests/EndpointRoutingTests.fs index 6a40aa74..cc51a333 100644 --- a/tests/Giraffe.Tests/EndpointRoutingTests.fs +++ b/tests/Giraffe.Tests/EndpointRoutingTests.fs @@ -1,9 +1,13 @@ module Giraffe.Tests.EndpointRoutingTests open System +open System.IO +open System.Collections.Generic open Microsoft.AspNetCore.Builder +open Microsoft.AspNetCore.Http open Microsoft.Extensions.DependencyInjection open Xunit +open NSubstitute open Giraffe open Giraffe.EndpointRouting open System.Net.Http @@ -50,3 +54,110 @@ let ``routef: GET "/try-a-guid/%O" returns "Success: ..." or "Not Found"`` (pote content |> shouldEqual expected } + +[] +[] +[] +[] +[] +let ``routeWithExtensions: GET request returns expected result`` (path: string, expected: string) = + let endpoints = + [ + GET [ + routeWithExtensions "/" [] (text "Hello World") + route "/foo" (text "bar") + routeWithExtensions "/bar" [ AspNetExtension.CacheOutput "nothing" ] (text "baz") + ] + ] + + let notFoundHandler = "Not Found" |> text |> RequestErrors.notFound + + let configureApp (app: IApplicationBuilder) = + app.UseRouting().UseGiraffe(endpoints).UseGiraffe(notFoundHandler) + + let configureServices (services: IServiceCollection) = + services.AddRouting().AddGiraffe() |> ignore + + task { + let request = createRequest HttpMethod.Get path + + let! response = makeRequest (fun () -> configureApp) configureServices () request + + let! content = response |> readText + + content |> shouldEqual expected + } + +[] +[] +[] +[] +[] +let ``routefWithExtensions: GET request returns expected result`` (path: string, expected: string) = + let endpoints = + [ + GET [ + routefWithExtensions "/empty/%i" [] (fun i -> text $"/empty i = {i}") + routef "/normal/%i" (fun i -> text $"/normal i = {i}") + routefWithExtensions + "/cache/%i" + [ AspNetExtension.CacheOutput "nothing" ] + (fun i -> text $"/cache i = {i}") + ] + ] + + let notFoundHandler = "Not Found" |> text |> RequestErrors.notFound + + let configureApp (app: IApplicationBuilder) = + app.UseRouting().UseGiraffe(endpoints).UseGiraffe(notFoundHandler) + + let configureServices (services: IServiceCollection) = + services.AddRouting().AddGiraffe() |> ignore + + task { + let request = createRequest HttpMethod.Get path + + let! response = makeRequest (fun () -> configureApp) configureServices () request + + let! content = response |> readText + + content |> shouldEqual expected + } + +[] +[] +[] +[] +[] +let ``subRouteWithExtensions: GET request returns expected result`` (path: string, expected: string) = + let endpoints = + [ + subRouteWithExtensions "/api" [] [ + GET [ + routefWithExtensions "/foo/%i" [] (fun i -> text $"/foo i = {i}") + routef "/bar/%i" (fun i -> text $"/bar i = {i}") + routefWithExtensions + "/baz/%i" + [ AspNetExtension.CacheOutput "nothing" ] + (fun i -> text $"/baz i = {i}") + ] + ] + ] + + let notFoundHandler = "Not Found" |> text |> RequestErrors.notFound + + let configureApp (app: IApplicationBuilder) = + app.UseRouting().UseGiraffe(endpoints).UseGiraffe(notFoundHandler) + + let configureServices (services: IServiceCollection) = + services.AddRouting().AddGiraffe() |> ignore + + task { + let request = createRequest HttpMethod.Get path + + let! response = makeRequest (fun () -> configureApp) configureServices () request + + let! content = response |> readText + + content |> shouldEqual expected + }