giraffe-fsharp / Giraffe

A native functional ASP.NET Core web framework for F# developers.
https://giraffe.wiki
Apache License 2.0
2.13k stars 266 forks source link

Fix EndpointRouting Guid regex + tests #594

Closed 64J0 closed 6 months ago

64J0 commented 6 months ago

Description

With this PR, I'm fixing the EndpointRouting Guid regex (route constraint) and adding more automated tests to assert that it's working.

How to test

You can use this project example:

open System
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Hosting
open Giraffe
open Giraffe.EndpointRouting

let endpoints =
    [ GET
          [ route "/" (text "Hello World")
            routef "/try-a-guid/%O" (fun (guid: Guid) -> text $"Success: {guid}") ] ]

let notFoundHandler = "Not Found" |> text |> RequestErrors.notFound

let configureApp (appBuilder: IApplicationBuilder) =
    appBuilder.UseRouting().UseGiraffe(endpoints).UseGiraffe(notFoundHandler)

let configureServices (services: IServiceCollection) =
    services.AddRouting().AddGiraffe() |> ignore

[<EntryPoint>]
let main args =
    let builder = WebApplication.CreateBuilder(args)
    configureServices builder.Services

    let app = builder.Build()

    configureApp app
    app.Run()

    0

And test with some inputs. For example:

# must not find
curl http://localhost:5000/try-a-guid/8b3557db-fa-c0c90785ec0b

# must work fine
curl http://localhost:5000/try-a-guid/8b3557db-fa-c0c90785ec
# ...

You can find more Guid ideas at the automated tests file.

Related issues