2019-10-22 05:11:50 +03:00
|
|
|
module StaticHttpRequestsTests exposing (all)
|
2019-10-22 04:49:46 +03:00
|
|
|
|
2019-10-22 08:00:15 +03:00
|
|
|
import Codec
|
2019-10-22 16:46:31 +03:00
|
|
|
import Dict exposing (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)
|
2019-10-22 04:49:46 +03:00
|
|
|
import Pages.ContentCache as ContentCache
|
|
|
|
import Pages.Document as Document
|
|
|
|
import Pages.ImagePath as ImagePath
|
2019-10-22 05:31:17 +03:00
|
|
|
import Pages.Internal.Platform.Cli as Main exposing (..)
|
2020-06-12 06:57:19 +03:00
|
|
|
import Pages.Internal.Platform.Effect as Effect exposing (Effect)
|
2020-06-12 06:55:19 +03:00
|
|
|
import Pages.Internal.Platform.ToJsPayload as ToJsPayload exposing (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
|
|
|
|
import Pages.PagePath as PagePath
|
2019-12-11 17:13:02 +03:00
|
|
|
import Pages.StaticHttp as StaticHttp
|
2020-01-03 23:18:18 +03:00
|
|
|
import Pages.StaticHttp.Request as Request
|
2020-03-04 19:17:06 +03:00
|
|
|
import PagesHttp
|
2019-10-22 04:49:46 +03:00
|
|
|
import ProgramTest exposing (ProgramTest)
|
2019-11-03 00:46:52 +03:00
|
|
|
import Regex
|
2019-11-12 04:48:08 +03:00
|
|
|
import Secrets
|
2019-10-22 05:31:17 +03:00
|
|
|
import SimulatedEffect.Cmd
|
2019-11-08 22:34:51 +03:00
|
|
|
import SimulatedEffect.Http as Http
|
2019-10-22 06:21:52 +03:00
|
|
|
import SimulatedEffect.Ports
|
2020-10-14 01:40:36 +03:00
|
|
|
import SimulatedEffect.Task
|
2020-06-16 05:42:04 +03:00
|
|
|
import Test exposing (Test, describe, only, skip, test)
|
2019-11-02 21:13:33 +03:00
|
|
|
import Test.Http
|
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
|
|
|
\() ->
|
2019-10-24 15:52:24 +03:00
|
|
|
start
|
|
|
|
[ ( []
|
2019-11-13 05:22:36 +03:00
|
|
|
, StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
2019-10-24 15:52:24 +03:00
|
|
|
)
|
|
|
|
]
|
2019-10-22 05:56:48 +03:00
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"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
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( ""
|
2020-01-04 00:22:53 +03:00
|
|
|
, [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
2020-01-03 23:18:18 +03:00
|
|
|
, """{"stargazer_count":86}"""
|
|
|
|
)
|
2019-10-22 08:00:15 +03:00
|
|
|
]
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2019-11-01 23:29:09 +03:00
|
|
|
, test "andThen" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( [ "elm-pages" ]
|
2019-11-12 23:48:01 +03:00
|
|
|
, StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.succeed ())
|
2019-11-01 23:29:09 +03:00
|
|
|
|> StaticHttp.andThen
|
|
|
|
(\continueUrl ->
|
2019-11-12 23:48:01 +03:00
|
|
|
StaticHttp.get (Secrets.succeed "NEXT-REQUEST") (Decode.succeed ())
|
2019-11-01 23:29:09 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
"""null"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"NEXT-REQUEST"
|
|
|
|
"""null"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( "elm-pages"
|
2020-01-04 00:22:53 +03:00
|
|
|
, [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
2020-01-03 23:18:18 +03:00
|
|
|
, """null"""
|
|
|
|
)
|
2020-01-04 00:22:53 +03:00
|
|
|
, ( get "NEXT-REQUEST"
|
2020-01-03 23:18:18 +03:00
|
|
|
, """null"""
|
|
|
|
)
|
2019-11-12 20:59:37 +03:00
|
|
|
]
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2019-11-12 20:59:37 +03:00
|
|
|
, test "andThen chain avoids repeat requests" <|
|
|
|
|
\() ->
|
|
|
|
let
|
2020-01-03 23:18:18 +03:00
|
|
|
getReq url decoder =
|
2019-11-12 20:59:37 +03:00
|
|
|
StaticHttp.request
|
2020-01-04 00:22:53 +03:00
|
|
|
(Secrets.succeed (get url))
|
2019-11-12 20:59:37 +03:00
|
|
|
decoder
|
|
|
|
|
|
|
|
pokemonDetailRequest : StaticHttp.Request ()
|
|
|
|
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
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> StaticHttp.resolve
|
|
|
|
|> StaticHttp.map (\_ -> ())
|
|
|
|
in
|
|
|
|
start
|
|
|
|
[ ( [ "elm-pages" ], pokemonDetailRequest )
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"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"}
|
|
|
|
]"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"url1"
|
|
|
|
"""{"image": "image1.jpg"}"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"url2"
|
|
|
|
"""{"image": "image2.jpg"}"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"url3"
|
|
|
|
"""{"image": "image3.jpg"}"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"url4"
|
|
|
|
"""{"image": "image4.jpg"}"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"url5"
|
|
|
|
"""{"image": "image5.jpg"}"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"url6"
|
|
|
|
"""{"image": "image6.jpg"}"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"url7"
|
|
|
|
"""{"image": "image7.jpg"}"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"url8"
|
|
|
|
"""{"image": "image8.jpg"}"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"url9"
|
|
|
|
"""{"image": "image9.jpg"}"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"url10"
|
|
|
|
"""{"image": "image10.jpg"}"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( "elm-pages"
|
2020-01-03 23:18:18 +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"}]"""
|
|
|
|
)
|
|
|
|
, ( get "url1"
|
|
|
|
, """{"image":"image1.jpg"}"""
|
|
|
|
)
|
|
|
|
, ( 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"}"""
|
|
|
|
)
|
2019-11-01 23:29:09 +03:00
|
|
|
]
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2019-10-22 18:42:17 +03:00
|
|
|
, test "port is sent out once all requests are finished" <|
|
|
|
|
\() ->
|
|
|
|
start
|
2019-10-24 15:52:24 +03:00
|
|
|
[ ( [ "elm-pages" ]
|
2019-11-13 05:22:36 +03:00
|
|
|
, StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
2019-10-24 15:52:24 +03:00
|
|
|
)
|
|
|
|
, ( [ "elm-pages-starter" ]
|
2019-11-13 05:22:36 +03:00
|
|
|
, StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages-starter") starDecoder
|
2019-10-24 15:52:24 +03:00
|
|
|
)
|
2019-10-22 18:42:17 +03:00
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
2019-10-22 22:43:26 +03:00
|
|
|
"""{ "stargazer_count": 86 }"""
|
2019-10-22 18:42:17 +03:00
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages-starter"
|
2019-10-22 22:43:26 +03:00
|
|
|
"""{ "stargazer_count": 22 }"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( "elm-pages"
|
2020-01-03 23:18:18 +03:00
|
|
|
, [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":86}"""
|
|
|
|
)
|
2019-10-22 18:42:17 +03:00
|
|
|
]
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
2020-03-11 19:03:47 +03:00
|
|
|
, ( "elm-pages-starter"
|
2020-01-03 23:18:18 +03:00
|
|
|
, [ ( 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" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
2019-11-13 05:22:36 +03:00
|
|
|
, StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.field "stargazer_count" Decode.int)
|
2019-11-05 00:05:12 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
"""{ "stargazer_count": 86, "unused_field": 123 }"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( ""
|
2020-01-03 23:18:18 +03:00
|
|
|
, [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":86}"""
|
|
|
|
)
|
2019-11-05 00:05:12 +03:00
|
|
|
]
|
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" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
2020-03-03 18:54:16 +03:00
|
|
|
, StaticHttp.unoptimizedRequest
|
2020-03-02 06:09:39 +03:00
|
|
|
(Secrets.succeed
|
|
|
|
{ url = "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
|
|
|
, body = StaticHttp.emptyBody
|
|
|
|
}
|
|
|
|
)
|
2020-03-03 18:54:16 +03:00
|
|
|
(StaticHttp.expectUnoptimizedJson
|
2020-03-03 09:08:00 +03:00
|
|
|
(JD.field "stargazer_count" JD.int)
|
|
|
|
)
|
2020-03-02 06:09:39 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
"""{ "stargazer_count": 86, "unused_field": 123 }"""
|
|
|
|
|> expectSuccess
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( ""
|
2020-03-02 06:09:39 +03:00
|
|
|
, [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{ "stargazer_count": 86, "unused_field": 123 }"""
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2020-03-03 18:54:16 +03:00
|
|
|
, test "plain string" <|
|
2020-03-03 09:08:00 +03:00
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
2020-03-03 18:54:16 +03:00
|
|
|
, StaticHttp.unoptimizedRequest
|
2020-03-03 09:08:00 +03:00
|
|
|
(Secrets.succeed
|
|
|
|
{ url = "https://example.com/file.txt"
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
|
|
|
, body = StaticHttp.emptyBody
|
|
|
|
}
|
|
|
|
)
|
2020-03-03 18:54:16 +03:00
|
|
|
(StaticHttp.expectString Ok)
|
2020-03-03 09:08:00 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://example.com/file.txt"
|
|
|
|
"This is a raw text file."
|
|
|
|
|> expectSuccess
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( ""
|
2020-03-03 09:08:00 +03:00
|
|
|
, [ ( get "https://example.com/file.txt"
|
|
|
|
, "This is a raw text file."
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2020-03-03 18:54:16 +03:00
|
|
|
, test "Err in String to Result function turns into decode error" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
|
|
|
, StaticHttp.unoptimizedRequest
|
|
|
|
(Secrets.succeed
|
|
|
|
{ url = "https://example.com/file.txt"
|
|
|
|
, method = "GET"
|
|
|
|
, headers = []
|
|
|
|
, body = StaticHttp.emptyBody
|
|
|
|
}
|
|
|
|
)
|
|
|
|
(StaticHttp.expectString
|
|
|
|
(\string ->
|
|
|
|
if String.toUpper string == string then
|
|
|
|
Ok string
|
|
|
|
|
|
|
|
else
|
|
|
|
Err "String was not uppercased"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://example.com/file.txt"
|
|
|
|
"This is a raw text file."
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
2020-10-21 06:48:21 +03:00
|
|
|
(Codec.decoder (ToJsPayload.toJsCodec canonicalSiteUrl))
|
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" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
2019-11-13 05:22:36 +03:00
|
|
|
, StaticHttp.request
|
|
|
|
(Secrets.succeed
|
|
|
|
{ method = "POST"
|
|
|
|
, url = "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, headers = []
|
2020-01-04 00:57:45 +03:00
|
|
|
, body = StaticHttp.emptyBody
|
2019-11-13 05:22:36 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
(Decode.field "stargazer_count" Decode.int)
|
2019-11-08 22:34:51 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"POST"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
"""{ "stargazer_count": 86, "unused_field": 123 }"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( ""
|
2020-01-03 23:18:18 +03:00
|
|
|
, [ ( { method = "POST"
|
|
|
|
, url = "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, headers = []
|
2020-01-04 00:57:45 +03:00
|
|
|
, body = StaticHttp.emptyBody
|
2019-11-08 22:34:51 +03:00
|
|
|
}
|
2020-01-03 23:18:18 +03:00
|
|
|
, """{"stargazer_count":86}"""
|
|
|
|
)
|
2019-11-08 22:34:51 +03:00
|
|
|
]
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2019-11-05 00:24:08 +03:00
|
|
|
, test "json is reduced from andThen chains" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
2019-11-13 05:22:36 +03:00
|
|
|
, StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.field "stargazer_count" Decode.int)
|
2019-11-05 00:24:08 +03:00
|
|
|
|> StaticHttp.andThen
|
|
|
|
(\continueUrl ->
|
2019-11-13 05:22:36 +03:00
|
|
|
StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages-starter") (Decode.field "stargazer_count" Decode.int)
|
2019-11-05 00:24:08 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
2019-11-05 00:48:30 +03:00
|
|
|
"""{ "stargazer_count": 100, "unused_field": 123 }"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"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
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( ""
|
2020-01-03 23:18:18 +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}"""
|
|
|
|
)
|
2019-11-05 00:48:30 +03:00
|
|
|
]
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
|
|
|
]
|
2019-11-05 00:48:30 +03:00
|
|
|
, test "reduced json is preserved by StaticHttp.map2" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
|
|
|
, StaticHttp.map2 (\_ _ -> ())
|
2019-11-13 05:22:36 +03:00
|
|
|
(StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.field "stargazer_count" Decode.int))
|
|
|
|
(StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages-starter") (Decode.field "stargazer_count" Decode.int))
|
2019-11-05 00:48:30 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
2019-11-05 00:24:08 +03:00
|
|
|
"""{ "stargazer_count": 100, "unused_field": 123 }"""
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"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
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( ""
|
2020-01-03 23:18:18 +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}"""
|
|
|
|
)
|
2019-11-05 00:24:08 +03:00
|
|
|
]
|
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
|
|
|
|
[ ( []
|
|
|
|
, StaticHttp.succeed ()
|
|
|
|
)
|
|
|
|
]
|
2020-03-11 19:03:47 +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" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
|
|
|
, StaticHttp.map2 (\_ _ -> ())
|
2019-11-12 23:48:01 +03:00
|
|
|
(StaticHttp.get (Secrets.succeed "http://example.com") (Decode.succeed ()))
|
|
|
|
(StaticHttp.get (Secrets.succeed "http://example.com") (Decode.succeed ()))
|
2019-10-30 07:45:23 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"http://example.com"
|
|
|
|
"""null"""
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( ""
|
2020-01-03 23:18:18 +03:00
|
|
|
, [ ( get "http://example.com"
|
|
|
|
, """null"""
|
|
|
|
)
|
2019-10-28 18:04:34 +03:00
|
|
|
]
|
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" <|
|
|
|
|
\() ->
|
|
|
|
start
|
2019-10-28 17:57:21 +03:00
|
|
|
[ ( [ "elm-pages" ]
|
2019-11-12 23:48:01 +03:00
|
|
|
, StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.fail "The user should get this message from the CLI.")
|
2019-10-28 17:57:21 +03:00
|
|
|
)
|
2019-10-24 15:52:24 +03:00
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
"""{ "stargazer_count": 86 }"""
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
2020-10-21 06:48:21 +03:00
|
|
|
(Codec.decoder (ToJsPayload.toJsCodec canonicalSiteUrl))
|
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
|
|
|
|
2020-03-11 19:03:47 +03:00
|
|
|
elm-pages
|
2019-10-27 02:50:09 +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" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( [ "elm-pages" ]
|
2019-11-12 23:48:01 +03:00
|
|
|
, StaticHttp.get
|
2019-11-12 04:48:08 +03:00
|
|
|
(Secrets.succeed
|
2019-11-11 23:40:38 +03:00
|
|
|
(\apiKey ->
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages?apiKey=" ++ apiKey
|
|
|
|
)
|
2019-11-12 04:48:08 +03:00
|
|
|
|> Secrets.with "API_KEY"
|
2019-11-03 00:17:32 +03:00
|
|
|
)
|
|
|
|
Decode.string
|
|
|
|
|> StaticHttp.andThen
|
|
|
|
(\url ->
|
2019-11-12 23:48:01 +03:00
|
|
|
StaticHttp.get
|
2019-11-12 04:48:08 +03:00
|
|
|
(Secrets.succeed
|
2019-11-11 23:40:38 +03:00
|
|
|
(\missingSecret ->
|
|
|
|
url ++ "?apiKey=" ++ missingSecret
|
|
|
|
)
|
2019-11-12 04:48:08 +03:00
|
|
|
|> Secrets.with "MISSING"
|
2019-11-03 00:17:32 +03:00
|
|
|
)
|
|
|
|
(Decode.succeed ())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages?apiKey=ABCD1234"
|
|
|
|
""" "continuation-url" """
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
2020-10-21 06:48:21 +03:00
|
|
|
(Codec.decoder (ToJsPayload.toJsCodec canonicalSiteUrl))
|
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-11-02 21:13:33 +03:00
|
|
|
, test "an error is sent for HTTP errors" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
2019-11-12 23:48:01 +03:00
|
|
|
, StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.succeed ())
|
2019-11-02 21:13:33 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpResponse
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
(Test.Http.httpResponse
|
|
|
|
{ statusCode = 404
|
|
|
|
, headers = []
|
|
|
|
, body = ""
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
2020-10-21 06:48:21 +03:00
|
|
|
(Codec.decoder (ToJsPayload.toJsCodec canonicalSiteUrl))
|
2019-12-07 17:46:45 +03:00
|
|
|
(expectErrorsPort """-- STATIC HTTP ERROR ----------------------------------------------------- elm-pages
|
|
|
|
|
|
|
|
I got an error making an HTTP request to this URL: https://api.github.com/repos/dillonkearns/elm-pages
|
|
|
|
|
2020-03-04 19:17:06 +03:00
|
|
|
Bad status: 404
|
|
|
|
Status message: TODO: if you need this, please report to https://github.com/avh4/elm-program-test/issues
|
2020-10-07 22:27:33 +03:00
|
|
|
Body:
|
|
|
|
|
|
|
|
-- STATIC HTTP DECODING ERROR ----------------------------------------------------- elm-pages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Payload sent back invalid JSON""")
|
2019-10-24 18:26:41 +03:00
|
|
|
, test "uses real secrets to perform request and masked secrets to store and lookup response" <|
|
|
|
|
\() ->
|
|
|
|
start
|
|
|
|
[ ( []
|
2019-11-12 03:56:25 +03:00
|
|
|
, StaticHttp.request
|
2019-11-12 04:48:08 +03:00
|
|
|
(Secrets.succeed
|
2019-11-12 03:56:25 +03:00
|
|
|
(\apiKey bearer ->
|
|
|
|
{ url = "https://api.github.com/repos/dillonkearns/elm-pages?apiKey=" ++ apiKey
|
|
|
|
, method = "GET"
|
|
|
|
, headers = [ ( "Authorization", "Bearer " ++ bearer ) ]
|
2020-01-04 00:57:45 +03:00
|
|
|
, body = StaticHttp.emptyBody
|
2019-11-12 03:56:25 +03:00
|
|
|
}
|
2019-11-11 23:40:38 +03:00
|
|
|
)
|
2019-11-12 04:48:08 +03:00
|
|
|
|> Secrets.with "API_KEY"
|
|
|
|
|> Secrets.with "BEARER"
|
2019-10-28 17:57:21 +03:00
|
|
|
)
|
2019-11-13 05:22:36 +03:00
|
|
|
(Decode.succeed ())
|
2019-10-24 18:26:41 +03:00
|
|
|
)
|
|
|
|
]
|
2019-11-12 03:56:25 +03:00
|
|
|
|> ProgramTest.ensureHttpRequest "GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages?apiKey=ABCD1234"
|
|
|
|
(\request ->
|
|
|
|
request.headers
|
|
|
|
|> Expect.equal [ ( "Authorization", "Bearer XYZ789" ) ]
|
|
|
|
)
|
|
|
|
|> ProgramTest.simulateHttpResponse
|
2019-10-24 18:26:41 +03:00
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages?apiKey=ABCD1234"
|
2019-11-12 03:56:25 +03:00
|
|
|
(Test.Http.httpResponse
|
|
|
|
{ statusCode = 200
|
|
|
|
, headers = []
|
|
|
|
, body = """{ "stargazer_count": 86 }"""
|
|
|
|
}
|
|
|
|
)
|
2020-01-03 23:18:18 +03:00
|
|
|
|> expectSuccess
|
2020-03-11 19:03:47 +03:00
|
|
|
[ ( ""
|
2020-01-03 23:18:18 +03:00
|
|
|
, [ ( { method = "GET"
|
|
|
|
, url = "https://api.github.com/repos/dillonkearns/elm-pages?apiKey=<API_KEY>"
|
|
|
|
, headers =
|
|
|
|
[ ( "Authorization", "Bearer <BEARER>" )
|
|
|
|
]
|
2020-01-04 00:57:45 +03:00
|
|
|
, body = StaticHttp.emptyBody
|
2019-10-24 18:26:41 +03:00
|
|
|
}
|
2020-01-03 23:18:18 +03:00
|
|
|
, """{}"""
|
|
|
|
)
|
2019-10-24 18:26:41 +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" <|
|
|
|
|
\() ->
|
2020-05-14 07:14:05 +03:00
|
|
|
startWithHttpCache (Ok ())
|
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}"""
|
|
|
|
)
|
|
|
|
]
|
|
|
|
[ ( []
|
|
|
|
, StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|> expectSuccess
|
|
|
|
[ ( ""
|
|
|
|
, [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":86}"""
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2020-04-19 20:35:17 +03:00
|
|
|
, test "it ignores unused cache" <|
|
|
|
|
\() ->
|
2020-05-14 07:14:05 +03:00
|
|
|
startWithHttpCache (Ok ())
|
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}"""
|
|
|
|
)
|
|
|
|
]
|
|
|
|
[ ( []
|
|
|
|
, StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
"""{ "stargazer_count": 86 }"""
|
|
|
|
|> expectSuccess
|
|
|
|
[ ( ""
|
|
|
|
, [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
, """{"stargazer_count":86}"""
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2020-04-19 18:17:51 +03:00
|
|
|
]
|
2020-05-14 07:14:05 +03:00
|
|
|
, describe "document parsing errors"
|
|
|
|
[ test "fails the build when there is an error" <|
|
|
|
|
\() ->
|
|
|
|
startWithHttpCache (Err "Found an unhandled HTML tag in markdown doc.")
|
|
|
|
[]
|
|
|
|
[ ( []
|
|
|
|
, StaticHttp.succeed ()
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|> expectError
|
|
|
|
[ """-- METADATA DECODE ERROR ----------------------------------------------------- elm-pages
|
|
|
|
|
2020-07-19 21:09:38 +03:00
|
|
|
I ran into a problem when parsing the metadata for the page with this path: /
|
2020-05-14 07:14:05 +03:00
|
|
|
|
|
|
|
Found an unhandled HTML tag in markdown doc."""
|
|
|
|
]
|
|
|
|
]
|
2020-06-13 23:43:35 +03:00
|
|
|
, describe "generateFiles"
|
|
|
|
[ test "initial requests are sent out" <|
|
|
|
|
\() ->
|
|
|
|
startLowLevel
|
|
|
|
(StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages")
|
|
|
|
(starDecoder
|
|
|
|
|> Decode.map
|
|
|
|
(\starCount ->
|
|
|
|
[ Ok
|
|
|
|
{ path = [ "test.txt" ]
|
|
|
|
, content = "Star count: " ++ String.fromInt starCount
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
(Ok ())
|
|
|
|
[]
|
|
|
|
[]
|
|
|
|
|> ProgramTest.simulateHttpOk
|
|
|
|
"GET"
|
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
|
|
|
"""{ "stargazer_count": 86 }"""
|
|
|
|
|> expectSuccessNew
|
2020-06-16 05:42:04 +03:00
|
|
|
[]
|
2020-06-13 23:43:35 +03:00
|
|
|
[ \success ->
|
|
|
|
success.filesToGenerate
|
|
|
|
|> Expect.equal
|
|
|
|
[ { path = [ "test.txt" ]
|
|
|
|
, content = "Star count: 86"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
2020-06-16 05:42:04 +03:00
|
|
|
, test "it sends success port when no HTTP requests are needed because they're all cached" <|
|
|
|
|
\() ->
|
|
|
|
startLowLevel
|
|
|
|
(StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages-starter")
|
|
|
|
(starDecoder
|
|
|
|
|> Decode.map
|
|
|
|
(\starCount ->
|
|
|
|
[ Ok
|
|
|
|
{ path = [ "test.txt" ]
|
|
|
|
, content = "Star count: " ++ String.fromInt starCount
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
(Ok ())
|
|
|
|
[ ( { 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}"""
|
|
|
|
)
|
|
|
|
]
|
|
|
|
[ ( []
|
|
|
|
, StaticHttp.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"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
2020-06-13 23:43:35 +03:00
|
|
|
]
|
2019-10-22 05:11:50 +03:00
|
|
|
]
|
2019-10-22 04:49:46 +03:00
|
|
|
|
|
|
|
|
2020-10-14 01:40:36 +03:00
|
|
|
start : List ( List String, StaticHttp.Request a ) -> ProgramTest (Main.Model PathKey ()) Main.Msg (Effect PathKey)
|
2019-10-22 16:46:31 +03:00
|
|
|
start pages =
|
2020-05-14 07:14:05 +03:00
|
|
|
startWithHttpCache (Ok ()) [] pages
|
2020-04-19 18:17:51 +03:00
|
|
|
|
|
|
|
|
2020-06-13 22:29:36 +03:00
|
|
|
startWithHttpCache :
|
|
|
|
Result String ()
|
|
|
|
-> List ( Request.Request, String )
|
|
|
|
-> List ( List String, StaticHttp.Request a )
|
2020-10-14 01:40:36 +03:00
|
|
|
-> ProgramTest (Main.Model PathKey ()) Main.Msg (Effect PathKey)
|
2020-06-13 22:31:47 +03:00
|
|
|
startWithHttpCache =
|
|
|
|
startLowLevel (StaticHttp.succeed [])
|
|
|
|
|
|
|
|
|
|
|
|
startLowLevel :
|
|
|
|
StaticHttp.Request
|
|
|
|
(List
|
2020-10-10 03:08:02 +03:00
|
|
|
(Result String
|
2020-06-13 22:31:47 +03:00
|
|
|
{ path : List String
|
|
|
|
, content : String
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
-> Result String ()
|
|
|
|
-> List ( Request.Request, String )
|
|
|
|
-> List ( List String, StaticHttp.Request a )
|
2020-10-14 01:40:36 +03:00
|
|
|
-> ProgramTest (Main.Model PathKey ()) Main.Msg (Effect PathKey)
|
2020-06-13 22:31:47 +03:00
|
|
|
startLowLevel generateFiles documentBodyResult staticHttpCache pages =
|
2019-10-22 04:49:46 +03:00
|
|
|
let
|
|
|
|
document =
|
2019-10-22 05:56:48 +03:00
|
|
|
Document.fromList
|
|
|
|
[ Document.parser
|
|
|
|
{ extension = "md"
|
2019-11-13 05:22:36 +03:00
|
|
|
, metadata = JD.succeed ()
|
2020-05-14 07:14:05 +03:00
|
|
|
, body = \_ -> documentBodyResult
|
2019-10-22 05:56:48 +03:00
|
|
|
}
|
|
|
|
]
|
2019-10-22 04:49:46 +03:00
|
|
|
|
|
|
|
content =
|
2019-10-22 16:46:31 +03:00
|
|
|
pages
|
|
|
|
|> List.map
|
|
|
|
(\( path, _ ) ->
|
|
|
|
( path, { extension = "md", frontMatter = "null", body = Just "" } )
|
|
|
|
)
|
2019-10-22 04:49:46 +03:00
|
|
|
|
|
|
|
contentCache =
|
2020-01-26 21:19:37 +03:00
|
|
|
ContentCache.init document content Nothing
|
2019-10-22 04:49:46 +03:00
|
|
|
|
|
|
|
siteMetadata =
|
|
|
|
contentCache
|
|
|
|
|> Result.map
|
|
|
|
(\cache -> cache |> ContentCache.extractMetadata PathKey)
|
2019-10-29 22:37:28 +03:00
|
|
|
|> Result.mapError (List.map Tuple.second)
|
2019-10-22 04:49:46 +03:00
|
|
|
|
|
|
|
config =
|
|
|
|
{ toJsPort = toJsPort
|
2020-04-17 07:31:21 +03:00
|
|
|
, fromJsPort = fromJsPort
|
2019-10-22 04:49:46 +03:00
|
|
|
, manifest = manifest
|
2020-06-13 22:31:47 +03:00
|
|
|
, generateFiles = \_ -> generateFiles
|
2020-01-26 21:19:37 +03:00
|
|
|
, init = \_ -> ( (), Cmd.none )
|
|
|
|
, update = \_ _ -> ( (), Cmd.none )
|
2019-10-22 04:49:46 +03:00
|
|
|
, view =
|
|
|
|
\allFrontmatter page ->
|
2019-10-22 16:46:31 +03:00
|
|
|
let
|
2019-10-24 15:52:24 +03:00
|
|
|
thing =
|
2019-10-22 16:46:31 +03:00
|
|
|
pages
|
|
|
|
|> Dict.fromList
|
|
|
|
|> Dict.get
|
|
|
|
(page.path
|
|
|
|
|> PagePath.toString
|
|
|
|
|> String.split "/"
|
|
|
|
|> List.filter (\pathPart -> pathPart /= "")
|
|
|
|
)
|
|
|
|
in
|
2019-10-24 15:52:24 +03:00
|
|
|
case thing of
|
2019-10-28 17:57:21 +03:00
|
|
|
Just request ->
|
|
|
|
request
|
2019-10-24 15:52:24 +03:00
|
|
|
|> StaticHttp.map
|
|
|
|
(\staticData -> { view = \model viewForPage -> { title = "Title", body = Html.text "" }, head = [] })
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Debug.todo "Couldn't find page"
|
2020-10-26 21:50:06 +03:00
|
|
|
, subscriptions = \_ _ _ -> Sub.none
|
2020-01-26 21:19:37 +03:00
|
|
|
, document = document
|
|
|
|
, content = []
|
2020-10-21 06:48:21 +03:00
|
|
|
, canonicalSiteUrl = canonicalSiteUrl
|
2020-01-26 21:19:37 +03:00
|
|
|
, pathKey = PathKey
|
2020-05-11 20:42:40 +03:00
|
|
|
, onPageChange = Just (\_ -> ())
|
2019-10-22 04:49:46 +03:00
|
|
|
}
|
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 )
|
|
|
|
]
|
|
|
|
|
|
|
|
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
|
|
|
|
-> Result (List BuildError) (List ( PagePath pathKey, metadata ))
|
|
|
|
-> Config pathKey userMsg userModel metadata view
|
|
|
|
-> Decode.Value
|
|
|
|
-> ( model, Effect pathKey )
|
|
|
|
-}
|
2019-10-22 04:49:46 +03:00
|
|
|
ProgramTest.createDocument
|
2019-11-13 05:24:54 +03:00
|
|
|
{ init = Main.init identity contentCache siteMetadata config
|
2020-10-12 07:21:34 +03:00
|
|
|
, update = Main.update contentCache siteMetadata 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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-06-12 06:57:19 +03:00
|
|
|
simulateEffects : Effect PathKey -> ProgramTest.SimulatedEffect Main.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
|
|
|
|
|
2020-06-12 06:57:19 +03:00
|
|
|
Effect.SendJsData value ->
|
2020-10-21 06:48:21 +03:00
|
|
|
SimulatedEffect.Ports.send "toJsPort" (value |> Codec.encoder (ToJsPayload.toJsCodec canonicalSiteUrl))
|
2019-10-22 05:31:17 +03:00
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
2020-06-12 06:57:19 +03:00
|
|
|
Effect.FetchHttp ({ unmasked, masked } as requests) ->
|
2019-11-08 22:34:51 +03:00
|
|
|
Http.request
|
|
|
|
{ method = unmasked.method
|
2020-03-04 19:17:06 +03:00
|
|
|
, url = unmasked.url
|
2019-11-12 03:56:25 +03:00
|
|
|
, headers = unmasked.headers |> List.map (\( key, value ) -> Http.header key value)
|
2020-03-04 19:17:06 +03:00
|
|
|
, body =
|
|
|
|
case unmasked.body of
|
|
|
|
StaticHttpBody.EmptyBody ->
|
|
|
|
Http.emptyBody
|
|
|
|
|
|
|
|
StaticHttpBody.StringBody contentType string ->
|
|
|
|
Http.stringBody contentType string
|
|
|
|
|
|
|
|
StaticHttpBody.JsonBody value ->
|
|
|
|
Http.jsonBody value
|
2019-10-22 05:31:17 +03:00
|
|
|
, expect =
|
2020-03-04 19:17:06 +03:00
|
|
|
PagesHttp.expectString
|
2019-10-22 05:31:17 +03:00
|
|
|
(\response ->
|
2019-10-24 17:06:50 +03:00
|
|
|
GotStaticHttpResponse
|
2019-11-12 03:56:25 +03:00
|
|
|
{ request = requests
|
2019-10-22 05:31:17 +03:00
|
|
|
, response = response
|
|
|
|
}
|
|
|
|
)
|
2019-11-08 22:34:51 +03:00
|
|
|
, timeout = Nothing
|
|
|
|
, tracker = Nothing
|
2019-10-22 05:31:17 +03:00
|
|
|
}
|
|
|
|
|
2020-10-10 03:08:02 +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"
|
|
|
|
, SimulatedEffect.Task.succeed ()
|
|
|
|
|> SimulatedEffect.Task.perform (\_ -> Main.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.Task.succeed ()
|
|
|
|
-- |> SimulatedEffect.Task.perform (\_ -> Continue)
|
|
|
|
SimulatedEffect.Cmd.none
|
2020-10-14 01:40:36 +03:00
|
|
|
|
2019-10-22 05:31:17 +03:00
|
|
|
|
2020-01-28 02:28:42 +03:00
|
|
|
expectErrorsPort : String -> List (ToJsPayload pathKey) -> Expect.Expectation
|
2019-11-03 00:46:52 +03:00
|
|
|
expectErrorsPort expectedPlainString actualPorts =
|
|
|
|
case actualPorts of
|
2020-06-12 06:55:19 +03:00
|
|
|
[ ToJsPayload.Errors actualRichTerminalString ] ->
|
2020-01-28 02:28:42 +03:00
|
|
|
actualRichTerminalString
|
|
|
|
|> normalizeErrorExpectEqual expectedPlainString
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
expectNonfatalErrorsPort : String -> List (ToJsPayload pathKey) -> Expect.Expectation
|
|
|
|
expectNonfatalErrorsPort expectedPlainString actualPorts =
|
|
|
|
case actualPorts of
|
2020-06-12 06:55:19 +03:00
|
|
|
[ ToJsPayload.Success successPayload ] ->
|
2020-01-28 02:28:42 +03:00
|
|
|
successPayload.errors
|
|
|
|
|> String.join "\n\n"
|
|
|
|
|> normalizeErrorExpectEqual expectedPlainString
|
2019-11-03 00:46:52 +03:00
|
|
|
|
|
|
|
_ ->
|
2020-01-28 02:28:42 +03:00
|
|
|
Expect.fail <| "Expected single non-fatal error port. Got\n" ++ String.join "\n\n" (List.map Debug.toString actualPorts)
|
|
|
|
|
|
|
|
|
|
|
|
normalizeErrorExpectEqual : String -> String -> Expect.Expectation
|
|
|
|
normalizeErrorExpectEqual expectedPlainString actualRichTerminalString =
|
|
|
|
actualRichTerminalString
|
|
|
|
|> Regex.replace
|
|
|
|
(Regex.fromString "\u{001B}\\[[0-9;]+m"
|
|
|
|
|> Maybe.withDefault Regex.never
|
|
|
|
)
|
|
|
|
(\_ -> "")
|
|
|
|
|> Expect.equal expectedPlainString
|
2019-11-03 00:46:52 +03:00
|
|
|
|
|
|
|
|
2020-05-14 07:14:05 +03:00
|
|
|
normalizeErrorsExpectEqual : List String -> List String -> Expect.Expectation
|
|
|
|
normalizeErrorsExpectEqual expectedPlainStrings actualRichTerminalStrings =
|
|
|
|
actualRichTerminalStrings
|
|
|
|
|> List.map
|
|
|
|
(Regex.replace
|
|
|
|
(Regex.fromString "\u{001B}\\[[0-9;]+m"
|
|
|
|
|> Maybe.withDefault Regex.never
|
|
|
|
)
|
|
|
|
(\_ -> "")
|
|
|
|
)
|
|
|
|
|> Expect.equalLists expectedPlainStrings
|
|
|
|
|
|
|
|
|
2019-10-22 04:49:46 +03:00
|
|
|
toJsPort foo =
|
|
|
|
Cmd.none
|
|
|
|
|
|
|
|
|
2020-04-17 07:31:21 +03:00
|
|
|
fromJsPort =
|
|
|
|
Sub.none
|
|
|
|
|
|
|
|
|
2019-10-22 04:49:46 +03:00
|
|
|
type PathKey
|
|
|
|
= PathKey
|
|
|
|
|
|
|
|
|
|
|
|
manifest : Manifest.Config PathKey
|
|
|
|
manifest =
|
|
|
|
{ backgroundColor = Nothing
|
|
|
|
, categories = []
|
|
|
|
, displayMode = Manifest.Standalone
|
|
|
|
, orientation = Manifest.Portrait
|
|
|
|
, description = "elm-pages - A statically typed site generator."
|
|
|
|
, iarcRatingId = Nothing
|
|
|
|
, name = "elm-pages docs"
|
|
|
|
, themeColor = Nothing
|
2019-10-22 09:02:31 +03:00
|
|
|
, startUrl = PagePath.external ""
|
2019-10-22 04:49:46 +03:00
|
|
|
, shortName = Just "elm-pages"
|
2019-10-22 09:02:31 +03:00
|
|
|
, sourceIcon = ImagePath.external ""
|
2020-10-21 05:38:51 +03:00
|
|
|
, icons = []
|
2019-10-22 04:49:46 +03:00
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
|
|
expectSuccess : List ( String, List ( Request.Request, String ) ) -> ProgramTest model msg effect -> Expect.Expectation
|
|
|
|
expectSuccess expectedRequests previous =
|
2020-06-16 05:42:04 +03:00
|
|
|
expectSuccessNew expectedRequests [] previous
|
|
|
|
|
|
|
|
|
|
|
|
expectSuccessNew : List ( String, List ( Request.Request, String ) ) -> List (ToJsPayload.ToJsSuccessPayload PathKey -> Expect.Expectation) -> ProgramTest model msg effect -> Expect.Expectation
|
|
|
|
expectSuccessNew expectedRequests expectations previous =
|
2020-01-03 23:18:18 +03:00
|
|
|
previous
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
2020-10-21 06:48:21 +03:00
|
|
|
(Codec.decoder (ToJsPayload.toJsCodec canonicalSiteUrl))
|
2020-04-19 18:17:51 +03:00
|
|
|
(\value ->
|
|
|
|
case value of
|
2020-06-16 05:42:04 +03:00
|
|
|
(ToJsPayload.Success portPayload) :: rest ->
|
|
|
|
portPayload
|
|
|
|
|> Expect.all
|
|
|
|
((\subject ->
|
|
|
|
subject.pages
|
|
|
|
|> Expect.equalDicts
|
|
|
|
(expectedRequests
|
2020-04-19 18:17:51 +03:00
|
|
|
|> List.map
|
2020-06-16 05:42:04 +03:00
|
|
|
(\( url, requests ) ->
|
|
|
|
( url
|
|
|
|
, requests
|
|
|
|
|> List.map
|
|
|
|
(\( request, response ) ->
|
|
|
|
( Request.hash request, response )
|
|
|
|
)
|
|
|
|
|> Dict.fromList
|
|
|
|
)
|
2020-04-19 18:17:51 +03:00
|
|
|
)
|
|
|
|
|> Dict.fromList
|
2020-01-03 23:18:18 +03:00
|
|
|
)
|
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
|
|
|
|
|
|
|
[ _ ] ->
|
|
|
|
Expect.fail "Expected success port."
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-14 07:14:05 +03:00
|
|
|
expectError : List String -> ProgramTest model msg effect -> Expect.Expectation
|
|
|
|
expectError expectedErrors previous =
|
|
|
|
previous
|
|
|
|
|> ProgramTest.expectOutgoingPortValues
|
|
|
|
"toJsPort"
|
2020-10-21 06:48:21 +03:00
|
|
|
(Codec.decoder (ToJsPayload.toJsCodec canonicalSiteUrl))
|
2020-05-14 07:14:05 +03:00
|
|
|
(\value ->
|
|
|
|
case value of
|
2020-06-12 06:55:19 +03:00
|
|
|
[ ToJsPayload.Success portPayload ] ->
|
2020-05-14 07:14:05 +03:00
|
|
|
portPayload.errors
|
|
|
|
|> normalizeErrorsExpectEqual expectedErrors
|
|
|
|
|
|
|
|
[ _ ] ->
|
|
|
|
Expect.fail "Expected success port."
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Expect.fail ("Expected ports to be called once, but instead there were " ++ String.fromInt (List.length value) ++ " calls.")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-10-21 06:48:21 +03:00
|
|
|
canonicalSiteUrl =
|
|
|
|
""
|
|
|
|
|
|
|
|
|
2020-01-03 23:18:18 +03:00
|
|
|
get : String -> Request.Request
|
|
|
|
get url =
|
|
|
|
{ method = "GET"
|
|
|
|
, url = url
|
|
|
|
, headers = []
|
2020-01-04 00:57:45 +03:00
|
|
|
, body = StaticHttp.emptyBody
|
2020-01-03 23:18:18 +03:00
|
|
|
}
|