How would I test IHP my CanRoute instance (parse route for beautiful URLs)?

Solution 1:

The parseRoute' function of the CanRoute we define for custom routes is basically just returning an attoparsec parser.

We can use attoparsecs parseOnly to run the parser with a provided input string. Here's an example:

module Test.Web.RoutesSpec where

import Test.Hspec
import IHP.ControllerPrelude
import IHP.Test.Mocking
import qualified Data.Attoparsec.ByteString as Attoparsec
import Web.Types
import Web.FrontController

tests = beforeAll (mockContextNoDatabase WebApplication (pure ())) do
    describe "Web.Routes" do
        describe "DocumentationController" do
            it "should be available at /docs" $ withContext do
                "/docs/api-reference/0bca60db-571e-4cdd-b02a-8d5b9e7e6295" `shouldRouteTo` DocumentationAction { projectId = "0bca60db-571e-4cdd-b02a-8d5b9e7e6295" }

shouldRouteTo path action = (Attoparsec.parseOnly parseRoute' path) `shouldBe` (Right action)