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

222 lines
8.6 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 (..)
import DataSource
import Expect
import Internal.ApiRoute exposing (tryMatch, withRoutes)
import Pattern exposing (Pattern(..))
2021-12-30 21:30:58 +03:00
import Server.Request
import ServerResponse
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"
]
, 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
(\() ->
""
|> ServerResponse.stringBody
|> DataSource.succeed
)
)
|> literal "no-dynamic-segments.json"
2021-12-24 00:34:19 +03:00
|> ApiRoute.serverRender
|> Internal.ApiRoute.toPattern
|> Expect.equal (Pattern [ Pattern.Literal "no-dynamic-segments.json" ] Pattern.NoPendingSlash)
, test "two literal segments" <|
\() ->
ApiRoute.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
(\() ->
""
|> ServerResponse.stringBody
|> DataSource.succeed
)
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "stars"
2021-12-24 00:34:19 +03:00
|> ApiRoute.serverRender
|> Internal.ApiRoute.toPattern
|> Expect.equal
(Pattern
[ Pattern.Literal "api"
, Pattern.Literal "stars"
]
Pattern.NoPendingSlash
)
, test "routes to patterns" <|
\() ->
succeed
(\userId ->
DataSource.succeed ("Data for user " ++ userId)
)
|> literal "users"
|> slash
|> capture
|> literal ".json"
|> preRender
(\route ->
DataSource.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
(\() ->
DataSource.succeed ("Data for repo " ++ repo |> ServerResponse.stringBody)
)
2021-12-21 21:02:01 +03:00
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "repo"
|> ApiRoute.slash
|> ApiRoute.capture
|> ApiRoute.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" ]
]
]