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

Fail on second router #384

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 11 additions & 2 deletions src/Saturn/Application.fs
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,28 @@ module Application =
)

///Defines top-level router used for the application
///
///This can only be called once, and not after `no_router`!
[<CustomOperation("use_router")>]
member __.Router(state, handler) =
{state with Router = Some handler}
match state.NoRouter, state.Router with
| false, None -> {state with Router = Some handler}
| true, _ -> failwith "Cannot add a router, after `no_router` was set!"
| _, Some _ -> failwith "Cannot add a second router!"

///Defines top-level endpoint router used for the application
[<CustomOperation("use_endpoint_router")>]
member __.EndpointRouter(state, routes) =
{state with EndpointRouter = Some routes}

///Disable warning message about lack of `router` definition. Should be used for channels-only or gRPC applications.
///
///This cannot be called after `use_router`!
[<CustomOperation("no_router")>]
member __.NoRouter(state) =
{state with NoRouter = true}
match state.Router with
| Some _ -> failwith "Cannot set `no_router` after a router with `use_router` has been set!"
| _ -> {state with NoRouter = true}

///Disables any configuration of webhost. Could be used for generic `IHostBuilder` applications not using Kestrel/IIS
[<CustomOperation("no_webhost")>]
Expand Down
34 changes: 34 additions & 0 deletions tests/Saturn.UnitTests/SimpleTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,37 @@ let tests =
Expect.equal (getBody ctx) """{"id":"myId","links":["myLink1","myLink2"]}""" "Result should be equal"

]

//---------------------------`Application only takes one router` tests----------------------------------------

[<Tests>]
let routerTests =
testList "Application only takes one router" [
testCase "Second router throws" (fun _ ->
let app () =
application {
use_router (text "")
use_router (text "")
}

Expect.throws (app >> ignore) "Application did not fail on second router!"
)
testCase "Adding a router after `no_router` throws" (fun _ ->
let app () =
application {
no_router
use_router (text "")
}

Expect.throws (app >> ignore) "Application did not fail on router after no_router!"
)
testCase "Adding a `no_router after `use_router` throws" (fun _ ->
let app () =
application {
use_router (text "")
no_router
}

Expect.throws (app >> ignore) "Application did not fail on no_router after use_router!"
)
]
Loading