2021-04-29 21:00:25 +03:00
|
|
|
module ApiHandlerTests exposing (..)
|
|
|
|
|
2021-04-30 18:42:07 +03:00
|
|
|
import ApiHandler 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 }
|
|
|
|
)
|
|
|
|
|> captureNew
|
|
|
|
|> tryMatch "123"
|
|
|
|
|> Expect.equal (Just { body = "Data for user 123" })
|
|
|
|
, test "file with extension" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
|
|
|
{ body = "Data for user " ++ userId }
|
|
|
|
)
|
|
|
|
|> captureNew
|
|
|
|
|> literalSegment ".json"
|
|
|
|
|> tryMatch "124.json"
|
|
|
|
|> Expect.equal (Just { body = "Data for user 124" })
|
|
|
|
, test "file path with multiple segments" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
|
|
|
{ body = "Data for user " ++ userId }
|
|
|
|
)
|
|
|
|
|> literalSegment "users"
|
|
|
|
|> slash
|
|
|
|
|> captureNew
|
|
|
|
|> literalSegment ".json"
|
|
|
|
|> tryMatch "users/123.json"
|
|
|
|
|> Expect.equal (Just { body = "Data for user 123" })
|
|
|
|
, test "routes" <|
|
|
|
|
\() ->
|
|
|
|
succeed
|
|
|
|
(\userId ->
|
|
|
|
{ body = "Data for user " ++ userId }
|
|
|
|
)
|
|
|
|
|> literalSegment "users"
|
|
|
|
|> slash
|
|
|
|
|> captureNew
|
|
|
|
|> literalSegment ".json"
|
|
|
|
|> withRoutes
|
|
|
|
(\constructor ->
|
2021-05-01 03:02:15 +03:00
|
|
|
[ constructor "100" ]
|
2021-05-01 02:03:47 +03:00
|
|
|
-- [
|
|
|
|
--, constructor "101"
|
|
|
|
--]
|
|
|
|
)
|
|
|
|
|> Expect.equal
|
2021-05-01 03:02:15 +03:00
|
|
|
[ "users/100.json" ]
|
2021-05-01 02:03:47 +03:00
|
|
|
|
|
|
|
--, "users/101.json"
|
|
|
|
, describe "multi-part"
|
2021-05-01 02:01:18 +03:00
|
|
|
[ test "multi-level routes" <|
|
|
|
|
\() ->
|
|
|
|
newThing
|
|
|
|
|> withRoutes
|
|
|
|
(\a ->
|
|
|
|
--constructor "dillonkearns" "elm-pages"
|
|
|
|
--, constructor "101"
|
2021-05-01 03:02:15 +03:00
|
|
|
[ a "dillonkearns" "elm-pages" ]
|
2021-05-01 02:01:18 +03:00
|
|
|
--, constructor "elm-pages"
|
|
|
|
--]
|
|
|
|
)
|
|
|
|
|> Expect.equal
|
2021-05-01 03:02:15 +03:00
|
|
|
[ "repos/dillonkearns/elm-pages.json" ]
|
2021-04-30 23:54:03 +03:00
|
|
|
|
2021-05-01 02:01:18 +03:00
|
|
|
--, "users/101.json"
|
|
|
|
, test "3-level route" <|
|
|
|
|
\() ->
|
|
|
|
threeParts
|
|
|
|
|> 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
|
|
|
|
|
|
|
--, "users/101.json"
|
|
|
|
]
|
2021-04-29 21:00:25 +03:00
|
|
|
]
|
2021-04-30 23:54:03 +03:00
|
|
|
|
|
|
|
|
2021-05-01 02:01:18 +03:00
|
|
|
newThing : Handler Response (String -> String -> List String)
|
2021-04-30 23:54:03 +03:00
|
|
|
newThing =
|
2021-05-01 02:03:47 +03:00
|
|
|
succeed
|
2021-04-30 23:54:03 +03:00
|
|
|
(\userName repoName ->
|
|
|
|
{ body = "Data for user" }
|
|
|
|
)
|
|
|
|
|> literalSegment "repos"
|
|
|
|
|> slash
|
|
|
|
|> captureNew
|
|
|
|
|> slash
|
|
|
|
|> captureNew
|
2021-05-01 02:01:18 +03:00
|
|
|
|> literalSegment ".json"
|
2021-04-30 23:54:03 +03:00
|
|
|
|
|
|
|
|
2021-05-01 02:01:18 +03:00
|
|
|
threeParts : Handler Response (String -> String -> String -> List String)
|
2021-04-30 23:54:03 +03:00
|
|
|
threeParts =
|
2021-05-01 02:03:47 +03:00
|
|
|
succeed
|
2021-04-30 23:54:03 +03:00
|
|
|
(\username repo branch ->
|
|
|
|
{ body = [ username, repo, branch ] |> String.join " - " }
|
|
|
|
)
|
|
|
|
|> literalSegment "repos"
|
|
|
|
|> slash
|
|
|
|
|> captureNew
|
|
|
|
|> slash
|
|
|
|
|> captureNew
|
|
|
|
|> slash
|
|
|
|
|> captureNew
|