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
|
2019-10-22 08:00:15 +03:00
|
|
|
import Codec
|
2021-06-04 03:57:57 +03:00
|
|
|
import DataSource exposing (DataSource)
|
2021-06-23 18:22:07 +03:00
|
|
|
import DataSource.File
|
|
|
|
import DataSource.Glob as Glob
|
2021-04-20 23:52:54 +03:00
|
|
|
import DataSource.Http
|
2020-12-07 19:41:10 +03:00
|
|
|
import Dict
|
2019-10-22 06:21:52 +03:00
|
|
|
import Expect
|
2019-10-22 04:49:46 +03:00
|
|
|
import Html
|
2019-11-13 05:22:36 +03:00
|
|
|
import Json.Decode as JD
|
2020-04-19 18:17:51 +03:00
|
|
|
import Json.Encode as Encode
|
2020-04-17 07:31:21 +03:00
|
|
|
import OptimizedDecoder as Decode exposing (Decoder)
|
2021-06-04 03:57:57 +03:00
|
|
|
import Pages.ContentCache as ContentCache exposing (ContentCache)
|
2021-07-31 01:23:36 +03:00
|
|
|
import Pages.Internal.NotFoundReason
|
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
|
2020-03-04 19:17:06 +03:00
|
|
|
import Pages.Internal.StaticHttpBody as StaticHttpBody
|
2019-10-22 04:49:46 +03:00
|
|
|
import Pages.Manifest as Manifest
|
2021-04-16 18:04:15 +03:00
|
|
|
import Pages.ProgramConfig exposing (ProgramConfig)
|
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
|
2019-11-12 04:48:08 +03:00
|
|
|
import Secrets
|
2019-10-22 05:31:17 +03:00
|
|
|
import SimulatedEffect.Cmd
|
2019-10-22 06:21:52 +03:00
|
|
|
import SimulatedEffect.Ports
|
2020-10-14 01:40:36 +03:00
|
|
|
import SimulatedEffect.Task
|
2021-07-27 19:39:06 +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 []
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2019-10-22 22:43:26 +03:00
|
|
|
"""{ "stargazer_count": 86 }"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":86}"""
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
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" ]
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2021-04-16 18:26:59 +03:00
|
|
|
"""{ "stargazer_count": 86 }"""
|
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":86}"""
|
2021-04-16 18:26:59 +03:00
|
|
|
)
|
|
|
|
]
|
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" ]
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2021-06-08 20:36:05 +03:00
|
|
|
"""{ "stargazer_count": 86 }"""
|
2021-06-08 19:54:10 +03:00
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
|
|
|
[ ToJsPayload.PageProgress portData ] ->
|
|
|
|
portData.is404
|
|
|
|
|> Expect.false "Expected page to be found and rendered"
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail <| "Expected exactly 1 port of type PageProgress. Instead, got \n" ++ Debug.toString actualPorts
|
|
|
|
)
|
|
|
|
, test "data sources are not resolved 404 pages with matching route but not pre-rendered" <|
|
|
|
|
\() ->
|
|
|
|
startWithRoutes [ "post-2" ]
|
|
|
|
[ [ "post-1" ]
|
|
|
|
]
|
|
|
|
[]
|
|
|
|
[ ( [ "post-2" ]
|
|
|
|
, DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
|
|
|
[ ToJsPayload.PageProgress portData ] ->
|
|
|
|
portData.is404
|
|
|
|
|> Expect.true "Expected 404 not found page"
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
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" ]
|
|
|
|
(DataSource.map2 Tuple.pair
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages")
|
|
|
|
(Decode.field "stargazer_count" Decode.int)
|
|
|
|
)
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages")
|
|
|
|
(Decode.field "language" Decode.string)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2021-06-01 06:59:30 +03:00
|
|
|
"""{ "stargazer_count": 86, "language": "Elm" }"""
|
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":86,"language":"Elm"}"""
|
2021-06-01 06:59:30 +03:00
|
|
|
)
|
|
|
|
]
|
2019-11-01 23:29:09 +03:00
|
|
|
, test "andThen" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple
|
|
|
|
[ "elm-pages" ]
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.succeed ())
|
|
|
|
|> DataSource.andThen
|
|
|
|
(\_ ->
|
|
|
|
DataSource.Http.get (Secrets.succeed "NEXT-REQUEST") (Decode.succeed ())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2019-11-01 23:29:09 +03:00
|
|
|
"""null"""
|
2021-07-29 19:17:36 +03:00
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "NEXT-REQUEST"))
|
2019-11-01 23:29:09 +03:00
|
|
|
"""null"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """null"""
|
|
|
|
)
|
|
|
|
, ( get "NEXT-REQUEST"
|
|
|
|
, """null"""
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2019-11-12 20:59:37 +03:00
|
|
|
, test "andThen chain avoids repeat requests" <|
|
|
|
|
\() ->
|
|
|
|
let
|
2021-06-04 03:57:57 +03:00
|
|
|
getReq : String -> Decoder a -> DataSource a
|
2020-01-03 23:18:18 +03:00
|
|
|
getReq url decoder =
|
2021-04-20 23:52:54 +03:00
|
|
|
DataSource.Http.request
|
2020-01-04 00:22:53 +03:00
|
|
|
(Secrets.succeed (get url))
|
2019-11-12 20:59:37 +03:00
|
|
|
decoder
|
|
|
|
|
2021-06-04 03:57:57 +03:00
|
|
|
pokemonDetailRequest : DataSource ()
|
2019-11-12 20:59:37 +03:00
|
|
|
pokemonDetailRequest =
|
2020-01-03 23:18:18 +03:00
|
|
|
getReq
|
2019-11-12 20:59:37 +03:00
|
|
|
"https://pokeapi.co/api/v2/pokemon/"
|
2019-11-13 05:22:36 +03:00
|
|
|
(Decode.list
|
|
|
|
(Decode.field "url" Decode.string
|
|
|
|
|> Decode.map
|
2019-11-12 20:59:37 +03:00
|
|
|
(\url ->
|
2020-01-03 23:18:18 +03:00
|
|
|
getReq url
|
2019-11-13 05:22:36 +03:00
|
|
|
(Decode.field "image" Decode.string)
|
2019-11-12 20:59:37 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2021-04-20 17:31:19 +03:00
|
|
|
|> DataSource.resolve
|
|
|
|
|> DataSource.map (\_ -> ())
|
2019-11-12 20:59:37 +03:00
|
|
|
in
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple
|
|
|
|
[ "elm-pages" ]
|
|
|
|
pokemonDetailRequest
|
|
|
|
|> simulateMultipleHttp
|
|
|
|
[ ( Secrets.succeed (get "https://pokeapi.co/api/v2/pokemon/")
|
|
|
|
, """[
|
2019-11-12 20:59:37 +03:00
|
|
|
{"url": "url1"},
|
|
|
|
{"url": "url2"},
|
|
|
|
{"url": "url3"},
|
|
|
|
{"url": "url4"},
|
|
|
|
{"url": "url5"},
|
|
|
|
{"url": "url6"},
|
|
|
|
{"url": "url7"},
|
|
|
|
{"url": "url8"},
|
|
|
|
{"url": "url9"},
|
|
|
|
{"url": "url10"}
|
|
|
|
]"""
|
2021-07-29 19:17:36 +03:00
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "url1")
|
|
|
|
, """{"image": "image1.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "url2")
|
|
|
|
, """{"image": "image2.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "url3")
|
|
|
|
, """{"image": "image3.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "url4")
|
|
|
|
, """{"image": "image4.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "url5")
|
|
|
|
, """{"image": "image5.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "url6")
|
|
|
|
, """{"image": "image6.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "url7")
|
|
|
|
, """{"image": "image7.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "url8")
|
|
|
|
, """{"image": "image8.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "url9")
|
|
|
|
, """{"image": "image9.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "url10")
|
|
|
|
, """{"image": "image10.jpg"}"""
|
|
|
|
)
|
|
|
|
]
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://pokeapi.co/api/v2/pokemon/"
|
|
|
|
, """[{"url":"url1"},{"url":"url2"},{"url":"url3"},{"url":"url4"},{"url":"url5"},{"url":"url6"},{"url":"url7"},{"url":"url8"},{"url":"url9"},{"url":"url10"}]"""
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
2021-07-28 18:06:05 +03:00
|
|
|
, ( get "url1"
|
|
|
|
, """{"image":"image1.jpg"}"""
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
2021-07-28 18:06:05 +03:00
|
|
|
, ( get "url2"
|
|
|
|
, """{"image":"image2.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( get "url3"
|
|
|
|
, """{"image":"image3.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( get "url4"
|
|
|
|
, """{"image":"image4.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( get "url5"
|
|
|
|
, """{"image":"image5.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( get "url6"
|
|
|
|
, """{"image":"image6.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( get "url7"
|
|
|
|
, """{"image":"image7.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( get "url8"
|
|
|
|
, """{"image":"image8.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( get "url9"
|
|
|
|
, """{"image":"image9.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( get "url10"
|
|
|
|
, """{"image":"image10.jpg"}"""
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2021-07-28 18:06:05 +03:00
|
|
|
|
|
|
|
--, test "port is sent out once all requests are finished" <|
|
|
|
|
-- \() ->
|
|
|
|
-- start
|
|
|
|
-- [ ( [ "elm-pages" ]
|
|
|
|
-- , DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
|
|
|
-- )
|
|
|
|
-- , ( [ "elm-pages-starter" ]
|
|
|
|
-- , DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages-starter") starDecoder
|
|
|
|
-- )
|
|
|
|
-- ]
|
|
|
|
-- |> 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}"""
|
|
|
|
-- )
|
|
|
|
-- ]
|
|
|
|
-- )
|
|
|
|
-- ]
|
2019-11-05 00:05:12 +03:00
|
|
|
, test "reduced JSON is sent out" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple []
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.field "stargazer_count" Decode.int))
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2019-11-05 00:05:12 +03:00
|
|
|
"""{ "stargazer_count": 86, "unused_field": 123 }"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":86}"""
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2020-03-02 06:09:39 +03:00
|
|
|
, test "you can use elm/json decoders with StaticHttp.unoptimizedRequest" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple []
|
|
|
|
(DataSource.Http.unoptimizedRequest
|
|
|
|
(Secrets.succeed
|
|
|
|
{ url = "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
2021-07-30 01:57:44 +03:00
|
|
|
, body = DataSource.Http.emptyBody
|
2021-07-29 19:17:36 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
(DataSource.Http.expectUnoptimizedJson
|
|
|
|
(JD.field "stargazer_count" JD.int)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2020-03-02 06:09:39 +03:00
|
|
|
"""{ "stargazer_count": 86, "unused_field": 123 }"""
|
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{ "stargazer_count": 86, "unused_field": 123 }"""
|
2020-03-02 06:09:39 +03:00
|
|
|
)
|
|
|
|
]
|
2020-03-03 18:54:16 +03:00
|
|
|
, test "plain string" <|
|
2020-03-03 09:08:00 +03:00
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple []
|
|
|
|
(DataSource.Http.unoptimizedRequest
|
|
|
|
(Secrets.succeed
|
|
|
|
{ url = "https://example.com/file.txt"
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
2021-07-30 01:57:44 +03:00
|
|
|
, body = DataSource.Http.emptyBody
|
2021-07-29 19:17:36 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
(DataSource.Http.expectString Ok)
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://example.com/file.txt"))
|
2020-03-03 09:08:00 +03:00
|
|
|
"This is a raw text file."
|
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://example.com/file.txt"
|
|
|
|
, "This is a raw text file."
|
2020-03-03 09:08:00 +03:00
|
|
|
)
|
|
|
|
]
|
2020-03-03 18:54:16 +03:00
|
|
|
, test "Err in String to Result function turns into decode error" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple []
|
|
|
|
(DataSource.Http.unoptimizedRequest
|
|
|
|
(Secrets.succeed
|
|
|
|
{ url = "https://example.com/file.txt"
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
2021-07-30 01:57:44 +03:00
|
|
|
, body = DataSource.Http.emptyBody
|
2021-07-29 19:17:36 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
(DataSource.Http.expectString
|
|
|
|
(\string ->
|
|
|
|
if String.toUpper string == string then
|
|
|
|
Ok string
|
2020-03-03 18:54:16 +03:00
|
|
|
|
2021-07-29 19:17:36 +03:00
|
|
|
else
|
|
|
|
Err "String was not uppercased"
|
2020-03-03 18:54:16 +03:00
|
|
|
)
|
2021-07-29 19:17:36 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://example.com/file.txt"))
|
2020-03-03 18:54:16 +03:00
|
|
|
"This is a raw text file."
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
2021-07-28 18:06:05 +03:00
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
2020-03-03 18:54:16 +03:00
|
|
|
(expectErrorsPort
|
|
|
|
"""-- STATIC HTTP DECODING ERROR ----------------------------------------------------- elm-pages
|
|
|
|
|
2020-03-11 19:03:47 +03:00
|
|
|
|
2020-03-03 18:54:16 +03:00
|
|
|
|
|
|
|
String was not uppercased"""
|
|
|
|
)
|
2019-11-08 22:34:51 +03:00
|
|
|
, test "POST method works" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple []
|
|
|
|
(DataSource.Http.request
|
|
|
|
(Secrets.succeed
|
|
|
|
{ method = "POST"
|
|
|
|
, url = "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, headers = []
|
2021-07-30 01:57:44 +03:00
|
|
|
, body = DataSource.Http.emptyBody
|
2021-07-29 19:17:36 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
(Decode.field "stargazer_count" Decode.int)
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (post "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2019-11-08 22:34:51 +03:00
|
|
|
"""{ "stargazer_count": 86, "unused_field": 123 }"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( { method = "POST"
|
|
|
|
, url = "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, headers = []
|
2021-07-30 01:57:44 +03:00
|
|
|
, body = DataSource.Http.emptyBody
|
2021-07-28 18:06:05 +03:00
|
|
|
}
|
|
|
|
, """{"stargazer_count":86}"""
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2019-11-05 00:24:08 +03:00
|
|
|
, test "json is reduced from andThen chains" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple []
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.field "stargazer_count" Decode.int)
|
|
|
|
|> DataSource.andThen
|
|
|
|
(\_ ->
|
|
|
|
DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages-starter") (Decode.field "stargazer_count" Decode.int)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2019-11-05 00:48:30 +03:00
|
|
|
"""{ "stargazer_count": 100, "unused_field": 123 }"""
|
2021-07-29 19:17:36 +03:00
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages-starter"))
|
2019-11-05 00:48:30 +03:00
|
|
|
"""{ "stargazer_count": 50, "unused_field": 456 }"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":100}"""
|
|
|
|
)
|
|
|
|
, ( get "https://api.github.com/repos/dillonkearns/elm-pages-starter"
|
|
|
|
, """{"stargazer_count":50}"""
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2019-11-05 00:48:30 +03:00
|
|
|
, test "reduced json is preserved by StaticHttp.map2" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple []
|
|
|
|
(DataSource.map2 (\_ _ -> ())
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.field "stargazer_count" Decode.int))
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages-starter") (Decode.field "stargazer_count" Decode.int))
|
|
|
|
)
|
|
|
|
|> simulateMultipleHttp
|
|
|
|
[ ( Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages")
|
|
|
|
, """{ "stargazer_count": 100, "unused_field": 123 }"""
|
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages-starter")
|
|
|
|
, """{ "stargazer_count": 50, "unused_field": 456 }"""
|
|
|
|
)
|
|
|
|
]
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":100}"""
|
|
|
|
)
|
|
|
|
, ( get "https://api.github.com/repos/dillonkearns/elm-pages-starter"
|
|
|
|
, """{"stargazer_count":50}"""
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2019-10-28 18:04:34 +03:00
|
|
|
, test "the port sends out even if there are no http requests" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
2021-04-20 17:31:19 +03:00
|
|
|
, DataSource.succeed ()
|
2019-10-28 18:04:34 +03:00
|
|
|
)
|
|
|
|
]
|
2021-07-28 18:06:05 +03:00
|
|
|
|> expectSuccess []
|
2019-10-30 07:45:23 +03:00
|
|
|
, test "the port sends out when there are duplicate http requests for the same page" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple []
|
|
|
|
(DataSource.map2 (\_ _ -> ())
|
|
|
|
(DataSource.Http.get (Secrets.succeed "http://example.com") (Decode.succeed ()))
|
|
|
|
(DataSource.Http.get (Secrets.succeed "http://example.com") (Decode.succeed ()))
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "http://example.com"))
|
2019-10-30 07:45:23 +03:00
|
|
|
"""null"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "http://example.com"
|
|
|
|
, """null"""
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2019-10-24 15:52:24 +03:00
|
|
|
, test "an error is sent out for decoder failures" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple [ "elm-pages" ]
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.fail "The user should get this message from the CLI."))
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2019-10-24 15:52:24 +03:00
|
|
|
"""{ "stargazer_count": 86 }"""
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
2021-07-28 18:06:05 +03:00
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
2019-11-13 05:22:36 +03:00
|
|
|
(expectErrorsPort
|
2019-12-07 17:35:36 +03:00
|
|
|
"""-- STATIC HTTP DECODING ERROR ----------------------------------------------------- elm-pages
|
2019-10-30 08:33:47 +03:00
|
|
|
|
2019-11-13 05:22:36 +03:00
|
|
|
I encountered some errors while decoding this JSON:
|
2019-10-27 02:50:09 +03:00
|
|
|
|
2019-11-13 05:22:36 +03:00
|
|
|
The user should get this message from the CLI.
|
2019-11-02 21:13:33 +03:00
|
|
|
|
2019-11-13 05:22:36 +03:00
|
|
|
{
|
|
|
|
"stargazer_count": 86
|
|
|
|
}"""
|
2019-11-02 21:13:33 +03:00
|
|
|
)
|
2019-11-03 00:17:32 +03:00
|
|
|
, test "an error is sent for missing secrets from continuation requests" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple
|
|
|
|
[ "elm-pages" ]
|
|
|
|
(DataSource.Http.get
|
|
|
|
(Secrets.succeed
|
|
|
|
(\apiKey ->
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages?apiKey=" ++ apiKey
|
2019-11-03 00:17:32 +03:00
|
|
|
)
|
2021-07-29 19:17:36 +03:00
|
|
|
|> Secrets.with "API_KEY"
|
|
|
|
)
|
|
|
|
Decode.string
|
|
|
|
|> DataSource.andThen
|
|
|
|
(\url ->
|
|
|
|
DataSource.Http.get
|
|
|
|
(Secrets.succeed
|
|
|
|
(\missingSecret ->
|
|
|
|
url ++ "?apiKey=" ++ missingSecret
|
2019-11-03 00:17:32 +03:00
|
|
|
)
|
2021-07-29 19:17:36 +03:00
|
|
|
|> Secrets.with "MISSING"
|
|
|
|
)
|
|
|
|
(Decode.succeed ())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed
|
|
|
|
(\apiKey ->
|
|
|
|
get
|
|
|
|
("https://api.github.com/repos/dillonkearns/elm-pages?apiKey=" ++ apiKey)
|
|
|
|
)
|
|
|
|
|> Secrets.with "API_KEY"
|
|
|
|
)
|
2019-11-03 00:17:32 +03:00
|
|
|
""" "continuation-url" """
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
2021-07-28 18:06:05 +03:00
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
2019-11-03 00:46:52 +03:00
|
|
|
(expectErrorsPort
|
|
|
|
"""-- MISSING SECRET ----------------------------------------------------- elm-pages
|
|
|
|
|
|
|
|
I expected to find this Secret in your environment variables but didn't find a match:
|
|
|
|
|
|
|
|
Secrets.get "MISSING"
|
|
|
|
^^^^^^^
|
|
|
|
|
|
|
|
So maybe MISSING should be API_KEY"""
|
2019-11-03 00:17:32 +03:00
|
|
|
)
|
2019-10-24 18:26:41 +03:00
|
|
|
, test "uses real secrets to perform request and masked secrets to store and lookup response" <|
|
|
|
|
\() ->
|
2021-07-29 19:32:51 +03:00
|
|
|
startSimple []
|
|
|
|
(DataSource.Http.request
|
|
|
|
(Secrets.succeed
|
|
|
|
(\apiKey bearer ->
|
|
|
|
{ url = "https://api.github.com/repos/dillonkearns/elm-pages?apiKey=" ++ apiKey
|
|
|
|
, method = "GET"
|
|
|
|
, headers = [ ( "Authorization", "Bearer " ++ bearer ) ]
|
2021-07-30 01:57:44 +03:00
|
|
|
, body = DataSource.Http.emptyBody
|
2021-07-29 19:32:51 +03:00
|
|
|
}
|
2019-10-28 17:57:21 +03:00
|
|
|
)
|
2021-07-29 19:32:51 +03:00
|
|
|
|> Secrets.with "API_KEY"
|
|
|
|
|> Secrets.with "BEARER"
|
2019-11-12 03:56:25 +03:00
|
|
|
)
|
2021-07-29 19:32:51 +03:00
|
|
|
(Decode.succeed ())
|
|
|
|
)
|
|
|
|
|> simulateHttpAssert
|
|
|
|
(Secrets.succeed
|
|
|
|
(\apiKey bearer ->
|
|
|
|
{ url = "https://api.github.com/repos/dillonkearns/elm-pages?apiKey=" ++ apiKey
|
|
|
|
, method = "GET"
|
|
|
|
, headers = [ ( "Authorization", "Bearer " ++ bearer ) ]
|
2021-07-30 01:57:44 +03:00
|
|
|
, body = DataSource.Http.emptyBody
|
2021-07-29 19:32:51 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|> Secrets.with "API_KEY"
|
|
|
|
|> Secrets.with "BEARER"
|
|
|
|
)
|
|
|
|
"""{ "stargazer_count": 86 }"""
|
|
|
|
(Expect.all
|
|
|
|
[ \requests ->
|
|
|
|
requests.unmasked.headers
|
|
|
|
|> Expect.equal [ ( "Authorization", "Bearer XYZ789" ) ]
|
|
|
|
, \requests ->
|
|
|
|
requests.unmasked.url
|
|
|
|
|> Expect.equal "https://api.github.com/repos/dillonkearns/elm-pages?apiKey=ABCD1234"
|
|
|
|
]
|
2019-11-12 03:56:25 +03:00
|
|
|
)
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( { method = "GET"
|
|
|
|
, url = "https://api.github.com/repos/dillonkearns/elm-pages?apiKey=<API_KEY>"
|
|
|
|
, headers =
|
|
|
|
[ ( "Authorization", "Bearer <BEARER>" )
|
|
|
|
]
|
2021-07-30 01:57:44 +03:00
|
|
|
, body = DataSource.Http.emptyBody
|
2021-07-28 18:06:05 +03:00
|
|
|
}
|
|
|
|
, """{}"""
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2020-04-19 18:17:51 +03:00
|
|
|
, describe "staticHttpCache"
|
|
|
|
[ test "it doesn't perform http requests that are provided in the http cache flag" <|
|
|
|
|
\() ->
|
2021-06-04 03:14:25 +03:00
|
|
|
startWithHttpCache
|
2020-04-19 18:17:51 +03:00
|
|
|
[ ( { url = "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
|
|
|
, body = StaticHttpBody.EmptyBody
|
|
|
|
}
|
|
|
|
, """{"stargazer_count":86}"""
|
|
|
|
)
|
|
|
|
]
|
|
|
|
[ ( []
|
2021-04-20 23:52:54 +03:00
|
|
|
, DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
2020-04-19 18:17:51 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":86}"""
|
2020-04-19 18:17:51 +03:00
|
|
|
)
|
|
|
|
]
|
2020-04-19 20:35:17 +03:00
|
|
|
, test "it ignores unused cache" <|
|
|
|
|
\() ->
|
2021-07-29 19:38:46 +03:00
|
|
|
startSimpleWithCache []
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder)
|
2020-04-19 20:35:17 +03:00
|
|
|
[ ( { url = "https://this-is-never-used.example.com/"
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
|
|
|
, body = StaticHttpBody.EmptyBody
|
|
|
|
}
|
|
|
|
, """{"stargazer_count":86}"""
|
|
|
|
)
|
|
|
|
]
|
2021-07-29 19:38:46 +03:00
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2020-04-19 20:35:17 +03:00
|
|
|
"""{ "stargazer_count": 86 }"""
|
|
|
|
|> expectSuccess
|
2021-07-28 18:06:05 +03:00
|
|
|
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":86}"""
|
2020-04-19 20:35:17 +03:00
|
|
|
)
|
|
|
|
]
|
2021-06-17 01:23:16 +03:00
|
|
|
, test "validate DataSource is not stored for any pages" <|
|
|
|
|
\() ->
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple [ "hello" ]
|
|
|
|
(DataSource.succeed "hello"
|
|
|
|
|> DataSource.validate identity
|
|
|
|
(\word ->
|
|
|
|
DataSource.Http.get (Secrets.succeed ("https://api.spellchecker.com?word=" ++ word))
|
|
|
|
(Decode.field "isCorrect" Decode.bool
|
|
|
|
|> Decode.map
|
|
|
|
(\isCorrect ->
|
|
|
|
if isCorrect then
|
|
|
|
Ok ()
|
|
|
|
|
|
|
|
else
|
|
|
|
Err "Spelling error"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> DataSource.map (\_ -> ())
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.spellchecker.com?word=hello"))
|
2021-06-17 01:23:16 +03:00
|
|
|
"""{ "isCorrect": true }"""
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
|
|
|
[ ToJsPayload.PageProgress portData ] ->
|
|
|
|
portData.contentJson
|
|
|
|
|> Expect.equalDicts Dict.empty
|
|
|
|
|
2021-06-17 20:01:48 +03:00
|
|
|
_ ->
|
|
|
|
Expect.fail <| "Expected exactly 1 port of type PageProgress. Instead, got \n" ++ Debug.toString actualPorts
|
|
|
|
)
|
|
|
|
, test "distill stores encoded JSON but not original DataSource" <|
|
|
|
|
\() ->
|
2021-07-29 19:38:46 +03:00
|
|
|
startSimple [ "hello" ]
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
|
|
|
|> DataSource.distill "abc123" Encode.int (JD.decodeValue JD.int >> Result.mapError JD.errorToString)
|
|
|
|
|> DataSource.map (\_ -> ())
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2021-06-18 19:00:54 +03:00
|
|
|
"""{ "stargazer_count": 86 }"""
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
|
|
|
[ ToJsPayload.PageProgress portData ] ->
|
|
|
|
portData.contentJson
|
|
|
|
|> Expect.equalDicts (Dict.fromList [ ( "abc123", "86" ) ])
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail <| "Expected exactly 1 port of type PageProgress. Instead, got \n" ++ Debug.toString actualPorts
|
|
|
|
)
|
2021-06-23 18:22:07 +03:00
|
|
|
, test "distill with andThen chains resolves successfully" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
andThenExample : DataSource (List ( String, String ))
|
|
|
|
andThenExample =
|
|
|
|
Glob.succeed
|
|
|
|
identity
|
|
|
|
|> Glob.match (Glob.literal "content/glossary/")
|
|
|
|
|> Glob.capture Glob.wildcard
|
|
|
|
|> Glob.match (Glob.literal ".md")
|
|
|
|
|> Glob.toDataSource
|
|
|
|
|> DataSource.map
|
|
|
|
(List.map
|
|
|
|
(\topic ->
|
2021-07-29 22:37:32 +03:00
|
|
|
DataSource.File.bodyWithoutFrontmatter ("content/glossary/" ++ topic ++ ".md")
|
2021-06-23 18:22:07 +03:00
|
|
|
|> DataSource.map (Tuple.pair topic)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> DataSource.resolve
|
|
|
|
|> DataSource.map
|
|
|
|
(\allNotes ->
|
|
|
|
allNotes
|
|
|
|
|> List.map
|
|
|
|
(\note ->
|
|
|
|
DataSource.succeed note
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> DataSource.resolve
|
|
|
|
in
|
|
|
|
startWithRoutes [ "hello" ]
|
|
|
|
[ [ "hello" ] ]
|
|
|
|
[]
|
|
|
|
[ ( [ "hello" ]
|
|
|
|
, andThenExample
|
|
|
|
|> DataSource.map (\_ -> ())
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.ensureOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
|
|
|
[ ToJsPayload.Glob _ ] ->
|
|
|
|
Expect.pass
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail <|
|
|
|
|
"Expected a glob, but got\n"
|
|
|
|
++ (actualPorts
|
|
|
|
|> List.indexedMap
|
|
|
|
(\index item -> "(" ++ String.fromInt (index + 1) ++ ") " ++ Debug.toString item)
|
|
|
|
|> String.join "\n\n"
|
|
|
|
)
|
|
|
|
++ "\n\n"
|
|
|
|
)
|
|
|
|
|> ProgramTest.simulateIncomingPort "fromJsPort"
|
|
|
|
(Encode.object
|
|
|
|
[ ( "tag", Encode.string "GotGlob" )
|
|
|
|
, ( "data"
|
|
|
|
, Encode.object
|
|
|
|
[ ( "pattern", Encode.string "content/glossary/*.md" )
|
|
|
|
, ( "result", Encode.list Encode.string [ "content/glossary/hello.md" ] )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> ProgramTest.ensureOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
|
|
|
[ ToJsPayload.ReadFile _ ] ->
|
|
|
|
Expect.pass
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail <|
|
|
|
|
"Expected a ReadFile, but got\n"
|
|
|
|
++ (actualPorts
|
|
|
|
|> List.indexedMap
|
|
|
|
(\index item -> "(" ++ String.fromInt (index + 1) ++ ") " ++ Debug.toString item)
|
|
|
|
|> String.join "\n\n"
|
|
|
|
)
|
|
|
|
++ "\n\n"
|
|
|
|
)
|
|
|
|
|> ProgramTest.simulateIncomingPort "fromJsPort"
|
|
|
|
(Encode.object
|
|
|
|
[ ( "tag", Encode.string "GotFile" )
|
|
|
|
, ( "data"
|
|
|
|
, Encode.object
|
|
|
|
[ ( "filePath", Encode.string "content/glossary/hello.md" )
|
|
|
|
, ( "withoutFrontmatter", Encode.string "BODY" )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
|
|
|
[ {- ToJsPayload.Glob _, ToJsPayload.ReadFile _ -} ToJsPayload.PageProgress portData ] ->
|
|
|
|
portData.contentJson
|
|
|
|
|> Expect.equalDicts
|
2021-07-28 19:00:34 +03:00
|
|
|
(Dict.fromList
|
|
|
|
[ ( Request.hash
|
|
|
|
{ method = "GET"
|
|
|
|
, url = "file://content/glossary/hello.md"
|
|
|
|
, headers = []
|
|
|
|
, body = DataSource.Http.emptyBody
|
|
|
|
}
|
|
|
|
, "{\"withoutFrontmatter\":\"BODY\"}"
|
|
|
|
)
|
|
|
|
, ( Request.hash
|
|
|
|
{ method = "GET"
|
|
|
|
, url = "glob://content/glossary/*.md"
|
|
|
|
, headers = []
|
|
|
|
, body = DataSource.Http.emptyBody
|
|
|
|
}
|
|
|
|
, "[\"content/glossary/hello.md\"]"
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2021-06-23 18:22:07 +03:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail <|
|
|
|
|
"Expected exactly 1 port of type PageProgress. Instead, got \n\n"
|
|
|
|
++ (actualPorts
|
|
|
|
|> List.indexedMap
|
|
|
|
(\index item -> "(" ++ String.fromInt (index + 1) ++ ") " ++ Debug.toString item)
|
|
|
|
|> String.join "\n\n"
|
|
|
|
)
|
|
|
|
++ "\n\n"
|
|
|
|
)
|
2021-06-18 19:00:54 +03:00
|
|
|
, test "distill successfully merges data sources with same key and same encoded JSON" <|
|
|
|
|
\() ->
|
2021-07-29 19:38:46 +03:00
|
|
|
startSimple [ "hello" ]
|
|
|
|
(DataSource.map2 (\_ _ -> ())
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
|
|
|
|> DataSource.distill "abc123" Encode.int (JD.decodeValue JD.int >> Result.mapError JD.errorToString)
|
|
|
|
)
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
|
|
|
|> DataSource.distill "abc123" Encode.int (JD.decodeValue JD.int >> Result.mapError JD.errorToString)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> simulateHttp
|
|
|
|
(Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|
2021-06-17 20:01:48 +03:00
|
|
|
"""{ "stargazer_count": 86 }"""
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
|
|
|
[ ToJsPayload.PageProgress portData ] ->
|
|
|
|
portData.contentJson
|
|
|
|
|> Expect.equalDicts (Dict.fromList [ ( "abc123", "86" ) ])
|
|
|
|
|
2021-06-17 01:23:16 +03:00
|
|
|
_ ->
|
|
|
|
Expect.fail <| "Expected exactly 1 port of type PageProgress. Instead, got \n" ++ Debug.toString actualPorts
|
|
|
|
)
|
2021-06-17 21:55:22 +03:00
|
|
|
, test "distill gives an error if there are matching keys with different encoded JSON" <|
|
|
|
|
\() ->
|
2021-07-29 19:38:46 +03:00
|
|
|
startSimple [ "hello" ]
|
|
|
|
(DataSource.map2 (\_ _ -> ())
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
|
|
|
|> DataSource.distill "stars" Encode.int (JD.decodeValue JD.int >> Result.mapError JD.errorToString)
|
|
|
|
)
|
|
|
|
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-markdown") starDecoder
|
|
|
|
|> DataSource.distill "stars" Encode.int (JD.decodeValue JD.int >> Result.mapError JD.errorToString)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> simulateMultipleHttp
|
|
|
|
[ ( Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages")
|
|
|
|
, """{ "stargazer_count": 86 }"""
|
|
|
|
)
|
|
|
|
, ( Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-markdown")
|
|
|
|
, """{ "stargazer_count": 123 }"""
|
|
|
|
)
|
|
|
|
]
|
2021-06-17 21:55:22 +03:00
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
2021-07-28 18:06:05 +03:00
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
2021-06-17 21:55:22 +03:00
|
|
|
(expectErrorsPort """-- NON-UNIQUE DISTILL KEYS ----------------------------------------------------- elm-pages
|
|
|
|
I encountered DataSource.distill with two matching keys that had differing encoded values.
|
|
|
|
|
|
|
|
Look for DataSource.distill with the key "stars"
|
|
|
|
|
|
|
|
The first encoded value was:
|
2021-08-10 03:32:54 +03:00
|
|
|
123
|
2021-06-17 21:55:22 +03:00
|
|
|
-------------------------------
|
|
|
|
The second encoded value was:
|
|
|
|
|
2021-08-10 03:32:54 +03:00
|
|
|
86""")
|
2020-05-14 07:14:05 +03:00
|
|
|
]
|
2021-07-28 18:06:05 +03:00
|
|
|
|
|
|
|
--, describe "generateFiles"
|
|
|
|
-- [ test "initial requests are sent out" <|
|
|
|
|
-- \() ->
|
|
|
|
-- startLowLevel
|
|
|
|
-- [ ApiRoute.succeed
|
|
|
|
-- (DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages")
|
|
|
|
-- (starDecoder
|
|
|
|
-- |> Decode.map
|
|
|
|
-- (\starCount ->
|
|
|
|
-- { body = "Star count: " ++ String.fromInt starCount
|
|
|
|
-- }
|
|
|
|
-- )
|
|
|
|
-- )
|
|
|
|
-- )
|
|
|
|
-- |> ApiRoute.literal "test.txt"
|
|
|
|
-- |> ApiRoute.single
|
|
|
|
-- ]
|
|
|
|
-- []
|
|
|
|
-- []
|
|
|
|
-- |> ProgramTest.simulateHttpOk
|
|
|
|
-- "GET"
|
|
|
|
-- "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
-- """{ "stargazer_count": 86 }"""
|
|
|
|
-- |> expectSuccessNew
|
|
|
|
-- []
|
|
|
|
-- [ \success ->
|
|
|
|
-- success.filesToGenerate
|
|
|
|
-- |> Expect.equal
|
|
|
|
-- [ { path = [ "test.txt" ]
|
|
|
|
-- , content = "Star count: 86"
|
|
|
|
-- }
|
|
|
|
-- ]
|
|
|
|
-- ]
|
|
|
|
-- , test "it sends success port when no HTTP requests are needed because they're all cached" <|
|
|
|
|
-- \() ->
|
|
|
|
-- startLowLevel
|
|
|
|
-- [ ApiRoute.succeed
|
|
|
|
-- (DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages-starter")
|
|
|
|
-- (starDecoder
|
|
|
|
-- |> Decode.map
|
|
|
|
-- (\starCount ->
|
|
|
|
-- { body = "Star count: " ++ String.fromInt starCount
|
|
|
|
-- }
|
|
|
|
-- )
|
|
|
|
-- )
|
|
|
|
-- )
|
|
|
|
-- |> ApiRoute.literal "test.txt"
|
|
|
|
-- |> ApiRoute.single
|
|
|
|
-- ]
|
|
|
|
-- [ ( { url = "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
-- , method = "GET"
|
|
|
|
-- , headers = []
|
|
|
|
-- , body = StaticHttpBody.EmptyBody
|
|
|
|
-- }
|
|
|
|
-- , """{"stargazer_count":86}"""
|
|
|
|
-- )
|
|
|
|
-- , ( { url = "https://api.github.com/repos/dillonkearns/elm-pages-starter"
|
|
|
|
-- , method = "GET"
|
|
|
|
-- , headers = []
|
|
|
|
-- , body = StaticHttpBody.EmptyBody
|
|
|
|
-- }
|
|
|
|
-- , """{"stargazer_count":23}"""
|
|
|
|
-- )
|
|
|
|
-- ]
|
|
|
|
-- [ ( []
|
|
|
|
-- , DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
|
|
|
-- )
|
|
|
|
-- ]
|
|
|
|
-- |> expectSuccessNew
|
|
|
|
-- [ ( ""
|
|
|
|
-- , [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
-- , """{"stargazer_count":86}"""
|
|
|
|
-- )
|
|
|
|
-- ]
|
|
|
|
-- )
|
|
|
|
-- ]
|
|
|
|
-- [ \success ->
|
|
|
|
-- success.filesToGenerate
|
|
|
|
-- |> Expect.equal
|
|
|
|
-- [ { path = [ "test.txt" ]
|
|
|
|
-- , content = "Star count: 23"
|
|
|
|
-- }
|
|
|
|
-- ]
|
|
|
|
-- ]
|
|
|
|
-- ]
|
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
|
|
|
|
|
|
|
|
|
2021-06-04 03:57:57 +03:00
|
|
|
start : List ( List String, DataSource 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 )
|
2021-06-04 03:57:57 +03:00
|
|
|
-> List ( List String, DataSource 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 )
|
2021-06-04 03:57:57 +03:00
|
|
|
-> List ( List String, DataSource a )
|
2021-04-16 18:04:15 +03:00
|
|
|
-> ProgramTest (Model Route) Msg Effect
|
2021-06-04 03:14:25 +03:00
|
|
|
startLowLevel apiRoutes staticHttpCache pages =
|
2019-10-22 04:49:46 +03:00
|
|
|
let
|
2021-06-04 03:57:57 +03:00
|
|
|
contentCache : ContentCache
|
2019-10-22 04:49:46 +03:00
|
|
|
contentCache =
|
2021-04-03 07:43:57 +03:00
|
|
|
ContentCache.init Nothing
|
2019-10-22 04:49:46 +03:00
|
|
|
|
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-04-16 18:04:15 +03:00
|
|
|
config : ProgramConfig Msg () Route () () ()
|
2019-10-22 04:49:46 +03:00
|
|
|
config =
|
|
|
|
{ toJsPort = toJsPort
|
2020-04-17 07:31:21 +03:00
|
|
|
, fromJsPort = fromJsPort
|
2021-05-19 17:58:37 +03:00
|
|
|
, init = \_ _ _ _ _ -> ( (), Cmd.none )
|
2021-04-03 19:53:42 +03:00
|
|
|
, getStaticRoutes =
|
2021-04-16 18:26:59 +03:00
|
|
|
--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
|
2021-04-20 17:31:19 +03:00
|
|
|
|> DataSource.succeed
|
2021-06-09 01:17:56 +03:00
|
|
|
, handleRoute = \_ -> DataSource.succeed Nothing
|
2021-04-16 18:04:15 +03:00
|
|
|
, urlToRoute = .path >> Route
|
2021-04-22 21:33:06 +03:00
|
|
|
, update = \_ _ _ _ _ -> ( (), Cmd.none )
|
2021-07-27 20:41:18 +03:00
|
|
|
, basePath = []
|
2021-04-24 02:24:26 +03:00
|
|
|
, data =
|
2021-04-17 17:42:33 +03:00
|
|
|
\(Route pageRoute) ->
|
|
|
|
let
|
2021-06-04 03:57:57 +03:00
|
|
|
thing : Maybe (DataSource a)
|
2021-04-17 17:42:33 +03:00
|
|
|
thing =
|
|
|
|
pages
|
|
|
|
|> Dict.fromList
|
|
|
|
|> Dict.get
|
|
|
|
(pageRoute
|
|
|
|
|> String.split "/"
|
|
|
|
|> List.filter (\pathPart -> pathPart /= "")
|
|
|
|
)
|
|
|
|
in
|
|
|
|
case thing of
|
|
|
|
Just request ->
|
2021-04-20 17:31:19 +03:00
|
|
|
request |> DataSource.map (\_ -> ())
|
2021-04-17 17:42:33 +03:00
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Debug.todo <| "Couldn't find page: " ++ pageRoute ++ "\npages: " ++ Debug.toString pages
|
2021-09-27 20:48:49 +03:00
|
|
|
, site = Just site
|
2019-10-22 04:49:46 +03:00
|
|
|
, view =
|
2021-05-24 21:40:36 +03:00
|
|
|
\page _ ->
|
2019-10-22 16:46:31 +03:00
|
|
|
let
|
2021-06-04 03:57:57 +03:00
|
|
|
thing : Maybe (DataSource a)
|
2019-10-24 15:52:24 +03:00
|
|
|
thing =
|
2019-10-22 16:46:31 +03:00
|
|
|
pages
|
|
|
|
|> Dict.fromList
|
|
|
|
|> Dict.get
|
2021-05-23 20:44:39 +03:00
|
|
|
(page.path |> Path.toSegments)
|
2019-10-22 16:46:31 +03:00
|
|
|
in
|
2019-10-24 15:52:24 +03:00
|
|
|
case thing of
|
2021-04-17 18:18:26 +03:00
|
|
|
Just _ ->
|
2021-04-17 17:42:33 +03:00
|
|
|
\_ _ -> { view = \_ -> { title = "Title", body = Html.text "" }, head = [] }
|
2019-10-24 15:52:24 +03:00
|
|
|
|
|
|
|
Nothing ->
|
2021-04-03 16:39:07 +03:00
|
|
|
Debug.todo <| "Couldn't find page: " ++ Debug.toString page ++ "\npages: " ++ Debug.toString pages
|
2020-10-26 21:50:06 +03:00
|
|
|
, subscriptions = \_ _ _ -> Sub.none
|
2021-04-16 18:04:15 +03:00
|
|
|
, routeToPath = \(Route route) -> route |> String.split "/"
|
2021-04-24 02:22:23 +03:00
|
|
|
, sharedData = DataSource.succeed ()
|
2021-04-21 17:52:56 +03:00
|
|
|
, onPageChange = \_ -> Continue
|
2021-05-15 05:38:10 +03:00
|
|
|
, apiRoutes = \_ -> apiRoutes
|
2021-06-06 21:11:34 +03:00
|
|
|
, pathPatterns = []
|
2019-10-22 04:49:46 +03:00
|
|
|
}
|
2020-04-19 18:17:51 +03:00
|
|
|
|
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" )
|
|
|
|
, ( "staticHttpCache", encodedStaticHttpCache )
|
|
|
|
]
|
|
|
|
|
2021-06-04 03:57:57 +03:00
|
|
|
encodedStaticHttpCache : Encode.Value
|
2020-04-19 18:17:51 +03:00
|
|
|
encodedStaticHttpCache =
|
|
|
|
staticHttpCache
|
|
|
|
|> List.map
|
|
|
|
(\( request, httpResponseString ) ->
|
|
|
|
( Request.hash request, Encode.string httpResponseString )
|
|
|
|
)
|
|
|
|
|> Encode.object
|
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 [])
|
|
|
|
)
|
|
|
|
contentCache
|
|
|
|
config
|
2021-09-27 20:48:49 +03:00
|
|
|
, update = update site contentCache config
|
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
|
|
|
|
|
|
|
|
2021-09-27 20:48:49 +03:00
|
|
|
site =
|
|
|
|
{ data = DataSource.succeed ()
|
|
|
|
, canonicalUrl = "canonical-site-url"
|
|
|
|
, manifest = \_ -> manifest
|
|
|
|
, head = \_ -> []
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-29 20:03:42 +03:00
|
|
|
startSimple : List String -> DataSource a -> ProgramTest (Model Route) Msg Effect
|
2021-07-29 19:17:36 +03:00
|
|
|
startSimple route dataSources =
|
|
|
|
startWithRoutes route [ route ] [] [ ( route, dataSources ) ]
|
|
|
|
|
|
|
|
|
2021-07-29 20:03:42 +03:00
|
|
|
startSimpleWithCache : List String -> DataSource a -> List ( Request.Request, String ) -> ProgramTest (Model Route) Msg Effect
|
2021-07-29 19:38:46 +03:00
|
|
|
startSimpleWithCache route dataSources cache =
|
|
|
|
startWithRoutes route [ route ] cache [ ( route, dataSources ) ]
|
|
|
|
|
|
|
|
|
2021-06-08 19:54:10 +03:00
|
|
|
startWithRoutes :
|
|
|
|
List String
|
|
|
|
-> List (List String)
|
|
|
|
-> List ( Request.Request, String )
|
|
|
|
-> List ( List String, DataSource a )
|
|
|
|
-> ProgramTest (Model Route) Msg Effect
|
|
|
|
startWithRoutes pageToLoad staticRoutes staticHttpCache pages =
|
|
|
|
let
|
|
|
|
contentCache : ContentCache
|
|
|
|
contentCache =
|
|
|
|
ContentCache.init Nothing
|
|
|
|
|
|
|
|
config : ProgramConfig Msg () Route () () ()
|
|
|
|
config =
|
|
|
|
{ toJsPort = toJsPort
|
|
|
|
, fromJsPort = fromJsPort
|
|
|
|
, init = \_ _ _ _ _ -> ( (), Cmd.none )
|
|
|
|
, getStaticRoutes =
|
|
|
|
staticRoutes
|
|
|
|
|> List.map (String.join "/")
|
|
|
|
|> List.map Route
|
|
|
|
|> DataSource.succeed
|
|
|
|
, handleRoute =
|
|
|
|
\(Route route) ->
|
|
|
|
staticRoutes
|
|
|
|
|> List.map (String.join "/")
|
2021-06-08 20:36:05 +03:00
|
|
|
|> List.member route
|
2021-06-09 01:17:56 +03:00
|
|
|
|> (\found ->
|
|
|
|
if found then
|
|
|
|
Nothing
|
|
|
|
|
|
|
|
else
|
2021-07-31 01:23:36 +03:00
|
|
|
Just Pages.Internal.NotFoundReason.NoMatchingRoute
|
2021-06-09 01:17:56 +03:00
|
|
|
)
|
2021-06-08 19:54:10 +03:00
|
|
|
|> DataSource.succeed
|
|
|
|
, urlToRoute = .path >> Route
|
|
|
|
, update = \_ _ _ _ _ -> ( (), Cmd.none )
|
2021-07-27 20:41:18 +03:00
|
|
|
, basePath = []
|
2021-06-08 19:54:10 +03:00
|
|
|
, data =
|
|
|
|
\(Route pageRoute) ->
|
|
|
|
let
|
|
|
|
thing : Maybe (DataSource a)
|
|
|
|
thing =
|
|
|
|
pages
|
|
|
|
|> Dict.fromList
|
|
|
|
|> Dict.get
|
|
|
|
(pageRoute
|
|
|
|
|> String.split "/"
|
|
|
|
|> List.filter (\pathPart -> pathPart /= "")
|
|
|
|
)
|
|
|
|
in
|
|
|
|
case thing of
|
|
|
|
Just request ->
|
|
|
|
request |> DataSource.map (\_ -> ())
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
DataSource.fail <| "Couldn't find page: " ++ pageRoute ++ "\npages: " ++ Debug.toString pages
|
2021-09-27 20:48:49 +03:00
|
|
|
, site = Just site
|
2021-06-08 19:54:10 +03:00
|
|
|
, view =
|
|
|
|
\page _ ->
|
|
|
|
let
|
|
|
|
thing : Maybe (DataSource a)
|
|
|
|
thing =
|
|
|
|
pages
|
|
|
|
|> Dict.fromList
|
|
|
|
|> Dict.get
|
|
|
|
(page.path |> Path.toSegments)
|
|
|
|
in
|
|
|
|
case thing of
|
|
|
|
Just _ ->
|
|
|
|
\_ _ -> { view = \_ -> { title = "Title", body = Html.text "" }, head = [] }
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Debug.todo <| "Couldn't find page: " ++ Debug.toString page ++ "\npages: " ++ Debug.toString pages
|
|
|
|
, subscriptions = \_ _ _ -> Sub.none
|
|
|
|
, routeToPath = \(Route route) -> route |> String.split "/"
|
|
|
|
, sharedData = DataSource.succeed ()
|
|
|
|
, onPageChange = \_ -> Continue
|
|
|
|
, apiRoutes = \_ -> []
|
|
|
|
, pathPatterns = []
|
|
|
|
}
|
|
|
|
|
|
|
|
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" )
|
2021-06-08 19:54:10 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
encodedStaticHttpCache : Encode.Value
|
|
|
|
encodedStaticHttpCache =
|
|
|
|
staticHttpCache
|
|
|
|
|> List.map
|
|
|
|
(\( request, httpResponseString ) ->
|
|
|
|
( Request.hash request, Encode.string httpResponseString )
|
|
|
|
)
|
|
|
|
|> Encode.object
|
|
|
|
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 [])
|
|
|
|
)
|
|
|
|
contentCache
|
|
|
|
config
|
2021-09-27 20:48:49 +03:00
|
|
|
, update = update site contentCache config
|
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 =
|
|
|
|
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
|
|
|
|
|
2021-07-29 19:17:36 +03:00
|
|
|
Effect.FetchHttp { unmasked, masked } ->
|
2021-06-23 18:22:07 +03:00
|
|
|
if unmasked.url |> String.startsWith "file://" then
|
|
|
|
let
|
|
|
|
filePath : String
|
|
|
|
filePath =
|
|
|
|
String.dropLeft 7 unmasked.url
|
|
|
|
in
|
|
|
|
ToJsPayload.ReadFile filePath
|
|
|
|
|> sendToJsPort
|
|
|
|
|> SimulatedEffect.Cmd.map never
|
|
|
|
|
|
|
|
else if unmasked.url |> String.startsWith "glob://" then
|
|
|
|
let
|
|
|
|
globPattern : String
|
|
|
|
globPattern =
|
|
|
|
String.dropLeft 7 unmasked.url
|
|
|
|
in
|
|
|
|
ToJsPayload.Glob globPattern
|
|
|
|
|> sendToJsPort
|
|
|
|
|> SimulatedEffect.Cmd.map never
|
|
|
|
|
|
|
|
else if unmasked.url |> String.startsWith "port://" then
|
|
|
|
let
|
|
|
|
portName : String
|
|
|
|
portName =
|
|
|
|
String.dropLeft 7 unmasked.url
|
|
|
|
in
|
|
|
|
ToJsPayload.Port portName
|
|
|
|
|> sendToJsPort
|
|
|
|
|> SimulatedEffect.Cmd.map never
|
|
|
|
|
|
|
|
else
|
2021-07-29 19:17:36 +03:00
|
|
|
ToJsPayload.DoHttp { masked = masked, unmasked = unmasked }
|
|
|
|
|> sendToJsPort
|
|
|
|
|> SimulatedEffect.Cmd.map never
|
2019-10-22 05:31:17 +03:00
|
|
|
|
2021-06-08 19:54:10 +03:00
|
|
|
Effect.SendSinglePage done 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"
|
2021-06-08 19:54:10 +03:00
|
|
|
, if done then
|
|
|
|
SimulatedEffect.Cmd.none
|
|
|
|
|
|
|
|
else
|
|
|
|
SimulatedEffect.Task.succeed ()
|
|
|
|
|> SimulatedEffect.Task.perform (\_ -> Continue)
|
2020-10-14 07:02:12 +03:00
|
|
|
]
|
2020-10-10 03:08:02 +03:00
|
|
|
|
2020-10-14 01:40:36 +03:00
|
|
|
Effect.Continue ->
|
2020-10-14 07:02:12 +03:00
|
|
|
SimulatedEffect.Cmd.none
|
2020-10-14 01:40:36 +03:00
|
|
|
|
2021-04-03 00:44:40 +03:00
|
|
|
Effect.ReadFile _ ->
|
2021-04-01 05:55:28 +03:00
|
|
|
SimulatedEffect.Cmd.none
|
|
|
|
|
2021-04-03 00:44:40 +03:00
|
|
|
Effect.GetGlob _ ->
|
2021-04-01 05:55:28 +03:00
|
|
|
SimulatedEffect.Cmd.none
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2021-04-16 18:04:15 +03:00
|
|
|
manifest : Manifest.Config
|
2019-10-22 04:49:46 +03:00
|
|
|
manifest =
|
2021-04-23 18:47:11 +03:00
|
|
|
Manifest.init
|
|
|
|
{ description = "elm-pages - A statically typed site generator."
|
|
|
|
, name = "elm-pages docs"
|
2021-05-23 20:53:43 +03:00
|
|
|
, startUrl = Path.join []
|
2021-04-23 18:47:11 +03:00
|
|
|
, icons = []
|
|
|
|
}
|
2019-11-13 05:22:36 +03:00
|
|
|
|
|
|
|
|
2020-05-12 02:42:54 +03:00
|
|
|
starDecoder : Decoder Int
|
2019-11-13 05:22:36 +03:00
|
|
|
starDecoder =
|
|
|
|
Decode.field "stargazer_count" Decode.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 _ =
|
|
|
|
SimulatedEffect.Ports.subscribe "fromJsPort"
|
|
|
|
(JD.field "tag" JD.string
|
|
|
|
|> JD.andThen
|
|
|
|
(\tag ->
|
|
|
|
case tag of
|
|
|
|
"GotGlob" ->
|
|
|
|
JD.field "data"
|
|
|
|
(JD.map2 Tuple.pair
|
|
|
|
(JD.field "pattern" JD.string)
|
|
|
|
(JD.field "result" JD.value)
|
|
|
|
)
|
2021-07-27 20:41:18 +03:00
|
|
|
|> JD.map
|
|
|
|
(\( globPattern, response ) ->
|
|
|
|
GotDataBatch
|
|
|
|
[ { request =
|
|
|
|
{ masked =
|
|
|
|
{ url = "glob://" ++ globPattern
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
|
|
|
, body = StaticHttpBody.EmptyBody
|
|
|
|
}
|
|
|
|
, unmasked =
|
|
|
|
{ url = "glob://" ++ globPattern
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
|
|
|
, body = StaticHttpBody.EmptyBody
|
|
|
|
}
|
|
|
|
}
|
|
|
|
, response = Encode.encode 0 response
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2021-06-23 18:22:07 +03:00
|
|
|
|
|
|
|
"GotFile" ->
|
|
|
|
JD.field "data"
|
|
|
|
(JD.map2 Tuple.pair
|
|
|
|
(JD.field "filePath" JD.string)
|
|
|
|
JD.value
|
|
|
|
)
|
2021-07-27 20:41:18 +03:00
|
|
|
|> JD.map
|
|
|
|
(\( filePath, response ) ->
|
|
|
|
GotDataBatch
|
|
|
|
[ { request =
|
|
|
|
{ masked =
|
|
|
|
{ url = "file://" ++ filePath
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
|
|
|
, body = StaticHttpBody.EmptyBody
|
|
|
|
}
|
|
|
|
, unmasked =
|
|
|
|
{ url = "file://" ++ filePath
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
|
|
|
, body = StaticHttpBody.EmptyBody
|
|
|
|
}
|
|
|
|
}
|
|
|
|
, response = Encode.encode 0 response
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2021-06-23 18:22:07 +03:00
|
|
|
|
2021-07-29 19:17:36 +03:00
|
|
|
"GotBatch" ->
|
|
|
|
JD.field "data"
|
|
|
|
(JD.list
|
|
|
|
(JD.map2
|
|
|
|
(\requests response ->
|
|
|
|
{ request =
|
|
|
|
{ masked = requests.masked
|
|
|
|
, unmasked = requests.unmasked
|
|
|
|
}
|
|
|
|
, response = response
|
|
|
|
}
|
|
|
|
)
|
|
|
|
(JD.field "request" requestDecoder)
|
|
|
|
(JD.field "response" JD.string)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> JD.map GotDataBatch
|
|
|
|
|
2021-06-23 18:22:07 +03:00
|
|
|
_ ->
|
|
|
|
JD.fail "Unexpected subscription tag."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
identity
|
|
|
|
|
|
|
|
|
2020-01-03 23:18:18 +03:00
|
|
|
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
|
|
|
}
|
2021-07-29 19:17:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
post : String -> Request.Request
|
|
|
|
post url =
|
|
|
|
{ method = "POST"
|
|
|
|
, url = url
|
|
|
|
, headers = []
|
2021-07-30 01:57:44 +03:00
|
|
|
, body = DataSource.Http.emptyBody
|
2021-07-29 19:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-29 20:03:42 +03:00
|
|
|
toRequest : Secrets.Value Request.Request -> { masked : Request.Request, unmasked : Request.Request }
|
2021-07-29 19:17:36 +03:00
|
|
|
toRequest secretsValue =
|
|
|
|
{ masked = Secrets.maskedLookup secretsValue
|
|
|
|
, unmasked = Secrets.maskedLookup secretsValue
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
simulateHttp : Secrets.Value Request.Request -> String -> ProgramTest model msg effect -> ProgramTest model msg effect
|
|
|
|
simulateHttp request response program =
|
|
|
|
program
|
|
|
|
|> ProgramTest.ensureOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
|
|
|
[ ToJsPayload.DoHttp _ ] ->
|
|
|
|
Expect.pass
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail <|
|
|
|
|
"Expected an HTTP request, got:\n"
|
|
|
|
++ Debug.toString actualPorts
|
|
|
|
)
|
|
|
|
|> ProgramTest.simulateIncomingPort "fromJsPort"
|
|
|
|
(Encode.object
|
|
|
|
[ ( "tag", Encode.string "GotBatch" )
|
|
|
|
, ( "data"
|
|
|
|
, Encode.list
|
|
|
|
(\req ->
|
|
|
|
Encode.object
|
|
|
|
[ ( "request"
|
|
|
|
, Encode.object
|
|
|
|
[ ( "masked"
|
|
|
|
, Codec.encodeToValue Request.codec
|
|
|
|
(toRequest req
|
|
|
|
|> .masked
|
|
|
|
)
|
|
|
|
)
|
|
|
|
, ( "unmasked"
|
|
|
|
, Codec.encodeToValue Request.codec
|
|
|
|
(toRequest req
|
|
|
|
|> .unmasked
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
, ( "response", Encode.string response )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
[ request ]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
simulateMultipleHttp : List ( Secrets.Value Request.Request, String ) -> ProgramTest model msg effect -> ProgramTest model msg effect
|
|
|
|
simulateMultipleHttp requests program =
|
|
|
|
program
|
|
|
|
|> ProgramTest.ensureOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
2021-07-29 20:03:42 +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
|
|
|
|
)
|
|
|
|
|> ProgramTest.simulateIncomingPort "fromJsPort"
|
|
|
|
(Encode.object
|
|
|
|
[ ( "tag", Encode.string "GotBatch" )
|
|
|
|
, ( "data"
|
|
|
|
, Encode.list
|
|
|
|
(\( req, response ) ->
|
|
|
|
Encode.object
|
|
|
|
[ ( "request"
|
|
|
|
, Encode.object
|
|
|
|
[ ( "masked"
|
|
|
|
, Codec.encodeToValue Request.codec
|
|
|
|
(toRequest req
|
|
|
|
|> .masked
|
|
|
|
)
|
|
|
|
)
|
|
|
|
, ( "unmasked"
|
|
|
|
, Codec.encodeToValue Request.codec
|
|
|
|
(toRequest req
|
|
|
|
|> .unmasked
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
, ( "response", Encode.string response )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
requests
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2021-07-29 19:32:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
simulateHttpAssert : Secrets.Value Request.Request -> String -> ({ masked : Request.Request, unmasked : Request.Request } -> Expect.Expectation) -> ProgramTest model msg effect -> ProgramTest model msg effect
|
|
|
|
simulateHttpAssert request response checkRequest program =
|
|
|
|
program
|
|
|
|
|> ProgramTest.ensureOutgoingPortValues
|
|
|
|
"toJsPort"
|
|
|
|
(Codec.decoder (ToJsPayload.successCodecNew2 "" ""))
|
|
|
|
(\actualPorts ->
|
|
|
|
case actualPorts of
|
|
|
|
[ ToJsPayload.DoHttp actualReq ] ->
|
|
|
|
--Expect.pass
|
|
|
|
checkRequest actualReq
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail <|
|
|
|
|
"Expected an HTTP request, got:\n"
|
|
|
|
++ Debug.toString actualPorts
|
|
|
|
)
|
|
|
|
|> ProgramTest.simulateIncomingPort "fromJsPort"
|
|
|
|
(Encode.object
|
|
|
|
[ ( "tag", Encode.string "GotBatch" )
|
|
|
|
, ( "data"
|
|
|
|
, Encode.list
|
|
|
|
(\req ->
|
|
|
|
Encode.object
|
|
|
|
[ ( "request"
|
|
|
|
, Encode.object
|
|
|
|
[ ( "masked"
|
|
|
|
, Codec.encodeToValue Request.codec
|
|
|
|
(toRequest req
|
|
|
|
|> .masked
|
|
|
|
)
|
|
|
|
)
|
|
|
|
, ( "unmasked"
|
|
|
|
, Codec.encodeToValue Request.codec
|
|
|
|
(toRequest req
|
|
|
|
|> .unmasked
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
, ( "response", Encode.string response )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
[ request ]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|