elm-pages-v3-beta/tests/ApiRouteTests.elm

254 lines
9.7 KiB
Elm
Raw Normal View History

2021-06-04 04:10:06 +03:00
module ApiRouteTests exposing (all)
2021-05-06 17:26:21 +03:00
import ApiRoute exposing (..)
2023-01-02 01:44:11 +03:00
import BackendTask
import Expect
import Internal.ApiRoute exposing (tryMatch, withRoutes)
import Pattern exposing (Pattern(..))
2021-12-30 21:30:58 +03:00
import Server.Request
2022-01-02 00:55:37 +03:00
import Server.Response
2021-06-04 03:59:58 +03:00
import Test exposing (Test, describe, test)
2021-06-04 03:59:58 +03:00
all : Test
all =
describe "api routes"
2021-05-01 02:03:47 +03:00
[ test "match top-level file with no extension" <|
\() ->
succeed
(\userId ->
"Data for user " ++ userId
2021-05-01 02:03:47 +03:00
)
2021-05-01 06:19:39 +03:00
|> capture
2021-05-01 02:03:47 +03:00
|> tryMatch "123"
|> Expect.equal (Just "Data for user 123")
2021-05-01 02:03:47 +03:00
, test "file with extension" <|
\() ->
succeed
(\userId ->
"Data for user " ++ userId
2021-05-01 02:03:47 +03:00
)
2021-05-01 06:19:39 +03:00
|> capture
|> literal ".json"
2021-05-01 02:03:47 +03:00
|> tryMatch "124.json"
|> Expect.equal (Just "Data for user 124")
2021-05-01 02:03:47 +03:00
, test "file path with multiple segments" <|
\() ->
succeed
(\userId ->
"Data for user " ++ userId
2021-05-01 02:03:47 +03:00
)
2021-05-01 06:19:39 +03:00
|> literal "users"
2021-05-01 02:03:47 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
|> literal ".json"
2021-05-01 02:03:47 +03:00
|> tryMatch "users/123.json"
|> Expect.equal (Just "Data for user 123")
2021-05-06 17:34:51 +03:00
, test "integer matcher" <|
\() ->
succeed
(\userId ->
2021-12-24 02:14:36 +03:00
"Data for user " ++ userId
2021-05-06 17:34:51 +03:00
)
|> literal "users"
|> slash
2021-12-24 02:14:36 +03:00
|> capture
2021-05-06 17:34:51 +03:00
|> literal ".json"
|> tryMatch "users/123.json"
|> Expect.equal (Just "Data for user 123")
2021-05-01 02:03:47 +03:00
, test "routes" <|
\() ->
succeed
(\userId ->
"Data for user " ++ userId
2021-05-01 02:03:47 +03:00
)
2021-05-01 06:19:39 +03:00
|> literal "users"
2021-05-01 02:03:47 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
|> literal ".json"
2021-05-01 02:03:47 +03:00
|> withRoutes
(\constructor ->
2021-05-01 03:03:57 +03:00
[ constructor "100"
, constructor "101"
]
2021-05-01 02:03:47 +03:00
)
|> Expect.equal
2021-05-01 03:03:57 +03:00
[ "users/100.json"
, "users/101.json"
]
2022-02-07 19:59:59 +03:00
, test "matches with two dynamic segments" <|
\() ->
succeed
(\author name ->
author ++ " - " ++ name
)
|> literal "package"
|> slash
|> capture
|> slash
|> capture
|> tryMatch "package/dillonkearns/elm-pages"
|> Expect.equal (Just "dillonkearns - elm-pages")
, test "routes with two dynamic segments" <|
\() ->
succeed
(\author name ->
author ++ " - " ++ name
)
|> literal "package"
|> slash
|> capture
|> slash
|> capture
|> withRoutes
(\constructor ->
[ constructor "dillonkearns" "elm-pages"
]
)
|> Expect.equal
[ "package/dillonkearns/elm-pages"
]
, describe "toPattern"
[ test "no dynamic segments" <|
\() ->
succeed
2021-12-30 21:30:58 +03:00
(Server.Request.succeed ()
2021-12-31 22:22:19 +03:00
|> Server.Request.map
2021-12-30 21:30:58 +03:00
(\() ->
""
|> Server.Response.plainText
2023-01-02 01:44:11 +03:00
|> BackendTask.succeed
2021-12-30 21:30:58 +03:00
)
)
|> literal "no-dynamic-segments.json"
2022-01-28 03:03:42 +03:00
|> serverRender
|> Internal.ApiRoute.toPattern
|> Expect.equal (Pattern [ Pattern.Literal "no-dynamic-segments.json" ] Pattern.NoPendingSlash)
, test "two literal segments" <|
\() ->
2022-01-28 03:03:42 +03:00
succeed
2021-12-30 21:30:58 +03:00
(Server.Request.succeed ()
2021-12-31 22:22:19 +03:00
|> Server.Request.map
2021-12-30 21:30:58 +03:00
(\() ->
""
|> Server.Response.plainText
2023-01-02 01:44:11 +03:00
|> BackendTask.succeed
2021-12-30 21:30:58 +03:00
)
)
2022-01-28 03:03:42 +03:00
|> literal "api"
|> slash
|> literal "stars"
|> serverRender
|> Internal.ApiRoute.toPattern
|> Expect.equal
(Pattern
[ Pattern.Literal "api"
, Pattern.Literal "stars"
]
Pattern.NoPendingSlash
)
, test "routes to patterns" <|
\() ->
succeed
(\userId ->
2023-01-02 01:44:11 +03:00
BackendTask.succeed ("Data for user " ++ userId)
)
|> literal "users"
|> slash
|> capture
|> literal ".json"
|> preRender
(\route ->
2023-01-02 01:44:11 +03:00
BackendTask.succeed
[ route "100"
, route "101"
]
)
|> Internal.ApiRoute.toPattern
|> Expect.equal
(Pattern
[ Pattern.Literal "users"
, Pattern.HybridSegment
( Pattern.Dynamic
2021-12-21 21:02:01 +03:00
, Pattern.Literal ".json"
, []
)
]
Pattern.NoPendingSlash
)
, test "hybrid route with multiple static segments" <|
\() ->
succeed
2021-12-30 21:30:58 +03:00
(\repo ->
Server.Request.succeed ()
2021-12-31 22:22:19 +03:00
|> Server.Request.map
2021-12-30 21:30:58 +03:00
(\() ->
2023-01-02 01:44:11 +03:00
BackendTask.succeed ("Data for repo " ++ repo |> Server.Response.plainText)
2021-12-30 21:30:58 +03:00
)
2021-12-21 21:02:01 +03:00
)
2022-01-28 03:03:42 +03:00
|> literal "api"
|> slash
|> literal "repo"
|> slash
|> capture
|> literal ".json"
2021-12-24 00:34:19 +03:00
|> serverRender
2021-12-21 21:02:01 +03:00
|> Internal.ApiRoute.toPattern
|> Expect.equal
(Pattern
[ Pattern.Literal "api"
, Pattern.Literal "repo"
, Pattern.HybridSegment
( Pattern.Dynamic
, Pattern.Literal ".json"
, []
)
]
Pattern.NoPendingSlash
)
]
2021-05-01 02:03:47 +03:00
, describe "multi-part"
[ test "multi-level routes" <|
\() ->
2021-05-01 03:04:34 +03:00
succeed
2021-06-04 03:14:25 +03:00
(\_ _ ->
"Data for user"
2021-05-01 03:04:34 +03:00
)
2021-05-01 06:19:39 +03:00
|> literal "repos"
2021-05-01 03:04:34 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
2021-05-01 03:04:34 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
|> literal ".json"
|> withRoutes
(\a ->
2021-05-01 03:03:57 +03:00
[ a "dillonkearns" "elm-pages"
, a "dillonkearns" "elm-markdown"
]
)
|> Expect.equal
2021-05-01 03:03:57 +03:00
[ "repos/dillonkearns/elm-pages.json"
, "repos/dillonkearns/elm-markdown.json"
]
, test "3-level route" <|
\() ->
2021-05-01 03:04:34 +03:00
succeed
(\username repo branch ->
[ username, repo, branch ] |> String.join " - "
2021-05-01 03:04:34 +03:00
)
2021-05-01 06:19:39 +03:00
|> literal "repos"
2021-05-01 03:04:34 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
2021-05-01 03:04:34 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
2021-05-01 03:04:34 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
|> withRoutes
(\constructor ->
[ constructor "dillonkearns" "elm-pages" "static-files" ]
)
|> Expect.equal
[ "repos/dillonkearns/elm-pages/static-files" ]
]
]