diff --git a/src/Saturn/Router.fs b/src/Saturn/Router.fs index 10ca8118..15e9d2d7 100644 --- a/src/Saturn/Router.fs +++ b/src/Saturn/Router.fs @@ -207,33 +207,33 @@ module Router = let lst = choose [ - for e in gets do - yield GET >=> e for e in getsf do yield GET >=> e + for e in gets do + yield GET >=> e - for e in posts do - yield POST >=> e for e in postsf do yield POST >=> e + for e in posts do + yield POST >=> e - for e in patches do - yield PATCH >=> e for e in patchesf do yield PATCH >=> e + for e in patches do + yield PATCH >=> e - for e in puts do - yield PUT >=> e for e in putsf do yield PUT >=> e + for e in puts do + yield PUT >=> e - for e in deletes do - yield DELETE >=> e for e in deletesf do yield DELETE >=> e + for e in deletes do + yield DELETE >=> e - yield! forwards yield! forwardsf + yield! forwards if state.NotFoundHandler.IsSome then siteMap.NotFound () yield state.NotFoundHandler.Value diff --git a/tests/Saturn.UnitTests/RouterTests.fs b/tests/Saturn.UnitTests/RouterTests.fs index f159a5cc..8c1256d8 100644 --- a/tests/Saturn.UnitTests/RouterTests.fs +++ b/tests/Saturn.UnitTests/RouterTests.fs @@ -111,3 +111,40 @@ let caseInsensitiveTest = ] + +let rootId = System.Guid.NewGuid() +let root = "roots" + +let testForwardingRouter = + router { + forward $"/{root}" (fun next context -> + text $"{root}" next context + ) + forwardf "/roots/%O" (fun id next context -> + text (sprintf "roots/%O" id) next context + ) + } + +[] +let forwardingTests = + testList "Common root forwarding tests" [ + testCase "FORWARD to `/ROOTS` returns `roots`" <| fun _ -> + let ctx = getEmptyContext "FORWARD" $"/{root}" + let expected = root + let result = testForwardingRouter next ctx |> runTask + match result with + | None -> failtestf "Result was expected to be %s" expected + | Some ctx -> + let body = (getBody ctx) + Expect.equal body expected "Result should be equal" + + testCase $"FORWARD to `/ROOTS/{rootId}` returns `roots/{rootId}`" <| fun _ -> + let ctx = getEmptyContext "FORWARD" $"/{root}/{rootId}" + let expected = $"{root}/{rootId}" + let result = testForwardingRouter next ctx |> runTask + match result with + | None -> failtestf "Result was expected to be %s" expected + | Some ctx -> + let body = (getBody ctx) + Expect.equal body expected "Result should be equal" + ]