2021-05-06 17:26:21 +03:00
|
|
|
module ApiRouteTests exposing (..)
|
2021-04-29 21:00:25 +03:00
|
|
|
|
2021-05-06 17:26:21 +03:00
|
|
|
import ApiRoute exposing (..)
|
2021-04-29 21:00:25 +03:00
|
|
|
import Expect
|
2021-04-29 21:15:28 +03:00
|
|
|
import Test exposing (describe, only, 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 ->
|
|
|
|
{ body = "Data for user " ++ userId }
|
|
|
|
)
|
2021-05-01 06:19:39 +03:00
|
|
|
|> capture
|
2021-05-01 02:03:47 +03:00
|
|
|
|> tryMatch "123"
|
|
|
|
|> Expect.equal (Just { body = "Data for user 123" })
|
|
|
|
, test "file with extension" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
|
|
|
{ body = "Data for user " ++ userId }
|
|
|
|
)
|
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 { body = "Data for user 124" })
|
|
|
|
, test "file path with multiple segments" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
|
|
|
{ body = "Data for user " ++ userId }
|
|
|
|
)
|
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 { body = "Data for user 123" })
|
2021-05-06 17:34:51 +03:00
|
|
|
, test "integer matcher" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
|
|
|
{ body = "Data for user " ++ String.fromInt userId }
|
|
|
|
)
|
|
|
|
|> literal "users"
|
|
|
|
|> slash
|
|
|
|
|> int
|
|
|
|
|> literal ".json"
|
|
|
|
|> tryMatch "users/123.json"
|
|
|
|
|> Expect.equal (Just { body = "Data for user 123" })
|
2021-05-01 02:03:47 +03:00
|
|
|
, test "routes" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
|
|
|
{ body = "Data for user " ++ userId }
|
|
|
|
)
|
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"
|
|
|
|
]
|
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
|
|
|
|
(\userName repoName ->
|
|
|
|
{ body = "Data for user" }
|
|
|
|
)
|
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 ->
|
|
|
|
{ body = [ username, repo, branch ] |> String.join " - " }
|
|
|
|
)
|
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
|
|
|
]
|