2019-10-31 06:11:24 +03:00
|
|
|
module StaticHttpUnitTests exposing (all)
|
|
|
|
|
2021-04-20 17:31:19 +03:00
|
|
|
import DataSource
|
2021-04-20 23:52:54 +03:00
|
|
|
import DataSource.Http
|
2020-12-07 19:41:10 +03:00
|
|
|
import Dict
|
2019-10-31 06:11:24 +03:00
|
|
|
import Expect
|
2022-01-28 07:43:16 +03:00
|
|
|
import Json.Decode as Decode
|
2020-01-03 23:18:18 +03:00
|
|
|
import Pages.StaticHttp.Request as Request
|
2019-10-31 06:11:24 +03:00
|
|
|
import Pages.StaticHttpRequest as StaticHttpRequest
|
2020-12-07 19:41:10 +03:00
|
|
|
import Test exposing (Test, describe, test)
|
2019-10-31 06:11:24 +03:00
|
|
|
|
|
|
|
|
2021-06-04 03:59:58 +03:00
|
|
|
getWithoutSecrets : String -> Decode.Decoder a -> DataSource.DataSource a
|
2019-11-13 05:22:36 +03:00
|
|
|
getWithoutSecrets url =
|
2022-01-28 07:43:16 +03:00
|
|
|
DataSource.Http.get url
|
2019-11-13 05:22:36 +03:00
|
|
|
|
|
|
|
|
2021-06-04 03:59:58 +03:00
|
|
|
requestsDict : List ( Request.Request, b ) -> Dict.Dict String (Maybe b)
|
2020-01-03 23:18:18 +03:00
|
|
|
requestsDict requestMap =
|
|
|
|
requestMap
|
|
|
|
|> List.map
|
|
|
|
(\( request, response ) ->
|
|
|
|
( request |> Request.hash
|
2020-10-06 06:35:15 +03:00
|
|
|
, Just response
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|> Dict.fromList
|
|
|
|
|
|
|
|
|
|
|
|
get : String -> Request.Request
|
|
|
|
get url =
|
|
|
|
{ method = "GET"
|
|
|
|
, url = url
|
|
|
|
, headers = []
|
2021-07-30 01:57:44 +03:00
|
|
|
, body = DataSource.Http.emptyBody
|
2020-01-03 23:18:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-31 06:11:24 +03:00
|
|
|
all : Test
|
|
|
|
all =
|
2021-04-01 05:55:28 +03:00
|
|
|
describe "Static Http Requests unit tests"
|
2019-10-31 06:11:24 +03:00
|
|
|
[ test "andThen" <|
|
|
|
|
\() ->
|
2022-01-28 07:43:16 +03:00
|
|
|
DataSource.Http.get "first" (Decode.succeed "NEXT")
|
2021-04-20 17:31:19 +03:00
|
|
|
|> DataSource.andThen
|
2021-04-03 00:44:40 +03:00
|
|
|
(\_ ->
|
2019-11-13 05:22:36 +03:00
|
|
|
getWithoutSecrets "NEXT" (Decode.succeed ())
|
2019-10-31 06:11:24 +03:00
|
|
|
)
|
|
|
|
|> (\request ->
|
2022-02-08 01:27:05 +03:00
|
|
|
StaticHttpRequest.resolveUrls
|
2020-04-18 06:10:25 +03:00
|
|
|
request
|
2020-01-03 23:18:18 +03:00
|
|
|
(requestsDict
|
|
|
|
[ ( get "first", "null" )
|
|
|
|
, ( get "NEXT", "null" )
|
2019-10-31 06:11:24 +03:00
|
|
|
]
|
|
|
|
)
|
2021-06-03 21:00:48 +03:00
|
|
|
|> Expect.equal [ getReq "first", getReq "NEXT" ]
|
2019-10-31 06:11:24 +03:00
|
|
|
)
|
|
|
|
, test "andThen staring with done" <|
|
|
|
|
\() ->
|
2021-04-20 17:31:19 +03:00
|
|
|
DataSource.succeed ()
|
|
|
|
|> DataSource.andThen
|
2019-10-31 06:11:24 +03:00
|
|
|
(\_ ->
|
2019-11-13 05:22:36 +03:00
|
|
|
getWithoutSecrets "NEXT" (Decode.succeed ())
|
2019-10-31 06:11:24 +03:00
|
|
|
)
|
|
|
|
|> (\request ->
|
2022-02-08 01:27:05 +03:00
|
|
|
StaticHttpRequest.resolveUrls
|
2020-04-18 06:10:25 +03:00
|
|
|
request
|
2020-01-03 23:18:18 +03:00
|
|
|
(requestsDict
|
|
|
|
[ ( get "NEXT", "null" )
|
2019-10-31 06:11:24 +03:00
|
|
|
]
|
|
|
|
)
|
2021-06-03 21:00:48 +03:00
|
|
|
|> Expect.equal [ getReq "NEXT" ]
|
2019-10-31 06:11:24 +03:00
|
|
|
)
|
|
|
|
, test "map" <|
|
|
|
|
\() ->
|
2019-11-13 05:22:36 +03:00
|
|
|
getWithoutSecrets "first" (Decode.succeed "NEXT")
|
2021-04-20 17:31:19 +03:00
|
|
|
|> DataSource.andThen
|
2021-04-03 00:44:40 +03:00
|
|
|
(\_ ->
|
2019-11-05 18:53:17 +03:00
|
|
|
-- StaticHttp.get continueUrl (Decode.succeed ())
|
2019-11-13 05:22:36 +03:00
|
|
|
getWithoutSecrets "NEXT" (Decode.succeed ())
|
2019-10-31 06:11:24 +03:00
|
|
|
)
|
2021-04-20 17:31:19 +03:00
|
|
|
|> DataSource.map (\_ -> ())
|
2019-10-31 06:11:24 +03:00
|
|
|
|> (\request ->
|
2022-02-08 01:27:05 +03:00
|
|
|
StaticHttpRequest.resolveUrls
|
2020-04-18 06:10:25 +03:00
|
|
|
request
|
2020-01-03 23:18:18 +03:00
|
|
|
(requestsDict
|
|
|
|
[ ( get "first", "null" )
|
|
|
|
, ( get "NEXT", "null" )
|
2019-10-31 06:11:24 +03:00
|
|
|
]
|
|
|
|
)
|
2021-06-03 21:00:48 +03:00
|
|
|
|> Expect.equal [ getReq "first", getReq "NEXT" ]
|
2019-11-01 23:29:09 +03:00
|
|
|
)
|
|
|
|
, test "andThen chain with 1 response available and 1 pending" <|
|
|
|
|
\() ->
|
2019-11-13 05:22:36 +03:00
|
|
|
getWithoutSecrets "first" (Decode.succeed "NEXT")
|
2021-04-20 17:31:19 +03:00
|
|
|
|> DataSource.andThen
|
2021-04-03 00:44:40 +03:00
|
|
|
(\_ ->
|
2019-11-13 05:22:36 +03:00
|
|
|
getWithoutSecrets "NEXT" (Decode.succeed ())
|
2019-11-01 23:29:09 +03:00
|
|
|
)
|
|
|
|
|> (\request ->
|
2022-02-08 01:27:05 +03:00
|
|
|
StaticHttpRequest.resolveUrls
|
2020-04-18 06:10:25 +03:00
|
|
|
request
|
2020-01-03 23:18:18 +03:00
|
|
|
(requestsDict
|
|
|
|
[ ( get "first", "null" )
|
2019-11-01 23:29:09 +03:00
|
|
|
]
|
|
|
|
)
|
2021-06-03 21:00:48 +03:00
|
|
|
|> Expect.equal [ getReq "first", getReq "NEXT" ]
|
2019-11-01 23:29:09 +03:00
|
|
|
)
|
|
|
|
, test "andThen chain with 1 response available and 2 pending" <|
|
|
|
|
\() ->
|
2019-11-13 05:22:36 +03:00
|
|
|
getWithoutSecrets "first" Decode.int
|
2021-04-20 17:31:19 +03:00
|
|
|
|> DataSource.andThen
|
2021-04-03 00:44:40 +03:00
|
|
|
(\_ ->
|
2019-11-13 05:22:36 +03:00
|
|
|
getWithoutSecrets "NEXT" Decode.string
|
2021-04-20 17:31:19 +03:00
|
|
|
|> DataSource.andThen
|
2019-11-01 23:29:09 +03:00
|
|
|
(\_ ->
|
2019-11-13 05:22:36 +03:00
|
|
|
getWithoutSecrets "LAST"
|
2019-11-01 23:29:09 +03:00
|
|
|
Decode.string
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> (\request ->
|
2022-02-08 01:27:05 +03:00
|
|
|
StaticHttpRequest.resolveUrls
|
2020-04-18 06:10:25 +03:00
|
|
|
request
|
2020-01-03 23:18:18 +03:00
|
|
|
(requestsDict
|
|
|
|
[ ( get "first", "1" )
|
|
|
|
]
|
|
|
|
)
|
2021-06-03 21:00:48 +03:00
|
|
|
|> Expect.equal [ getReq "first", getReq "NEXT" ]
|
2019-10-31 06:11:24 +03:00
|
|
|
)
|
|
|
|
]
|
2019-11-11 23:40:38 +03:00
|
|
|
|
|
|
|
|
2021-04-20 23:52:54 +03:00
|
|
|
getReq : String -> DataSource.Http.RequestDetails
|
2020-01-03 23:18:18 +03:00
|
|
|
getReq url =
|
2020-01-04 00:22:53 +03:00
|
|
|
{ url = url
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
2021-07-30 01:57:44 +03:00
|
|
|
, body = DataSource.Http.emptyBody
|
2020-01-04 00:22:53 +03:00
|
|
|
}
|