2021-06-04 04:10:06 +03:00
|
|
|
module ApiRouteTests exposing (all)
|
2021-04-29 21:00:25 +03:00
|
|
|
|
2021-05-06 17:26:21 +03:00
|
|
|
import ApiRoute exposing (..)
|
2023-01-02 01:44:11 +03:00
|
|
|
import BackendTask
|
2021-04-29 21:00:25 +03:00
|
|
|
import Expect
|
2021-06-04 04:45:11 +03:00
|
|
|
import Internal.ApiRoute exposing (tryMatch, withRoutes)
|
2021-12-20 22:26:12 +03:00
|
|
|
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-04-29 21:00:25 +03:00
|
|
|
|
|
|
|
|
2021-06-04 03:59:58 +03:00
|
|
|
all : Test
|
2021-04-29 21:00:25 +03:00
|
|
|
all =
|
|
|
|
describe "api routes"
|
2021-05-01 02:03:47 +03:00
|
|
|
[ test "match top-level file with no extension" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
2021-12-21 18:47:47 +03:00
|
|
|
"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"
|
2021-12-21 18:47:47 +03:00
|
|
|
|> Expect.equal (Just "Data for user 123")
|
2021-05-01 02:03:47 +03:00
|
|
|
, test "file with extension" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
2021-12-21 18:47:47 +03:00
|
|
|
"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"
|
2021-12-21 18:47:47 +03:00
|
|
|
|> Expect.equal (Just "Data for user 124")
|
2021-05-01 02:03:47 +03:00
|
|
|
, test "file path with multiple segments" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
2021-12-21 18:47:47 +03:00
|
|
|
"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"
|
2021-12-21 18:47:47 +03:00
|
|
|
|> 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"
|
2021-12-21 18:47:47 +03:00
|
|
|
|> Expect.equal (Just "Data for user 123")
|
2021-05-01 02:03:47 +03:00
|
|
|
, test "routes" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
2021-12-21 18:47:47 +03:00
|
|
|
"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"
|
|
|
|
]
|
2021-12-20 22:26:12 +03:00
|
|
|
, 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
|
|
|
(\() ->
|
|
|
|
""
|
2022-01-18 07:56:37 +03:00
|
|
|
|> Server.Response.plainText
|
2023-01-02 01:44:11 +03:00
|
|
|
|> BackendTask.succeed
|
2021-12-30 21:30:58 +03:00
|
|
|
)
|
2021-12-21 18:47:47 +03:00
|
|
|
)
|
2021-12-20 22:26:12 +03:00
|
|
|
|> literal "no-dynamic-segments.json"
|
2022-01-28 03:03:42 +03:00
|
|
|
|> serverRender
|
2021-12-21 00:35:27 +03:00
|
|
|
|> Internal.ApiRoute.toPattern
|
2021-12-20 22:26:12 +03:00
|
|
|
|> Expect.equal (Pattern [ Pattern.Literal "no-dynamic-segments.json" ] Pattern.NoPendingSlash)
|
2021-12-21 00:35:27 +03:00
|
|
|
, 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
|
|
|
(\() ->
|
|
|
|
""
|
2022-01-18 07:56:37 +03:00
|
|
|
|> Server.Response.plainText
|
2023-01-02 01:44:11 +03:00
|
|
|
|> BackendTask.succeed
|
2021-12-30 21:30:58 +03:00
|
|
|
)
|
2021-12-21 18:47:47 +03:00
|
|
|
)
|
2022-01-28 03:03:42 +03:00
|
|
|
|> literal "api"
|
|
|
|
|> slash
|
|
|
|
|> literal "stars"
|
|
|
|
|> serverRender
|
2021-12-21 00:35:27 +03:00
|
|
|
|> Internal.ApiRoute.toPattern
|
|
|
|
|> Expect.equal
|
|
|
|
(Pattern
|
|
|
|
[ Pattern.Literal "api"
|
|
|
|
, Pattern.Literal "stars"
|
|
|
|
]
|
|
|
|
Pattern.NoPendingSlash
|
|
|
|
)
|
2021-12-20 22:26:12 +03:00
|
|
|
, test "routes to patterns" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
2023-01-02 01:44:11 +03:00
|
|
|
BackendTask.succeed ("Data for user " ++ userId)
|
2021-12-20 22:26:12 +03:00
|
|
|
)
|
|
|
|
|> literal "users"
|
|
|
|
|> slash
|
|
|
|
|> capture
|
|
|
|
|> literal ".json"
|
2021-12-24 02:01:38 +03:00
|
|
|
|> preRender
|
2021-12-20 22:26:12 +03:00
|
|
|
(\route ->
|
2023-01-02 01:44:11 +03:00
|
|
|
BackendTask.succeed
|
2021-12-20 22:26:12 +03:00
|
|
|
[ route "100"
|
|
|
|
, route "101"
|
|
|
|
]
|
|
|
|
)
|
2021-12-21 00:35:27 +03:00
|
|
|
|> Internal.ApiRoute.toPattern
|
2021-12-20 22:26:12 +03:00
|
|
|
|> 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
|
2021-12-20 22:26:12 +03:00
|
|
|
, Pattern.Literal ".json"
|
|
|
|
, []
|
|
|
|
)
|
|
|
|
]
|
|
|
|
Pattern.NoPendingSlash
|
|
|
|
)
|
|
|
|
]
|
2021-05-01 02:03:47 +03:00
|
|
|
, describe "multi-part"
|
2021-05-01 02:01:18 +03:00
|
|
|
[ test "multi-level routes" <|
|
|
|
|
\() ->
|
2021-05-01 03:04:34 +03:00
|
|
|
succeed
|
2021-06-04 03:14:25 +03:00
|
|
|
(\_ _ ->
|
2021-12-21 18:47:47 +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"
|
2021-05-01 02:01:18 +03:00
|
|
|
|> withRoutes
|
|
|
|
(\a ->
|
2021-05-01 03:03:57 +03:00
|
|
|
[ a "dillonkearns" "elm-pages"
|
|
|
|
, a "dillonkearns" "elm-markdown"
|
|
|
|
]
|
2021-05-01 02:01:18 +03:00
|
|
|
)
|
|
|
|
|> Expect.equal
|
2021-05-01 03:03:57 +03:00
|
|
|
[ "repos/dillonkearns/elm-pages.json"
|
|
|
|
, "repos/dillonkearns/elm-markdown.json"
|
|
|
|
]
|
2021-05-01 02:01:18 +03:00
|
|
|
, test "3-level route" <|
|
|
|
|
\() ->
|
2021-05-01 03:04:34 +03:00
|
|
|
succeed
|
|
|
|
(\username repo branch ->
|
2021-12-21 18:47:47 +03:00
|
|
|
[ 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
|
2021-05-01 02:01:18 +03:00
|
|
|
|> withRoutes
|
|
|
|
(\constructor ->
|
2021-05-01 03:02:15 +03:00
|
|
|
[ constructor "dillonkearns" "elm-pages" "static-files" ]
|
2021-05-01 02:01:18 +03:00
|
|
|
)
|
|
|
|
|> Expect.equal
|
2021-05-01 03:02:15 +03:00
|
|
|
[ "repos/dillonkearns/elm-pages/static-files" ]
|
2021-05-01 02:01:18 +03:00
|
|
|
]
|
2021-04-29 21:00:25 +03:00
|
|
|
]
|