Skip to content

Commit

Permalink
Fix EndpointRouting Guid regex + tests (#594)
Browse files Browse the repository at this point in the history
* fix: guidPattern regex endpoint routing template

* tests: add new tests to EndpointRouting

* chore: improve test description

* tests: add ShortGuid tests

* chore: add comment explaining why we use the regex to filter guid routes on %O
  • Loading branch information
64J0 authored May 12, 2024
1 parent e4e89a2 commit 69c92e6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Giraffe/EndpointRouting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ open FSharp.Core
open Giraffe

module private RouteTemplateBuilder =
// We use this regex route constraint to be compatible with Giraffe's default router,
// which supports ShortGuid's.
// More information on ASP.NET route constraints:
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-8.0#route-constraints
let private guidPattern =
"([0-9A-Fa-f]{{8}}\-[0-9A-Fa-f]{{4}}\-[0-9A-Fa-f]{{4}}\-[0-9A-Fa-f]{{4}}\-[0-9A-Fa-f]{{12}}|[0-9A-Fa-f]{{32}}|[-_0-9A-Za-z]{{22}})"
"^[0-9A-Fa-f]{{8}}-[0-9A-Fa-f]{{4}}-[0-9A-Fa-f]{{4}}-[0-9A-Fa-f]{{4}}-[0-9A-Fa-f]{{12}}$|^[0-9A-Fa-f]{{32}}$|^[-_0-9A-Za-z]{{22}}$"
let private shortIdPattern =
"([-_0-9A-Za-z]{{10}}[048AEIMQUYcgkosw])"

Expand Down
54 changes: 54 additions & 0 deletions tests/Giraffe.Tests/EndpointRoutingTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Giraffe.Tests.EndpointRoutingTests

open System
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.DependencyInjection
open Xunit
open Giraffe
open Giraffe.EndpointRouting
open System.Net.Http

// ---------------------------------
// routef Tests
// ---------------------------------

[<Theory>]
[<InlineData("00000000-0000-0000-0000-0000000000000", "Not Found")>]
[<InlineData("00000000-0000-0000-0000-000000000000", "Success: 00000000-0000-0000-0000-000000000000")>]
[<InlineData("00000000-0000-0000-0000-00000000000", "Not Found")>]
[<InlineData("0000000000000000000000", "Success: d3344dd3-344d-4dd3-34d3-4d34d34d34d3")>] // ShortGuid
[<InlineData("DBvYFN7y#$@u933Kc8pM#^", "Not Found")>]
[<InlineData("8b3557db-fa-c0c90785ec0b", "Not Found")>]
[<InlineData("8b3557db-fa-c0c90785ec", "Success: e7f9bdf1-5bb7-f6f9-be73-473dd3bf3979")>] // ShortGuid
[<InlineData("8b3557db-fa-c0c90785e", "Not Found")>]
[<InlineData("does-not-make-sense", "Not Found")>]
[<InlineData("1", "Not Found")>]
let ``routef: GET "/try-a-guid/%O" returns "Success: ..." or "Not Found"`` (potentialGuid: string, expected: string) =
task {
let endpoints: Endpoint list =
[ GET
[ route "/" (text "Hello World")
route "/foo" (text "bar")
routef "/try-a-guid/%O" (fun (guid: Guid) -> text $"Success: {guid}") ] ]

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

let request =
createRequest HttpMethod.Get $"/try-a-guid/{potentialGuid}"

let! response =
makeRequest (fun () -> configureApp) configureServices () request

let! content =
response |> readText

content |> shouldEqual expected
}
1 change: 1 addition & 0 deletions tests/Giraffe.Tests/Giraffe.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Compile Include="FormatExpressionTests.fs" />
<Compile Include="HttpHandlerTests.fs" />
<Compile Include="RoutingTests.fs" />
<Compile Include="EndpointRoutingTests.fs" />
<Compile Include="AuthTests.fs" />
<Compile Include="ModelBindingTests.fs" />
<Compile Include="ModelValidationTests.fs" />
Expand Down

0 comments on commit 69c92e6

Please sign in to comment.