Skip to content

Commit

Permalink
add Endpoint routing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
64J0 committed Dec 19, 2024
1 parent 9a81c8c commit dc8fbd3
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions tests/Giraffe.Tests/EndpointRoutingTests.fs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -50,3 +54,110 @@ let ``routef: GET "/try-a-guid/%O" returns "Success: ..." or "Not Found"`` (pote

content |> shouldEqual expected
}

[<Theory>]
[<InlineData("/", "Hello World")>]
[<InlineData("/foo", "bar")>]
[<InlineData("/bar", "baz")>]
[<InlineData("/NOT-EXIST", "Not Found")>]
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
}

[<Theory>]
[<InlineData("/empty/5", "/empty i = 5")>]
[<InlineData("/normal/10", "/normal i = 10")>]
[<InlineData("/cache/25", "/cache i = 25")>]
[<InlineData("/NOT-EXIST", "Not Found")>]
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
}

[<Theory>]
[<InlineData("/api/foo/5", "/foo i = 5")>]
[<InlineData("/api/bar/10", "/bar i = 10")>]
[<InlineData("/api/baz/25", "/baz i = 25")>]
[<InlineData("/NOT-EXIST", "Not Found")>]
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
}

0 comments on commit dc8fbd3

Please sign in to comment.