Skip to content

Commit ff40d26

Browse files
committed
Add tests for the Giraffe router named parameters
1 parent 04d4b5e commit ff40d26

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

tests/Giraffe.Tests/RoutingTests.fs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,50 @@ let ``routeStartsWith(f|Cif)`` (uri: string, expected: string) =
429429
| Some ctx -> Assert.Equal(expected, getBody ctx)
430430
}
431431

432+
[<Theory>]
433+
[<InlineData("/pet/123", "PetId: 123")>]
434+
[<InlineData("/name/Fluffy", "Name: Fluffy")>]
435+
[<InlineData("/weight/45.5", "Weight: 45.500000")>]
436+
[<InlineData("/guid/550e8400-e29b-41d4-a716-446655440000", "Guid: 550e8400-e29b-41d4-a716-446655440000")>]
437+
[<InlineData("/combined/pet/123/name/Fluffy", "PetId: 123, Name: Fluffy")>]
438+
[<InlineData("/combined/pet/123/weight/45.5", "PetId: 123, Weight: 45.500000")>]
439+
[<InlineData("/combined/name/Fluffy/guid/550e8400-e29b-41d4-a716-446655440000",
440+
"Name: Fluffy, Guid: 550e8400-e29b-41d4-a716-446655440000")>]
441+
[<InlineData("/invalid-route", "Not Found")>]
442+
let ``routef works with named parameters`` (uri: string, expected: string) =
443+
let app =
444+
choose [
445+
routef "/pet/%i:petId" (fun (petId: int) -> text (sprintf "PetId: %i" petId))
446+
routef "/name/%s:name" (fun (name: string) -> text (sprintf "Name: %s" name))
447+
routef "/weight/%f:weight" (fun (weight: float) -> text (sprintf "Weight: %f" weight))
448+
routef "/guid/%O" (fun (id: System.Guid) -> text (sprintf "Guid: %O" id))
449+
routef
450+
"/combined/pet/%i:petId/name/%s:name"
451+
(fun (petId: int, name: string) -> text (sprintf "PetId: %i, Name: %s" petId name))
452+
routef
453+
"/combined/pet/%i:petId/weight/%f:weight"
454+
(fun (petId: int, weight: float) -> text (sprintf "PetId: %i, Weight: %f" petId weight))
455+
routef
456+
"/combined/name/%s/guid/%O"
457+
(fun (name: string, id: System.Guid) -> text (sprintf "Name: %s, Guid: %O" name id))
458+
459+
// If none of the routes matched then return a 404
460+
RequestErrors.notFound (text "Not Found")
461+
]
462+
463+
let ctx = Substitute.For<HttpContext>()
464+
ctx.Request.Method.ReturnsForAnyArgs "GET" |> ignore
465+
ctx.Request.Path.ReturnsForAnyArgs(PathString(uri)) |> ignore
466+
ctx.Response.Body <- new MemoryStream()
467+
468+
task {
469+
let! result = app next ctx
470+
471+
match result with
472+
| None -> assertFailf "Result was expected to be %s" expected
473+
| Some ctx -> Assert.Equal(expected, getBody ctx)
474+
}
475+
432476
[<Fact>]
433477
let ``routef: GET "/foo/%O/bar/%O" returns "Guid1: ..., Guid2: ..."`` () =
434478
let ctx = Substitute.For<HttpContext>()

0 commit comments

Comments
 (0)