Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more test scenarios for the router feature #355

Merged
merged 1 commit into from
Sep 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions tests/Saturn.UnitTests/RouterTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ open Giraffe
let testRouter = router {
get "/1" (text "HelloWorld")
get "/post/1" (text "Foo")
getf "/user/%i" (fun (userId: int32) -> (sprintf "User Id: %i" userId) |> text)
getf "/isbn/%d" (fun (bookRef: int64) -> (sprintf "Book ISBN: %d" bookRef) |> text)
post "/post/1" (text "1")
post "/post/2" (text "2")
put "/user/123" (text "User updated!")
delete "/user/123" (text "User deleted!")
}

[<Tests>]
Expand Down Expand Up @@ -54,7 +58,45 @@ let tests =
| Some ctx ->
Expect.equal (getBody ctx) expected "Result should be equal"

testCase "GET to `/user/18` returns `User Id: 18`" <| fun _ ->
let ctx = getEmptyContext "GET" "/user/18"

let expected = "User Id: 18"
let result = testRouter next ctx |> runTask
match result with
| None -> failtestf "Result was expected to be %s" expected
| Some ctx ->
Expect.equal (getBody ctx) expected "Result should be equal"

testCase "GET to `/isbn/9781491951170` returns `Book ISNB: 9781491951170`" <| fun _ ->
let ctx = getEmptyContext "GET" "/isbn/9781491951170"

let expected = "Book ISBN: 9781491951170"
let result = testRouter next ctx |> runTask
match result with
| None -> failtestf "Result was expected to be %s" expected
| Some ctx ->
Expect.equal (getBody ctx) expected "Result should be equal"

testCase "PUT to `/user/123` returns `User updated!`" <| fun _ ->
let ctx = getEmptyContext "PUT" "/user/123"

let expected = "User updated!"
let result = testRouter next ctx |> runTask
match result with
| None -> failtestf "Result was expected to be %s" expected
| Some ctx ->
Expect.equal (getBody ctx) expected "Result should be equal"

testCase "DELETE to `/user/123` returns `User deleted!`" <| fun _ ->
let ctx = getEmptyContext "DELETE" "/user/123"

let expected = "User deleted!"
let result = testRouter next ctx |> runTask
match result with
| None -> failtestf "Result was expected to be %s" expected
| Some ctx ->
Expect.equal (getBody ctx) expected "Result should be equal"
]

let testCiRouter = router {
Expand Down