2019-10-22 05:11:50 +03:00
|
|
|
module StaticHttpRequestsTests exposing (all)
|
2019-10-22 04:49:46 +03:00
|
|
|
|
2021-05-06 17:26:21 +03:00
|
|
|
import ApiRoute
|
2023-01-02 01:44:11 +03:00
|
|
|
import BackendTask exposing (BackendTask)
|
|
|
|
import BackendTask.Http
|
2022-02-08 01:50:38 +03:00
|
|
|
import Bytes.Decode
|
|
|
|
import Bytes.Encode
|
2019-10-22 08:00:15 +03:00
|
|
|
import Codec
|
2020-12-07 19:41:10 +03:00
|
|
|
import Dict
|
2019-10-22 06:21:52 +03:00
|
|
|
import Expect
|
2023-01-17 02:22:44 +03:00
|
|
|
import FatalError exposing (FatalError)
|
2019-10-22 04:49:46 +03:00
|
|
|
import Html
|
2022-01-28 07:41:54 +03:00
|
|
|
import Json.Decode as JD exposing (Decoder)
|
2020-04-19 18:17:51 +03:00
|
|
|
import Json.Encode as Encode
|
2021-04-03 00:44:40 +03:00
|
|
|
import Pages.Internal.Platform.Cli exposing (..)
|
2020-06-12 06:57:19 +03:00
|
|
|
import Pages.Internal.Platform.Effect as Effect exposing (Effect)
|
2021-07-28 18:06:05 +03:00
|
|
|
import Pages.Internal.Platform.ToJsPayload as ToJsPayload
|
2022-03-25 20:42:00 +03:00
|
|
|
import Pages.ProgramConfig exposing (ProgramConfig)
|
2021-10-07 22:11:23 +03:00
|
|
|
import Pages.SiteConfig exposing (SiteConfig)
|
2020-01-03 23:18:18 +03:00
|
|
|
import Pages.StaticHttp.Request as Request
|
2021-05-23 20:44:39 +03:00
|
|
|
import Path
|
2019-10-22 04:49:46 +03:00
|
|
|
import ProgramTest exposing (ProgramTest)
|
2019-11-03 00:46:52 +03:00
|
|
|
import Regex
|
2021-04-17 17:42:33 +03:00
|
|
|
import RenderRequest
|
2022-02-27 01:10:31 +03:00
|
|
|
import RequestsAndPending exposing (ResponseBody(..))
|
2022-01-25 02:43:37 +03:00
|
|
|
import Server.Response as Response
|
2019-10-22 05:31:17 +03:00
|
|
|
import SimulatedEffect.Cmd
|
2019-10-22 06:21:52 +03:00
|
|
|
import SimulatedEffect.Ports
|
2022-03-11 22:52:33 +03:00
|
|
|
import Test exposing (Test, describe, test)
|
2019-10-22 05:11:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
all : Test
|
|
|
|
all =
|
2019-10-22 16:46:31 +03:00
|
|
|
describe "Static Http Requests"
|
|
|
|
[ test "initial requests are sent out" <|
|
2019-10-22 05:11:50 +03:00
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple []
|
2023-01-17 05:24:48 +03:00
|
|
|
(BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages" starDecoder |> BackendTask.allowFatal)
|
2021-07-29 19:17:36 +03:00
|
|
|
|> simulateHttp
|
2022-01-28 07:41:54 +03:00
|
|
|
(get "https://api.github.com/repos/dillonkearns/elm-pages")
|
2022-02-27 01:10:31 +03:00
|
|
|
(JsonBody
|
|
|
|
(Encode.object [ ( "stargazer_count", Encode.int 86 ) ])
|
|
|
|
)
|
|
|
|
|> expectSuccess []
|
2021-04-16 18:26:59 +03:00
|
|
|
, test "StaticHttp request for initial are resolved" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple
|
|
|
|
[ "post-1" ]
|
2023-01-17 05:24:48 +03:00
|
|
|
(BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages" starDecoder |> BackendTask.allowFatal)
|
2021-07-29 19:17:36 +03:00
|
|
|
|> simulateHttp
|
2022-01-28 07:41:54 +03:00
|
|
|
(get "https://api.github.com/repos/dillonkearns/elm-pages")
|
2022-02-27 01:10:31 +03:00
|
|
|
(JsonBody
|
|
|
|
(Encode.object [ ( "stargazer_count", Encode.int 86 ) ])
|
|
|
|
)
|
2022-02-26 04:30:21 +03:00
|
|
|
|> expectSuccess []
|
2021-06-08 19:54:10 +03:00
|
|
|
, describe "single page renders"
|
|
|
|
[ test "single pages that are pre-rendered" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple [ "post-1" ]
|
2023-01-17 05:24:48 +03:00
|
|
|
(BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages" starDecoder |> BackendTask.allowFatal)
|
2021-07-29 19:17:36 +03:00
|
|
|
|> simulateHttp
|
2022-01-28 07:41:54 +03:00
|
|
|
(get "https://api.github.com/repos/dillonkearns/elm-pages")
|
2022-02-27 01:10:31 +03:00
|
|
|
(JsonBody
|
|
|
|
(Encode.object [ ( "stargazer_count", Encode.int 86 ) ])
|
|
|
|
)
|
2021-06-08 19:54:10 +03:00
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
|
|
|
[ ToJsPayload.PageProgress portData ] ->
|
2022-11-16 03:30:26 +03:00
|
|
|
if portData.is404 then
|
|
|
|
Expect.fail "Expected page to be found and rendered"
|
|
|
|
|
|
|
|
else
|
|
|
|
Expect.pass
|
2021-06-08 19:54:10 +03:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail <| "Expected exactly 1 port of type PageProgress. Instead, got \n" ++ Debug.toString actualPorts
|
|
|
|
)
|
|
|
|
]
|
2021-06-01 06:59:30 +03:00
|
|
|
, test "the stripped JSON from the same request with different decoders is merged so the decoders succeed" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple
|
|
|
|
[ "post-1" ]
|
2023-01-02 01:44:11 +03:00
|
|
|
(BackendTask.map2 Tuple.pair
|
2023-01-08 02:33:23 +03:00
|
|
|
(BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages"
|
2022-01-28 07:41:54 +03:00
|
|
|
(JD.field "stargazer_count" JD.int)
|
2023-01-17 05:24:48 +03:00
|
|
|
|> BackendTask.allowFatal
|
2021-07-29 19:17:36 +03:00
|
|
|
)
|
2023-01-08 02:33:23 +03:00
|
|
|
(BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages"
|
2022-01-28 07:41:54 +03:00
|
|
|
(JD.field "language" JD.string)
|
2023-01-17 05:24:48 +03:00
|
|
|
|> BackendTask.allowFatal
|
2021-07-29 19:17:36 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
2022-01-28 07:41:54 +03:00
|
|
|
(get "https://api.github.com/repos/dillonkearns/elm-pages")
|
2022-02-27 01:10:31 +03:00
|
|
|
(JsonBody
|
|
|
|
(Encode.object
|
|
|
|
[ ( "stargazer_count", Encode.int 86 )
|
|
|
|
, ( "language", Encode.string "Elm" )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2022-02-26 04:30:21 +03:00
|
|
|
|> expectSuccess []
|
2019-11-01 23:29:09 +03:00
|
|
|
, test "andThen" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple
|
|
|
|
[ "elm-pages" ]
|
2023-01-08 02:33:23 +03:00
|
|
|
(BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages" (JD.succeed ())
|
2023-01-17 05:24:48 +03:00
|
|
|
|> BackendTask.allowFatal
|
2023-01-02 01:44:11 +03:00
|
|
|
|> BackendTask.andThen
|
2021-07-29 19:17:36 +03:00
|
|
|
(\_ ->
|
2023-01-08 02:33:23 +03:00
|
|
|
BackendTask.Http.getJson "NEXT-REQUEST" (JD.succeed ())
|
2023-01-17 05:24:48 +03:00
|
|
|
|> BackendTask.allowFatal
|
2021-07-29 19:17:36 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
2022-01-28 07:41:54 +03:00
|
|
|
(get "https://api.github.com/repos/dillonkearns/elm-pages")
|
2022-02-27 01:10:31 +03:00
|
|
|
(JsonBody Encode.null)
|
2021-07-29 19:17:36 +03:00
|
|
|
|> simulateHttp
|
2022-01-28 07:41:54 +03:00
|
|
|
(get "NEXT-REQUEST")
|
2022-02-27 01:10:31 +03:00
|
|
|
(JsonBody Encode.null)
|
2022-02-26 04:30:21 +03:00
|
|
|
|> expectSuccess []
|
2021-07-28 18:06:05 +03:00
|
|
|
|
2022-12-23 01:27:00 +03:00
|
|
|
--, test "andThen chain avoids repeat requests" <|
|
|
|
|
-- TODO is this test case still relevant? Need to think about the new desired functionality with caching HTTP requests given that
|
2023-01-02 01:44:11 +03:00
|
|
|
-- BackendTask's can perform non-deterministic effects now.
|
2022-12-23 01:27:00 +03:00
|
|
|
-- \() ->
|
|
|
|
-- let
|
2023-01-02 01:44:11 +03:00
|
|
|
-- pokemonDetailRequest : BackendTask ()
|
2022-12-23 01:27:00 +03:00
|
|
|
-- pokemonDetailRequest =
|
2023-01-08 02:33:23 +03:00
|
|
|
-- JsonBackendTask.Http.get
|
2022-12-23 01:27:00 +03:00
|
|
|
-- "https://pokeapi.co/api/v2/pokemon/"
|
|
|
|
-- (JD.list
|
|
|
|
-- (JD.field "url" JD.string
|
|
|
|
-- |> JD.map
|
|
|
|
-- (\url ->
|
2023-01-08 02:33:23 +03:00
|
|
|
-- BackendTask.Http.getJson url
|
2022-12-23 01:27:00 +03:00
|
|
|
-- (JD.field "image" JD.string)
|
|
|
|
-- )
|
|
|
|
-- )
|
|
|
|
-- )
|
2023-01-02 01:44:11 +03:00
|
|
|
-- |> BackendTask.resolve
|
|
|
|
-- |> BackendTask.map (\_ -> ())
|
2022-12-23 01:27:00 +03:00
|
|
|
-- in
|
|
|
|
-- startSimple
|
|
|
|
-- [ "elm-pages" ]
|
|
|
|
-- pokemonDetailRequest
|
|
|
|
-- |> simulateMultipleHttp
|
|
|
|
-- [ ( get "https://pokeapi.co/api/v2/pokemon/"
|
|
|
|
-- , jsonBody """[
|
|
|
|
-- {"url": "url1"},
|
|
|
|
-- {"url": "url2"},
|
|
|
|
-- {"url": "url3"},
|
|
|
|
-- {"url": "url4"},
|
|
|
|
-- {"url": "url5"},
|
|
|
|
-- {"url": "url6"},
|
|
|
|
-- {"url": "url7"},
|
|
|
|
-- {"url": "url8"},
|
|
|
|
-- {"url": "url9"},
|
|
|
|
-- {"url": "url10"}
|
|
|
|
-- ]"""
|
|
|
|
-- )
|
|
|
|
-- , ( get "url1"
|
|
|
|
-- , jsonBody """{"image": "image1.jpg"}"""
|
|
|
|
-- )
|
|
|
|
-- , ( get "url2"
|
|
|
|
-- , jsonBody """{"image": "image2.jpg"}"""
|
|
|
|
-- )
|
|
|
|
-- , ( get "url3"
|
|
|
|
-- , jsonBody """{"image": "image3.jpg"}"""
|
|
|
|
-- )
|
|
|
|
-- , ( get "url4"
|
|
|
|
-- , jsonBody """{"image": "image4.jpg"}"""
|
|
|
|
-- )
|
|
|
|
-- , ( get "url5"
|
|
|
|
-- , jsonBody """{"image": "image5.jpg"}"""
|
|
|
|
-- )
|
|
|
|
-- , ( get "url6"
|
|
|
|
-- , jsonBody """{"image": "image6.jpg"}"""
|
|
|
|
-- )
|
|
|
|
-- , ( get "url7"
|
|
|
|
-- , jsonBody """{"image": "image7.jpg"}"""
|
|
|
|
-- )
|
|
|
|
-- , ( get "url8"
|
|
|
|
-- , jsonBody """{"image": "image8.jpg"}"""
|
|
|
|
-- )
|
|
|
|
-- , ( get "url9"
|
|
|
|
-- , jsonBody """{"image": "image9.jpg"}"""
|
|
|
|
-- )
|
|
|
|
-- , ( get "url10"
|
|
|
|
-- , jsonBody """{"image": "image10.jpg"}"""
|
|
|
|
-- )
|
|
|
|
-- ]
|
|
|
|
-- |> expectSuccess []
|
|
|
|
--
|
2021-07-28 18:06:05 +03:00
|
|
|
--, test "port is sent out once all requests are finished" <|
|
|
|
|
-- \() ->
|
|
|
|
-- start
|
|
|
|
-- [ ( [ "elm-pages" ]
|
2023-01-08 02:33:23 +03:00
|
|
|
-- , BackendTask.Http.getJson (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
2021-07-28 18:06:05 +03:00
|
|
|
-- )
|
|
|
|
-- , ( [ "elm-pages-starter" ]
|
2023-01-08 02:33:23 +03:00
|
|
|
-- , BackendTask.Http.getJson (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages-starter") starDecoder
|
2021-07-28 18:06:05 +03:00
|
|
|
-- )
|
|
|
|
-- ]
|
|
|
|
-- |> ProgramTest.simulateHttpOk
|
|
|
|
-- "GET"
|
|
|
|
-- "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
-- """{ "stargazer_count": 86 }"""
|
|
|
|
-- |> ProgramTest.simulateHttpOk
|
|
|
|
-- "GET"
|
|
|
|
-- "https://api.github.com/repos/dillonkearns/elm-pages-starter"
|
|
|
|
-- """{ "stargazer_count": 22 }"""
|
|
|
|
-- |> expectSuccess
|
|
|
|
-- [ ( "elm-pages"
|
|
|
|
-- , [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
-- , """{"stargazer_count":86}"""
|
|
|
|
-- )
|
|
|
|
-- ]
|
|
|
|
-- )
|
|
|
|
-- , ( "elm-pages-starter"
|
|
|
|
-- , [ ( get "https://api.github.com/repos/dillonkearns/elm-pages-starter"
|
|
|
|
-- , """{"stargazer_count":22}"""
|
|
|
|
-- )
|
|
|
|
-- ]
|
|
|
|
-- )
|
|
|
|
-- ]
|
2022-02-26 04:30:21 +03:00
|
|
|
, test "reduced JSON is sent out" <|
|
|
|
|
\() ->
|
|
|
|
startSimple []
|
2023-01-08 02:33:23 +03:00
|
|
|
(BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages" (JD.field "stargazer_count" JD.int)
|
2023-01-17 05:24:48 +03:00
|
|
|
|> BackendTask.allowFatal
|
2022-12-30 00:03:10 +03:00
|
|
|
)
|
2022-02-26 04:30:21 +03:00
|
|
|
|> simulateHttp
|
|
|
|
(get "https://api.github.com/repos/dillonkearns/elm-pages")
|
2022-02-27 01:10:31 +03:00
|
|
|
(jsonBody """{ "stargazer_count": 86, "unused_field": 123 }""")
|
2022-02-26 04:30:21 +03:00
|
|
|
|> expectSuccess []
|
|
|
|
, test "you can use elm/json decoders with StaticHttp.unoptimizedRequest" <|
|
|
|
|
\() ->
|
|
|
|
startSimple []
|
2023-01-02 01:44:11 +03:00
|
|
|
(BackendTask.Http.request
|
2022-02-26 04:30:21 +03:00
|
|
|
{ url = "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
2023-01-02 01:44:11 +03:00
|
|
|
, body = BackendTask.Http.emptyBody
|
2023-01-08 21:57:38 +03:00
|
|
|
, retries = Nothing
|
|
|
|
, timeoutInMs = Nothing
|
2022-02-26 04:30:21 +03:00
|
|
|
}
|
2023-01-02 01:44:11 +03:00
|
|
|
(BackendTask.Http.expectJson
|
2022-02-26 04:30:21 +03:00
|
|
|
(JD.field "stargazer_count" JD.int)
|
2021-07-29 19:17:36 +03:00
|
|
|
)
|
2023-01-17 05:24:48 +03:00
|
|
|
|> BackendTask.allowFatal
|
2022-02-26 04:30:21 +03:00
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(get "https://api.github.com/repos/dillonkearns/elm-pages")
|
2022-02-27 01:10:31 +03:00
|
|
|
(jsonBody """{ "stargazer_count": 86, "unused_field": 123 }""")
|
|
|
|
|> expectSuccess []
|
|
|
|
, test "plain string" <|
|
|
|
|
\() ->
|
|
|
|
startSimple []
|
2023-01-02 01:44:11 +03:00
|
|
|
(BackendTask.Http.request
|
2022-02-27 01:10:31 +03:00
|
|
|
{ url = "https://example.com/file.txt"
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
2023-01-02 01:44:11 +03:00
|
|
|
, body = BackendTask.Http.emptyBody
|
2023-01-08 21:57:38 +03:00
|
|
|
, retries = Nothing
|
|
|
|
, timeoutInMs = Nothing
|
2022-02-27 01:10:31 +03:00
|
|
|
}
|
2023-01-02 01:44:11 +03:00
|
|
|
BackendTask.Http.expectString
|
2023-01-17 05:24:48 +03:00
|
|
|
|> BackendTask.allowFatal
|
2022-02-27 01:10:31 +03:00
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
{ method = "GET"
|
|
|
|
, url = "https://example.com/file.txt"
|
|
|
|
, headers =
|
|
|
|
[]
|
2023-01-02 01:44:11 +03:00
|
|
|
, body = BackendTask.Http.emptyBody
|
2023-01-08 02:31:54 +03:00
|
|
|
, cacheOptions = Nothing
|
2022-02-27 01:10:31 +03:00
|
|
|
}
|
|
|
|
(StringBody "This is a raw text file.")
|
2022-02-26 04:30:21 +03:00
|
|
|
|> expectSuccess []
|
2022-02-27 01:35:52 +03:00
|
|
|
, test "Err in String to Result function turns into decode error" <|
|
|
|
|
\() ->
|
|
|
|
startSimple []
|
2023-01-02 01:44:11 +03:00
|
|
|
(BackendTask.Http.request
|
2022-02-27 01:35:52 +03:00
|
|
|
{ url = "https://example.com/file.txt"
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
2023-01-02 01:44:11 +03:00
|
|
|
, body = BackendTask.Http.emptyBody
|
2023-01-08 21:57:38 +03:00
|
|
|
, retries = Nothing
|
|
|
|
, timeoutInMs = Nothing
|
2022-02-27 01:35:52 +03:00
|
|
|
}
|
2023-01-02 01:44:11 +03:00
|
|
|
BackendTask.Http.expectString
|
2023-01-17 05:24:48 +03:00
|
|
|
|> BackendTask.allowFatal
|
2023-01-02 01:44:11 +03:00
|
|
|
|> BackendTask.map
|
2022-02-27 01:35:52 +03:00
|
|
|
(\string ->
|
|
|
|
if String.toUpper string == string then
|
|
|
|
Ok string
|
2020-03-03 18:54:16 +03:00
|
|
|
|
2022-02-27 01:35:52 +03:00
|
|
|
else
|
|
|
|
Err "String was not uppercased"
|
2020-03-03 18:54:16 +03:00
|
|
|
)
|
2023-01-17 02:22:44 +03:00
|
|
|
|> BackendTask.andThen (\result -> result |> Result.mapError FatalError.fromString |> BackendTask.fromResult)
|
2022-02-27 01:35:52 +03:00
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(get "https://example.com/file.txt")
|
|
|
|
(StringBody "This is a raw text file.")
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(expectErrorsPort
|
2023-01-02 01:28:58 +03:00
|
|
|
"""-- CUSTOM ERROR ----------------------------------------------------- elm-pages
|
2020-03-03 18:54:16 +03:00
|
|
|
|
2020-03-11 19:03:47 +03:00
|
|
|
|
2020-03-03 18:54:16 +03:00
|
|
|
|
2022-12-30 00:14:08 +03:00
|
|
|
String was not uppercased"""
|
2022-02-27 01:35:52 +03:00
|
|
|
)
|
2022-02-27 01:18:37 +03:00
|
|
|
, test "POST method works" <|
|
|
|
|
\() ->
|
|
|
|
startSimple []
|
2023-01-02 01:44:11 +03:00
|
|
|
(BackendTask.Http.request
|
2022-02-27 01:18:37 +03:00
|
|
|
{ method = "POST"
|
|
|
|
, url = "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, headers = []
|
2023-01-02 01:44:11 +03:00
|
|
|
, body = BackendTask.Http.emptyBody
|
2023-01-08 21:57:38 +03:00
|
|
|
, retries = Nothing
|
|
|
|
, timeoutInMs = Nothing
|
2022-02-27 01:18:37 +03:00
|
|
|
}
|
2023-01-02 01:44:11 +03:00
|
|
|
(BackendTask.Http.expectJson
|
2022-02-27 01:18:37 +03:00
|
|
|
(JD.field "stargazer_count" JD.int)
|
2022-02-26 03:57:50 +03:00
|
|
|
)
|
2023-01-17 05:24:48 +03:00
|
|
|
|> BackendTask.allowFatal
|
2022-02-27 01:18:37 +03:00
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(post "https://api.github.com/repos/dillonkearns/elm-pages")
|
|
|
|
(jsonBody """{ "stargazer_count": 86, "unused_field": 123 }""")
|
|
|
|
|> expectSuccess []
|
2022-02-26 04:30:21 +03:00
|
|
|
, test "json is reduced from andThen chains" <|
|
|
|
|
\() ->
|
|
|
|
startSimple []
|
2023-01-08 02:33:23 +03:00
|
|
|
(BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages" (JD.field "stargazer_count" JD.int)
|
2023-01-02 01:44:11 +03:00
|
|
|
|> BackendTask.andThen
|
2022-02-26 04:30:21 +03:00
|
|
|
(\_ ->
|
2023-01-08 02:33:23 +03:00
|
|
|
BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages-starter" (JD.field "stargazer_count" JD.int)
|
2022-02-26 04:30:21 +03:00
|
|
|
)
|
2023-01-17 05:24:48 +03:00
|
|
|
|> BackendTask.allowFatal
|
2022-02-26 04:30:21 +03:00
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(get "https://api.github.com/repos/dillonkearns/elm-pages")
|
2022-02-27 01:10:31 +03:00
|
|
|
(jsonBody """{ "stargazer_count": 100, "unused_field": 123 }""")
|
2022-02-26 04:30:21 +03:00
|
|
|
|> simulateHttp
|
|
|
|
(get "https://api.github.com/repos/dillonkearns/elm-pages-starter")
|
2022-02-27 01:10:31 +03:00
|
|
|
(jsonBody """{ "stargazer_count": 50, "unused_field": 456 }""")
|
2022-02-26 04:30:21 +03:00
|
|
|
|> expectSuccess []
|
2022-02-26 18:44:16 +03:00
|
|
|
, test "reduced json is preserved by StaticHttp.map2" <|
|
|
|
|
\() ->
|
|
|
|
startSimple []
|
2023-01-02 01:44:11 +03:00
|
|
|
(BackendTask.map2 (\_ _ -> ())
|
2023-01-17 05:24:48 +03:00
|
|
|
(BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages" (JD.field "stargazer_count" JD.int) |> BackendTask.allowFatal)
|
|
|
|
(BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages-starter" (JD.field "stargazer_count" JD.int) |> BackendTask.allowFatal)
|
2022-02-26 18:44:16 +03:00
|
|
|
)
|
|
|
|
|> simulateMultipleHttp
|
|
|
|
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
2022-02-27 01:10:31 +03:00
|
|
|
, jsonBody """{ "stargazer_count": 100, "unused_field": 123 }"""
|
2022-02-26 18:44:16 +03:00
|
|
|
)
|
|
|
|
, ( get "https://api.github.com/repos/dillonkearns/elm-pages-starter"
|
2022-02-27 01:10:31 +03:00
|
|
|
, jsonBody """{ "stargazer_count": 50, "unused_field": 456 }"""
|
2022-02-26 18:44:16 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|> expectSuccess []
|
2022-02-27 01:10:31 +03:00
|
|
|
, test "the port sends out even if there are no http requests" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
2023-01-02 01:44:11 +03:00
|
|
|
, BackendTask.succeed ()
|
2022-02-27 01:10:31 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|> expectSuccess []
|
|
|
|
, test "the port sends out when there are duplicate http requests for the same page" <|
|
|
|
|
\() ->
|
|
|
|
startSimple []
|
2023-01-02 01:44:11 +03:00
|
|
|
(BackendTask.map2 (\_ _ -> ())
|
2023-01-17 05:24:48 +03:00
|
|
|
(BackendTask.Http.getJson "http://example.com" (JD.succeed ()) |> BackendTask.allowFatal)
|
|
|
|
(BackendTask.Http.getJson "http://example.com" (JD.succeed ()) |> BackendTask.allowFatal)
|
2022-02-27 01:10:31 +03:00
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(get "http://example.com")
|
|
|
|
(jsonBody """null""")
|
|
|
|
|> expectSuccess []
|
2022-02-27 01:42:31 +03:00
|
|
|
, test "an error is sent out for decoder failures" <|
|
|
|
|
\() ->
|
|
|
|
startSimple [ "elm-pages" ]
|
2023-01-17 05:24:48 +03:00
|
|
|
(BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages" (JD.fail "The user should get this message from the CLI.") |> BackendTask.allowFatal)
|
2022-02-27 01:42:31 +03:00
|
|
|
|> simulateHttp
|
|
|
|
(get "https://api.github.com/repos/dillonkearns/elm-pages")
|
|
|
|
(jsonBody """{ "stargazer_count": 86 }""")
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(expectErrorsPort
|
2023-01-02 01:28:58 +03:00
|
|
|
"""-- HTTP ERROR ----------------------------------------------------- elm-pages
|
2019-10-30 08:33:47 +03:00
|
|
|
|
2023-01-01 00:14:44 +03:00
|
|
|
BadBody: Problem with the given value:
|
2019-10-27 02:50:09 +03:00
|
|
|
|
2022-02-27 01:42:31 +03:00
|
|
|
{
|
|
|
|
"stargazer_count": 86
|
|
|
|
}
|
2019-11-02 21:13:33 +03:00
|
|
|
|
2022-02-27 01:42:31 +03:00
|
|
|
The user should get this message from the CLI."""
|
|
|
|
)
|
2019-10-22 05:11:50 +03:00
|
|
|
]
|
2019-10-22 04:49:46 +03:00
|
|
|
|
|
|
|
|
2021-04-16 18:04:15 +03:00
|
|
|
type Route
|
|
|
|
= Route String
|
|
|
|
|
|
|
|
|
2023-01-17 02:22:44 +03:00
|
|
|
start : List ( List String, BackendTask FatalError a ) -> ProgramTest (Model Route) Msg Effect
|
2019-10-22 16:46:31 +03:00
|
|
|
start pages =
|
2021-06-04 03:14:25 +03:00
|
|
|
startWithHttpCache [] pages
|
2020-04-19 18:17:51 +03:00
|
|
|
|
|
|
|
|
2020-06-13 22:29:36 +03:00
|
|
|
startWithHttpCache :
|
2021-06-04 03:14:25 +03:00
|
|
|
List ( Request.Request, String )
|
2023-01-17 02:22:44 +03:00
|
|
|
-> List ( List String, BackendTask FatalError a )
|
2021-04-16 18:04:15 +03:00
|
|
|
-> ProgramTest (Model Route) Msg Effect
|
2020-06-13 22:31:47 +03:00
|
|
|
startWithHttpCache =
|
2021-05-06 17:14:49 +03:00
|
|
|
startLowLevel []
|
2020-06-13 22:31:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
startLowLevel :
|
2021-07-31 01:35:02 +03:00
|
|
|
List (ApiRoute.ApiRoute ApiRoute.Response)
|
2020-06-13 22:31:47 +03:00
|
|
|
-> List ( Request.Request, String )
|
2023-01-17 02:22:44 +03:00
|
|
|
-> List ( List String, BackendTask FatalError a )
|
2021-04-16 18:04:15 +03:00
|
|
|
-> ProgramTest (Model Route) Msg Effect
|
2023-01-07 06:31:55 +03:00
|
|
|
startLowLevel apiRoutes _ pages =
|
2019-10-22 04:49:46 +03:00
|
|
|
let
|
2021-07-27 20:41:18 +03:00
|
|
|
pageToLoad : List String
|
|
|
|
pageToLoad =
|
|
|
|
case pages |> List.head |> Maybe.map Tuple.first of
|
|
|
|
Just justPageToLoad ->
|
|
|
|
justPageToLoad
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Debug.todo "Error - no pages"
|
|
|
|
|
2021-06-04 03:57:57 +03:00
|
|
|
encodedFlags : Encode.Value
|
2020-04-19 18:17:51 +03:00
|
|
|
encodedFlags =
|
|
|
|
--{"secrets":
|
|
|
|
-- {"API_KEY": "ABCD1234","BEARER": "XYZ789"}, "mode": "prod", "staticHttpCache": {}
|
|
|
|
-- }
|
|
|
|
Encode.object
|
|
|
|
[ ( "secrets"
|
|
|
|
, [ ( "API_KEY", "ABCD1234" )
|
|
|
|
, ( "BEARER", "XYZ789" )
|
|
|
|
]
|
|
|
|
|> Dict.fromList
|
|
|
|
|> Encode.dict identity Encode.string
|
|
|
|
)
|
|
|
|
, ( "mode", Encode.string "prod" )
|
2022-11-07 09:09:06 +03:00
|
|
|
, ( "compatibilityKey", Encode.int currentCompatibilityKey )
|
2020-04-19 18:17:51 +03:00
|
|
|
]
|
2019-10-22 04:49:46 +03:00
|
|
|
in
|
2020-01-25 23:47:06 +03:00
|
|
|
{-
|
|
|
|
(Model -> model)
|
|
|
|
-> ContentCache.ContentCache metadata view
|
2021-04-10 20:33:26 +03:00
|
|
|
-> Result (List BuildError) (List ( PagePath, metadata ))
|
2020-01-25 23:47:06 +03:00
|
|
|
-> Config pathKey userMsg userModel metadata view
|
|
|
|
-> Decode.Value
|
|
|
|
-> ( model, Effect pathKey )
|
|
|
|
-}
|
2019-10-22 04:49:46 +03:00
|
|
|
ProgramTest.createDocument
|
2021-07-27 20:41:18 +03:00
|
|
|
{ init =
|
|
|
|
init
|
2021-09-27 20:48:49 +03:00
|
|
|
site
|
2021-07-27 20:41:18 +03:00
|
|
|
(RenderRequest.SinglePage
|
|
|
|
RenderRequest.OnlyJson
|
|
|
|
(RenderRequest.Page
|
|
|
|
{ path = Path.fromString (pageToLoad |> String.join "/")
|
|
|
|
, frontmatter = Route (pageToLoad |> String.join "/")
|
|
|
|
}
|
|
|
|
)
|
|
|
|
(Encode.object [])
|
|
|
|
)
|
2022-05-12 01:49:04 +03:00
|
|
|
(config apiRoutes pages)
|
2022-12-23 02:17:21 +03:00
|
|
|
, update = update
|
2019-10-25 17:00:04 +03:00
|
|
|
, view = \_ -> { title = "", body = [] }
|
2019-10-22 04:49:46 +03:00
|
|
|
}
|
2019-10-22 05:31:17 +03:00
|
|
|
|> ProgramTest.withSimulatedEffects simulateEffects
|
2020-04-19 18:17:51 +03:00
|
|
|
|> ProgramTest.start (flags (Encode.encode 0 encodedFlags))
|
2019-10-24 18:26:41 +03:00
|
|
|
|
|
|
|
|
2022-02-26 03:57:50 +03:00
|
|
|
site : SiteConfig
|
2021-09-27 20:48:49 +03:00
|
|
|
site =
|
2022-02-26 03:57:50 +03:00
|
|
|
{ canonicalUrl = "canonical-site-url"
|
2023-01-02 01:44:11 +03:00
|
|
|
, head = BackendTask.succeed []
|
2021-09-27 20:48:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-17 02:22:44 +03:00
|
|
|
startSimple : List String -> BackendTask FatalError a -> ProgramTest (Model Route) Msg Effect
|
2023-01-02 01:44:11 +03:00
|
|
|
startSimple route backendTasks =
|
|
|
|
startWithRoutes route [ route ] [] [ ( route, backendTasks ) ]
|
2021-07-29 19:17:36 +03:00
|
|
|
|
|
|
|
|
2023-01-17 02:22:44 +03:00
|
|
|
config : List (ApiRoute.ApiRoute ApiRoute.Response) -> List ( List String, BackendTask FatalError a ) -> ProgramConfig Msg () Route () () () Effect mappedMsg ()
|
2022-05-12 01:49:04 +03:00
|
|
|
config apiRoutes pages =
|
|
|
|
{ toJsPort = toJsPort
|
|
|
|
, fromJsPort = fromJsPort
|
2022-09-27 01:37:10 +03:00
|
|
|
, init = \_ _ _ _ _ -> ( (), Effect.NoEffect )
|
2022-05-12 01:49:04 +03:00
|
|
|
, getStaticRoutes =
|
|
|
|
--StaticHttp.get (Secrets.succeed "https://my-cms.com/posts")
|
|
|
|
-- (Decode.field "posts" (Decode.list (Decode.string |> Decode.map Route)))
|
|
|
|
pages
|
|
|
|
|> List.map Tuple.first
|
|
|
|
|> List.map (String.join "/")
|
|
|
|
|> List.map Route
|
2023-01-02 01:44:11 +03:00
|
|
|
|> BackendTask.succeed
|
|
|
|
, handleRoute = \_ -> BackendTask.succeed Nothing
|
2022-05-12 01:49:04 +03:00
|
|
|
, urlToRoute = .path >> Route
|
2022-06-29 00:30:41 +03:00
|
|
|
, update = \_ _ _ _ _ _ _ _ -> ( (), Effect.NoEffect )
|
2022-05-12 01:49:04 +03:00
|
|
|
, basePath = []
|
2022-06-29 00:30:41 +03:00
|
|
|
, onActionData = \() -> Nothing
|
2022-05-12 01:49:04 +03:00
|
|
|
, data =
|
2022-12-23 01:27:00 +03:00
|
|
|
\_ (Route pageRoute) ->
|
2022-05-12 01:49:04 +03:00
|
|
|
let
|
2023-01-17 02:22:44 +03:00
|
|
|
thing : Maybe (BackendTask FatalError a)
|
2022-05-12 01:49:04 +03:00
|
|
|
thing =
|
|
|
|
pages
|
|
|
|
|> Dict.fromList
|
|
|
|
|> Dict.get
|
|
|
|
(pageRoute
|
|
|
|
|> String.split "/"
|
|
|
|
|> List.filter (\pathPart -> pathPart /= "")
|
|
|
|
)
|
|
|
|
in
|
|
|
|
case thing of
|
|
|
|
Just request ->
|
2023-01-02 01:44:11 +03:00
|
|
|
request |> BackendTask.map (\_ -> Response.render ())
|
2022-05-12 01:49:04 +03:00
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Debug.todo <| "Couldn't find page: " ++ pageRoute ++ "\npages: " ++ Debug.toString pages
|
|
|
|
, site = Just site
|
|
|
|
, view =
|
2022-06-29 00:30:41 +03:00
|
|
|
\_ _ _ page _ _ _ _ ->
|
2022-05-12 01:49:04 +03:00
|
|
|
let
|
2023-01-17 02:22:44 +03:00
|
|
|
thing : Maybe (BackendTask FatalError a)
|
2022-05-12 01:49:04 +03:00
|
|
|
thing =
|
|
|
|
pages
|
|
|
|
|> Dict.fromList
|
2023-03-05 21:36:24 +03:00
|
|
|
|> Dict.get page.path
|
2022-05-12 01:49:04 +03:00
|
|
|
in
|
|
|
|
case thing of
|
|
|
|
Just _ ->
|
2022-10-05 22:47:48 +03:00
|
|
|
{ view = \_ -> { title = "Title", body = [ Html.text "" ] }, head = [] }
|
2022-05-12 01:49:04 +03:00
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Debug.todo <| "Couldn't find page: " ++ Debug.toString page ++ "\npages: " ++ Debug.toString pages
|
|
|
|
, subscriptions = \_ _ _ -> Sub.none
|
|
|
|
, routeToPath = \(Route route) -> route |> String.split "/"
|
2023-01-02 01:44:11 +03:00
|
|
|
, sharedData = BackendTask.succeed ()
|
2023-01-07 06:31:55 +03:00
|
|
|
, onPageChange = \_ -> GotDataBatch (Encode.object [])
|
2022-05-12 01:49:04 +03:00
|
|
|
, apiRoutes = \_ -> apiRoutes
|
|
|
|
, pathPatterns = []
|
|
|
|
, byteDecodePageData = \_ -> Bytes.Decode.fail
|
|
|
|
, sendPageData = \_ -> Cmd.none
|
|
|
|
, encodeResponse = \_ -> Bytes.Encode.signedInt8 0
|
|
|
|
, hotReloadData = Sub.none
|
|
|
|
, decodeResponse = Bytes.Decode.fail
|
|
|
|
, byteEncodePageData = \_ -> Bytes.Encode.signedInt8 0
|
|
|
|
, gotBatchSub = Sub.none
|
|
|
|
, globalHeadTags = Nothing
|
|
|
|
, perform = \_ _ -> Cmd.none
|
|
|
|
, cmdToEffect = \_ -> Effect.NoEffect
|
|
|
|
, errorStatusCode = \_ -> 404
|
|
|
|
, notFoundPage = ()
|
|
|
|
, notFoundRoute = Route "not-found"
|
|
|
|
, internalError = \_ -> ()
|
|
|
|
, errorPageToData = \_ -> ()
|
2023-01-17 02:22:44 +03:00
|
|
|
, action = \_ _ -> BackendTask.fail (FatalError.fromString "No action.")
|
2022-05-12 01:49:04 +03:00
|
|
|
, encodeAction = \_ -> Bytes.Encode.signedInt8 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-08 19:54:10 +03:00
|
|
|
startWithRoutes :
|
|
|
|
List String
|
|
|
|
-> List (List String)
|
|
|
|
-> List ( Request.Request, String )
|
2023-01-17 02:22:44 +03:00
|
|
|
-> List ( List String, BackendTask FatalError a )
|
2021-06-08 19:54:10 +03:00
|
|
|
-> ProgramTest (Model Route) Msg Effect
|
2023-01-07 06:31:55 +03:00
|
|
|
startWithRoutes pageToLoad _ _ pages =
|
2021-06-08 19:54:10 +03:00
|
|
|
let
|
|
|
|
encodedFlags : Encode.Value
|
|
|
|
encodedFlags =
|
|
|
|
--{"secrets":
|
|
|
|
-- {"API_KEY": "ABCD1234","BEARER": "XYZ789"}, "mode": "prod", "staticHttpCache": {}
|
|
|
|
-- }
|
|
|
|
Encode.object
|
|
|
|
[ ( "secrets"
|
|
|
|
, [ ( "API_KEY", "ABCD1234" )
|
|
|
|
, ( "BEARER", "XYZ789" )
|
|
|
|
]
|
|
|
|
|> Dict.fromList
|
|
|
|
|> Encode.dict identity Encode.string
|
|
|
|
)
|
|
|
|
, ( "staticHttpCache", encodedStaticHttpCache )
|
2021-08-10 05:52:56 +03:00
|
|
|
, ( "mode", Encode.string "dev-server" )
|
2022-11-07 09:09:06 +03:00
|
|
|
, ( "compatibilityKey", Encode.int currentCompatibilityKey )
|
2021-06-08 19:54:10 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
encodedStaticHttpCache : Encode.Value
|
|
|
|
encodedStaticHttpCache =
|
2023-01-07 06:31:55 +03:00
|
|
|
[] |> Encode.object
|
2021-06-08 19:54:10 +03:00
|
|
|
in
|
|
|
|
{-
|
|
|
|
(Model -> model)
|
|
|
|
-> ContentCache.ContentCache metadata view
|
|
|
|
-> Result (List BuildError) (List ( PagePath, metadata ))
|
|
|
|
-> Config pathKey userMsg userModel metadata view
|
|
|
|
-> Decode.Value
|
|
|
|
-> ( model, Effect pathKey )
|
|
|
|
-}
|
|
|
|
ProgramTest.createDocument
|
|
|
|
{ init =
|
|
|
|
init
|
2021-09-27 20:48:49 +03:00
|
|
|
site
|
2021-06-08 19:54:10 +03:00
|
|
|
(RenderRequest.SinglePage
|
|
|
|
RenderRequest.OnlyJson
|
|
|
|
(RenderRequest.Page
|
|
|
|
{ path = Path.fromString (pageToLoad |> String.join "/")
|
|
|
|
, frontmatter = Route (pageToLoad |> String.join "/")
|
|
|
|
}
|
|
|
|
)
|
|
|
|
(Encode.object [])
|
|
|
|
)
|
2022-05-12 01:49:04 +03:00
|
|
|
(config [] pages)
|
2022-12-23 02:17:21 +03:00
|
|
|
, update = update
|
2021-06-08 19:54:10 +03:00
|
|
|
, view = \_ -> { title = "", body = [] }
|
|
|
|
}
|
|
|
|
|> ProgramTest.withSimulatedEffects simulateEffects
|
2021-06-23 18:22:07 +03:00
|
|
|
|> ProgramTest.withSimulatedSubscriptions simulateSubscriptions
|
2021-06-08 19:54:10 +03:00
|
|
|
|> ProgramTest.start (flags (Encode.encode 0 encodedFlags))
|
|
|
|
|
|
|
|
|
2019-11-13 05:22:36 +03:00
|
|
|
flags : String -> JD.Value
|
2019-10-24 18:26:41 +03:00
|
|
|
flags jsonString =
|
2019-11-13 05:22:36 +03:00
|
|
|
case JD.decodeString JD.value jsonString of
|
2019-10-24 18:26:41 +03:00
|
|
|
Ok value ->
|
|
|
|
value
|
|
|
|
|
|
|
|
Err _ ->
|
|
|
|
Debug.todo "Invalid JSON value."
|
2019-10-22 04:49:46 +03:00
|
|
|
|
|
|
|
|
2021-07-29 20:03:42 +03:00
|
|
|
sendToJsPort : ToJsPayload.ToJsSuccessPayloadNewCombined -> ProgramTest.SimulatedEffect msg
|
2021-06-23 18:22:07 +03:00
|
|
|
sendToJsPort value =
|
|
|
|
SimulatedEffect.Ports.send "toJsPort" (value |> Codec.encoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
|
|
|
|
|
2021-04-16 18:04:15 +03:00
|
|
|
simulateEffects : Effect -> ProgramTest.SimulatedEffect Msg
|
2019-10-22 05:31:17 +03:00
|
|
|
simulateEffects effect =
|
2022-02-27 19:50:43 +03:00
|
|
|
case effect of
|
2020-06-12 06:57:19 +03:00
|
|
|
Effect.NoEffect ->
|
2019-10-22 05:31:17 +03:00
|
|
|
SimulatedEffect.Cmd.none
|
|
|
|
|
|
|
|
-- toJsPort value |> Cmd.map never
|
2020-06-12 06:57:19 +03:00
|
|
|
Effect.Batch list ->
|
2019-10-22 05:31:17 +03:00
|
|
|
list
|
|
|
|
|> List.map simulateEffects
|
|
|
|
|> SimulatedEffect.Cmd.batch
|
|
|
|
|
2023-01-18 02:46:40 +03:00
|
|
|
Effect.FetchHttp requests ->
|
|
|
|
requests
|
|
|
|
|> List.map
|
|
|
|
(\request -> ( Request.hash request, request ))
|
|
|
|
|> ToJsPayload.DoHttp
|
|
|
|
|> sendToJsPort
|
|
|
|
|> SimulatedEffect.Cmd.map never
|
2019-10-22 05:31:17 +03:00
|
|
|
|
2022-02-26 03:57:50 +03:00
|
|
|
Effect.SendSinglePage info ->
|
2020-10-14 07:02:12 +03:00
|
|
|
SimulatedEffect.Cmd.batch
|
2020-10-18 04:58:17 +03:00
|
|
|
[ info
|
2020-10-20 03:41:27 +03:00
|
|
|
|> Codec.encoder (ToJsPayload.successCodecNew2 "" "")
|
2020-10-18 04:58:17 +03:00
|
|
|
|> SimulatedEffect.Ports.send "toJsPort"
|
2022-02-26 03:57:50 +03:00
|
|
|
, SimulatedEffect.Cmd.none
|
2020-10-14 07:02:12 +03:00
|
|
|
]
|
2020-10-10 03:08:02 +03:00
|
|
|
|
2022-02-26 03:57:50 +03:00
|
|
|
Effect.SendSinglePageNew _ toJsSuccessPayloadNewCombined ->
|
2022-02-08 01:50:38 +03:00
|
|
|
SimulatedEffect.Cmd.batch
|
|
|
|
[ toJsSuccessPayloadNewCombined
|
|
|
|
|> Codec.encoder (ToJsPayload.successCodecNew2 "" "")
|
|
|
|
|> SimulatedEffect.Ports.send "toJsPort"
|
2022-02-26 03:57:50 +03:00
|
|
|
, SimulatedEffect.Cmd.none
|
2022-02-08 01:50:38 +03:00
|
|
|
]
|
|
|
|
|
2019-10-22 05:31:17 +03:00
|
|
|
|
2021-07-28 18:06:05 +03:00
|
|
|
expectErrorsPort : String -> List ToJsPayload.ToJsSuccessPayloadNewCombined -> Expect.Expectation
|
2019-11-03 00:46:52 +03:00
|
|
|
expectErrorsPort expectedPlainString actualPorts =
|
2021-07-29 19:17:36 +03:00
|
|
|
case actualPorts |> List.reverse |> List.head of
|
|
|
|
Just (ToJsPayload.Errors actualRichTerminalString) ->
|
2020-01-28 02:28:42 +03:00
|
|
|
actualRichTerminalString
|
2021-04-16 19:20:28 +03:00
|
|
|
|> List.map .title
|
|
|
|
|> String.join "\n"
|
2020-01-28 02:28:42 +03:00
|
|
|
|> normalizeErrorExpectEqual expectedPlainString
|
|
|
|
|
2021-07-29 19:17:36 +03:00
|
|
|
Nothing ->
|
2020-10-07 21:06:44 +03:00
|
|
|
Expect.fail "Expected single error port. Didn't receive any ports."
|
|
|
|
|
2020-01-28 02:28:42 +03:00
|
|
|
_ ->
|
|
|
|
Expect.fail <| "Expected single error port. Got\n" ++ String.join "\n\n" (List.map Debug.toString actualPorts)
|
|
|
|
|
|
|
|
|
|
|
|
normalizeErrorExpectEqual : String -> String -> Expect.Expectation
|
|
|
|
normalizeErrorExpectEqual expectedPlainString actualRichTerminalString =
|
|
|
|
actualRichTerminalString
|
|
|
|
|> Regex.replace
|
2021-07-28 18:06:05 +03:00
|
|
|
-- strip out all possible ANSI sequences
|
|
|
|
(Regex.fromString "(\\x9B|\\x1B\\[)[0-?]*[ -/]*[@-~]"
|
2020-01-28 02:28:42 +03:00
|
|
|
|> Maybe.withDefault Regex.never
|
|
|
|
)
|
|
|
|
(\_ -> "")
|
2021-07-28 18:06:05 +03:00
|
|
|
|> String.replace "\u{001B}" ""
|
2021-04-16 19:20:28 +03:00
|
|
|
|> normalizeNewlines
|
|
|
|
|> Expect.equal
|
|
|
|
(expectedPlainString |> normalizeNewlines)
|
|
|
|
|
|
|
|
|
|
|
|
normalizeNewlines : String -> String
|
|
|
|
normalizeNewlines string =
|
|
|
|
string
|
|
|
|
|> Regex.replace
|
|
|
|
(Regex.fromString "(\n)+" |> Maybe.withDefault Regex.never)
|
2021-04-16 19:27:20 +03:00
|
|
|
(\_ -> "")
|
|
|
|
|> Regex.replace
|
|
|
|
(Regex.fromString "( )+" |> Maybe.withDefault Regex.never)
|
|
|
|
(\_ -> " ")
|
2021-07-28 18:06:05 +03:00
|
|
|
|> String.replace "\u{000D}" ""
|
|
|
|
|> Regex.replace
|
|
|
|
(Regex.fromString "\\s" |> Maybe.withDefault Regex.never)
|
|
|
|
(\_ -> "")
|
2019-11-03 00:46:52 +03:00
|
|
|
|
|
|
|
|
2021-06-04 03:59:58 +03:00
|
|
|
toJsPort : a -> Cmd msg
|
2021-04-03 00:49:18 +03:00
|
|
|
toJsPort _ =
|
2019-10-22 04:49:46 +03:00
|
|
|
Cmd.none
|
|
|
|
|
|
|
|
|
2021-06-04 03:59:58 +03:00
|
|
|
fromJsPort : Sub msg
|
2020-04-17 07:31:21 +03:00
|
|
|
fromJsPort =
|
|
|
|
Sub.none
|
|
|
|
|
|
|
|
|
2020-05-12 02:42:54 +03:00
|
|
|
starDecoder : Decoder Int
|
2019-11-13 05:22:36 +03:00
|
|
|
starDecoder =
|
2022-01-28 07:41:54 +03:00
|
|
|
JD.field "stargazer_count" JD.int
|
2020-01-03 23:18:18 +03:00
|
|
|
|
|
|
|
|
2021-07-28 18:06:05 +03:00
|
|
|
expectSuccess : List ( Request.Request, String ) -> ProgramTest model msg effect -> Expect.Expectation
|
2020-01-03 23:18:18 +03:00
|
|
|
expectSuccess expectedRequests previous =
|
2020-06-16 05:42:04 +03:00
|
|
|
expectSuccessNew expectedRequests [] previous
|
|
|
|
|
|
|
|
|
2021-07-28 18:06:05 +03:00
|
|
|
expectSuccessNew : List ( Request.Request, String ) -> List (ToJsPayload.ToJsSuccessPayloadNew -> Expect.Expectation) -> ProgramTest model msg effect -> Expect.Expectation
|
|
|
|
expectSuccessNew expectedRequest expectations previous =
|
2020-01-03 23:18:18 +03:00
|
|
|
previous
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
2021-07-28 18:06:05 +03:00
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
2020-04-19 18:17:51 +03:00
|
|
|
(\value ->
|
|
|
|
case value of
|
2021-07-28 18:06:05 +03:00
|
|
|
(ToJsPayload.PageProgress portPayload) :: _ ->
|
|
|
|
let
|
|
|
|
singleExpectation : ToJsPayload.ToJsSuccessPayloadNew -> Expect.Expectation
|
|
|
|
singleExpectation =
|
|
|
|
\subject ->
|
|
|
|
subject.contentJson
|
|
|
|
|> Expect.equal
|
|
|
|
(expectedRequest
|
2020-04-19 18:17:51 +03:00
|
|
|
|> List.map
|
2021-07-28 18:06:05 +03:00
|
|
|
(\( request, response ) ->
|
|
|
|
( Request.hash request, response )
|
2020-04-19 18:17:51 +03:00
|
|
|
)
|
|
|
|
|> Dict.fromList
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
2021-07-28 18:06:05 +03:00
|
|
|
in
|
|
|
|
portPayload
|
|
|
|
|> Expect.all
|
|
|
|
(singleExpectation
|
2020-06-16 05:42:04 +03:00
|
|
|
:: expectations
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
2020-04-19 18:17:51 +03:00
|
|
|
|
2021-04-03 23:07:28 +03:00
|
|
|
[ errorPort ] ->
|
|
|
|
Expect.fail <| "Expected success port. Got:\n" ++ Debug.toString errorPort
|
2020-04-19 18:17:51 +03:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail ("Expected ports to be called once, but instead there were " ++ String.fromInt (List.length value) ++ " calls.")
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-06-23 18:22:07 +03:00
|
|
|
simulateSubscriptions : a -> ProgramTest.SimulatedSub Msg
|
|
|
|
simulateSubscriptions _ =
|
2022-02-26 03:57:50 +03:00
|
|
|
-- TODO handle build errors or not needed?
|
|
|
|
SimulatedEffect.Ports.subscribe "gotBatchSub"
|
2023-01-07 06:31:55 +03:00
|
|
|
(JD.value |> JD.map GotDataBatch)
|
2021-06-23 18:22:07 +03:00
|
|
|
identity
|
|
|
|
|
|
|
|
|
2020-01-03 23:18:18 +03:00
|
|
|
get : String -> Request.Request
|
|
|
|
get url =
|
|
|
|
{ method = "GET"
|
|
|
|
, url = url
|
2022-02-27 01:36:03 +03:00
|
|
|
, headers = []
|
2023-01-02 01:44:11 +03:00
|
|
|
, body = BackendTask.Http.emptyBody
|
2023-01-08 02:31:54 +03:00
|
|
|
, cacheOptions = Nothing
|
2020-01-03 23:18:18 +03:00
|
|
|
}
|
2021-07-29 19:17:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
post : String -> Request.Request
|
|
|
|
post url =
|
|
|
|
{ method = "POST"
|
|
|
|
, url = url
|
2022-02-27 01:36:03 +03:00
|
|
|
, headers = []
|
2023-01-02 01:44:11 +03:00
|
|
|
, body = BackendTask.Http.emptyBody
|
2023-01-08 02:31:54 +03:00
|
|
|
, cacheOptions = Nothing
|
2021-07-29 19:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-27 19:50:43 +03:00
|
|
|
simulateHttp : Request.Request -> ResponseBody -> ProgramTest model msg effect -> ProgramTest model msg effect
|
2021-07-29 19:17:36 +03:00
|
|
|
simulateHttp request response program =
|
|
|
|
program
|
|
|
|
|> ProgramTest.ensureOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
2023-01-18 02:46:40 +03:00
|
|
|
[ ToJsPayload.DoHttp _ ] ->
|
2021-07-29 19:17:36 +03:00
|
|
|
Expect.pass
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail <|
|
|
|
|
"Expected an HTTP request, got:\n"
|
|
|
|
++ Debug.toString actualPorts
|
|
|
|
)
|
2022-02-26 03:57:50 +03:00
|
|
|
|> ProgramTest.simulateIncomingPort "gotBatchSub"
|
2023-01-07 06:31:55 +03:00
|
|
|
(Encode.object [ encodeBatchEntry ( request, response ) ])
|
2021-07-29 19:17:36 +03:00
|
|
|
|
|
|
|
|
2022-02-27 01:10:31 +03:00
|
|
|
simulateMultipleHttp : List ( Request.Request, ResponseBody ) -> ProgramTest model msg effect -> ProgramTest model msg effect
|
2021-07-29 19:17:36 +03:00
|
|
|
simulateMultipleHttp requests program =
|
|
|
|
program
|
|
|
|
|> ProgramTest.ensureOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
2023-01-18 02:46:40 +03:00
|
|
|
(ToJsPayload.DoHttp _) :: _ ->
|
2021-07-29 19:17:36 +03:00
|
|
|
-- TODO check count of HTTP requests, and check the URLs
|
|
|
|
Expect.pass
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail <|
|
|
|
|
"Expected an HTTP request, got:\n"
|
|
|
|
++ Debug.toString actualPorts
|
|
|
|
)
|
2022-02-26 03:57:50 +03:00
|
|
|
|> ProgramTest.simulateIncomingPort "gotBatchSub"
|
|
|
|
(requests
|
2023-01-07 06:31:55 +03:00
|
|
|
|> List.map encodeBatchEntry
|
|
|
|
|> Encode.object
|
2021-07-29 19:17:36 +03:00
|
|
|
)
|
2022-02-26 18:44:16 +03:00
|
|
|
|
|
|
|
|
2022-02-27 01:10:31 +03:00
|
|
|
jsonBody : String -> ResponseBody
|
|
|
|
jsonBody jsonString =
|
|
|
|
JsonBody
|
|
|
|
(jsonString
|
|
|
|
|> JD.decodeString JD.value
|
|
|
|
|> Result.withDefault Encode.null
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-01-07 06:31:55 +03:00
|
|
|
encodeBatchEntry : ( Request.Request, ResponseBody ) -> ( String, Encode.Value )
|
|
|
|
encodeBatchEntry ( req, response ) =
|
2023-01-07 20:48:40 +03:00
|
|
|
( Request.hash (req |> withInternalHeader response)
|
2023-01-07 06:31:55 +03:00
|
|
|
, Encode.object
|
2022-02-26 18:44:16 +03:00
|
|
|
[ ( "request"
|
2022-02-27 01:10:31 +03:00
|
|
|
, Codec.encodeToValue Request.codec
|
|
|
|
(withInternalHeader response req)
|
2022-02-26 18:44:16 +03:00
|
|
|
)
|
|
|
|
, ( "response"
|
2022-02-27 01:10:31 +03:00
|
|
|
, RequestsAndPending.bodyEncoder response
|
2022-02-26 18:44:16 +03:00
|
|
|
)
|
|
|
|
]
|
2023-01-07 06:31:55 +03:00
|
|
|
)
|
2022-02-27 01:10:31 +03:00
|
|
|
|
|
|
|
|
2022-02-27 19:50:43 +03:00
|
|
|
withInternalHeader : ResponseBody -> { a | headers : List ( String, String ) } -> { a | headers : List ( String, String ) }
|
2022-02-27 01:10:31 +03:00
|
|
|
withInternalHeader res req =
|
|
|
|
{ req
|
|
|
|
| headers =
|
|
|
|
( "elm-pages-internal"
|
|
|
|
, case res of
|
|
|
|
JsonBody _ ->
|
|
|
|
"ExpectJson"
|
|
|
|
|
|
|
|
BytesBody _ ->
|
|
|
|
"ExpectBytes"
|
|
|
|
|
|
|
|
StringBody _ ->
|
|
|
|
"ExpectString"
|
|
|
|
|
|
|
|
WhateverBody ->
|
|
|
|
"ExpectWhatever"
|
|
|
|
)
|
|
|
|
:: req.headers
|
|
|
|
}
|