From 1c27657b1144bed01f784d23d6819e53d557a558 Mon Sep 17 00:00:00 2001 From: Christian Steinert Date: Fri, 31 May 2024 21:05:10 +0200 Subject: [PATCH 1/3] Add tests for #323. --- tests/Saturn.UnitTests/SimpleTests.fs | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/Saturn.UnitTests/SimpleTests.fs b/tests/Saturn.UnitTests/SimpleTests.fs index 159058aa..a61bf913 100644 --- a/tests/Saturn.UnitTests/SimpleTests.fs +++ b/tests/Saturn.UnitTests/SimpleTests.fs @@ -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---------------------------------------- + +[] +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!" + ) + ] From 7da37c76d1209cb4918447f2093172c3169f2355 Mon Sep 17 00:00:00 2001 From: Christian Steinert Date: Fri, 31 May 2024 21:08:40 +0200 Subject: [PATCH 2/3] Application-CE: Throw on multipe `*_router` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a check when `use_router` is called twice, or `use_router` after `no_router` or the other way around, and fail directly. This is to prohibit setting two routers, which does not have any effect… only the last router is used, which might be confusing. Fixes #323. --- src/Saturn/Application.fs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Saturn/Application.fs b/src/Saturn/Application.fs index 72d539c3..e820329f 100644 --- a/src/Saturn/Application.fs +++ b/src/Saturn/Application.fs @@ -226,7 +226,10 @@ module Application = ///Defines top-level router used for the application [] 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 [] @@ -236,7 +239,9 @@ module Application = ///Disable warning message about lack of `router` definition. Should be used for channels-only or gRPC applications. [] 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 [] From 5c09622b9f705340ec270f322853282590a07360 Mon Sep 17 00:00:00 2001 From: Christian Steinert Date: Fri, 31 May 2024 21:19:34 +0200 Subject: [PATCH 3/3] Application-CE: XML Doc Warning Add a warning in the XML doc about the new throws. --- src/Saturn/Application.fs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Saturn/Application.fs b/src/Saturn/Application.fs index e820329f..01091f4b 100644 --- a/src/Saturn/Application.fs +++ b/src/Saturn/Application.fs @@ -224,6 +224,8 @@ module Application = ) ///Defines top-level router used for the application + /// + ///This can only be called once, and not after `no_router`! [] member __.Router(state, handler) = match state.NoRouter, state.Router with @@ -237,6 +239,8 @@ module Application = {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`! [] member __.NoRouter(state) = match state.Router with