Use ports for HTTP requests in unit tests.

This commit is contained in:
Dillon Kearns 2021-07-29 09:17:36 -07:00
parent 780e78fe02
commit dfe6330abf
2 changed files with 363 additions and 327 deletions

View File

@ -5,6 +5,7 @@ module Pages.Internal.Platform.Cli exposing
, Program , Program
, cliApplication , cliApplication
, init , init
, requestDecoder
, update , update
) )

View File

@ -40,14 +40,10 @@ all =
describe "Static Http Requests" describe "Static Http Requests"
[ test "initial requests are sent out" <| [ test "initial requests are sent out" <|
\() -> \() ->
start startSimple []
[ ( [] (DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder)
, 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"))
]
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86 }""" """{ "stargazer_count": 86 }"""
|> expectSuccess |> expectSuccess
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages" [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
@ -56,19 +52,11 @@ all =
] ]
, test "StaticHttp request for initial are resolved" <| , test "StaticHttp request for initial are resolved" <|
\() -> \() ->
start startSimple
[ ( [ "post-1" ] [ "post-1" ]
, DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder (DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder)
--, StaticHttp.succeed 86 |> simulateHttp
) (Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
]
--|> ProgramTest.simulateHttpOk
-- "GET"
-- "https://my-cms.com/posts"
-- """{ "posts": ["post-1"] }"""
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86 }""" """{ "stargazer_count": 86 }"""
|> expectSuccess |> expectSuccess
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages" [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
@ -78,17 +66,10 @@ all =
, describe "single page renders" , describe "single page renders"
[ test "single pages that are pre-rendered" <| [ test "single pages that are pre-rendered" <|
\() -> \() ->
startWithRoutes [ "post-1" ] startSimple [ "post-1" ]
[ [ "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"))
[ ( [ "post-1" ]
, DataSource.Http.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 }""" """{ "stargazer_count": 86 }"""
|> ProgramTest.expectOutgoingPortValues |> ProgramTest.expectOutgoingPortValues
"toJsPort" "toJsPort"
@ -127,20 +108,18 @@ all =
] ]
, test "the stripped JSON from the same request with different decoders is merged so the decoders succeed" <| , test "the stripped JSON from the same request with different decoders is merged so the decoders succeed" <|
\() -> \() ->
start startSimple
[ ( [ "post-1" ] [ "post-1" ]
, DataSource.map2 Tuple.pair (DataSource.map2 Tuple.pair
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages")
(Decode.field "stargazer_count" Decode.int) (Decode.field "stargazer_count" Decode.int)
) )
(DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages")
(Decode.field "language" Decode.string) (Decode.field "language" Decode.string)
) )
) )
] |> simulateHttp
|> ProgramTest.simulateHttpOk (Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86, "language": "Elm" }""" """{ "stargazer_count": 86, "language": "Elm" }"""
|> expectSuccess |> expectSuccess
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages" [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
@ -149,22 +128,19 @@ all =
] ]
, test "andThen" <| , test "andThen" <|
\() -> \() ->
start startSimple
[ ( [ "elm-pages" ] [ "elm-pages" ]
, DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.succeed ()) (DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") (Decode.succeed ())
|> DataSource.andThen |> DataSource.andThen
(\_ -> (\_ ->
DataSource.Http.get (Secrets.succeed "NEXT-REQUEST") (Decode.succeed ()) DataSource.Http.get (Secrets.succeed "NEXT-REQUEST") (Decode.succeed ())
) )
) )
] |> simulateHttp
|> ProgramTest.simulateHttpOk (Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""null""" """null"""
|> ProgramTest.simulateHttpOk |> simulateHttp
"GET" (Secrets.succeed (get "NEXT-REQUEST"))
"NEXT-REQUEST"
"""null""" """null"""
|> expectSuccess |> expectSuccess
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages" [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
@ -199,13 +175,12 @@ all =
|> DataSource.resolve |> DataSource.resolve
|> DataSource.map (\_ -> ()) |> DataSource.map (\_ -> ())
in in
start startSimple
[ ( [ "elm-pages" ], pokemonDetailRequest ) [ "elm-pages" ]
] pokemonDetailRequest
|> ProgramTest.simulateHttpOk |> simulateMultipleHttp
"GET" [ ( Secrets.succeed (get "https://pokeapi.co/api/v2/pokemon/")
"https://pokeapi.co/api/v2/pokemon/" , """[
"""[
{"url": "url1"}, {"url": "url1"},
{"url": "url2"}, {"url": "url2"},
{"url": "url3"}, {"url": "url3"},
@ -217,46 +192,38 @@ all =
{"url": "url9"}, {"url": "url9"},
{"url": "url10"} {"url": "url10"}
]""" ]"""
|> ProgramTest.simulateHttpOk )
"GET" , ( Secrets.succeed (get "url1")
"url1" , """{"image": "image1.jpg"}"""
"""{"image": "image1.jpg"}""" )
|> ProgramTest.simulateHttpOk , ( Secrets.succeed (get "url2")
"GET" , """{"image": "image2.jpg"}"""
"url2" )
"""{"image": "image2.jpg"}""" , ( Secrets.succeed (get "url3")
|> ProgramTest.simulateHttpOk , """{"image": "image3.jpg"}"""
"GET" )
"url3" , ( Secrets.succeed (get "url4")
"""{"image": "image3.jpg"}""" , """{"image": "image4.jpg"}"""
|> ProgramTest.simulateHttpOk )
"GET" , ( Secrets.succeed (get "url5")
"url4" , """{"image": "image5.jpg"}"""
"""{"image": "image4.jpg"}""" )
|> ProgramTest.simulateHttpOk , ( Secrets.succeed (get "url6")
"GET" , """{"image": "image6.jpg"}"""
"url5" )
"""{"image": "image5.jpg"}""" , ( Secrets.succeed (get "url7")
|> ProgramTest.simulateHttpOk , """{"image": "image7.jpg"}"""
"GET" )
"url6" , ( Secrets.succeed (get "url8")
"""{"image": "image6.jpg"}""" , """{"image": "image8.jpg"}"""
|> ProgramTest.simulateHttpOk )
"GET" , ( Secrets.succeed (get "url9")
"url7" , """{"image": "image9.jpg"}"""
"""{"image": "image7.jpg"}""" )
|> ProgramTest.simulateHttpOk , ( Secrets.succeed (get "url10")
"GET" , """{"image": "image10.jpg"}"""
"url8" )
"""{"image": "image8.jpg"}""" ]
|> ProgramTest.simulateHttpOk
"GET"
"url9"
"""{"image": "image9.jpg"}"""
|> ProgramTest.simulateHttpOk
"GET"
"url10"
"""{"image": "image10.jpg"}"""
|> expectSuccess |> expectSuccess
[ ( get "https://pokeapi.co/api/v2/pokemon/" [ ( 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"}]""" , """[{"url":"url1"},{"url":"url2"},{"url":"url3"},{"url":"url4"},{"url":"url5"},{"url":"url6"},{"url":"url7"},{"url":"url8"},{"url":"url9"},{"url":"url10"}]"""
@ -327,14 +294,10 @@ all =
-- ] -- ]
, test "reduced JSON is sent out" <| , test "reduced JSON is sent out" <|
\() -> \() ->
start startSimple []
[ ( [] (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 "stargazer_count" Decode.int) |> simulateHttp
) (Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
]
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86, "unused_field": 123 }""" """{ "stargazer_count": 86, "unused_field": 123 }"""
|> expectSuccess |> expectSuccess
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages" [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
@ -343,24 +306,21 @@ all =
] ]
, test "you can use elm/json decoders with StaticHttp.unoptimizedRequest" <| , test "you can use elm/json decoders with StaticHttp.unoptimizedRequest" <|
\() -> \() ->
start startSimple []
[ ( [] (DataSource.Http.unoptimizedRequest
, DataSource.Http.unoptimizedRequest (Secrets.succeed
(Secrets.succeed { url = "https://api.github.com/repos/dillonkearns/elm-pages"
{ url = "https://api.github.com/repos/dillonkearns/elm-pages" , method = "GET"
, method = "GET" , headers = []
, headers = [] , body = DataSource.emptyBody
, body = DataSource.emptyBody }
} )
) (DataSource.Http.expectUnoptimizedJson
(DataSource.Http.expectUnoptimizedJson (JD.field "stargazer_count" JD.int)
(JD.field "stargazer_count" JD.int) )
) )
) |> simulateHttp
] (Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86, "unused_field": 123 }""" """{ "stargazer_count": 86, "unused_field": 123 }"""
|> expectSuccess |> expectSuccess
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages" [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
@ -369,22 +329,19 @@ all =
] ]
, test "plain string" <| , test "plain string" <|
\() -> \() ->
start startSimple []
[ ( [] (DataSource.Http.unoptimizedRequest
, DataSource.Http.unoptimizedRequest (Secrets.succeed
(Secrets.succeed { url = "https://example.com/file.txt"
{ url = "https://example.com/file.txt" , method = "GET"
, method = "GET" , headers = []
, headers = [] , body = DataSource.emptyBody
, body = DataSource.emptyBody }
} )
) (DataSource.Http.expectString Ok)
(DataSource.Http.expectString Ok) )
) |> simulateHttp
] (Secrets.succeed (get "https://example.com/file.txt"))
|> ProgramTest.simulateHttpOk
"GET"
"https://example.com/file.txt"
"This is a raw text file." "This is a raw text file."
|> expectSuccess |> expectSuccess
[ ( get "https://example.com/file.txt" [ ( get "https://example.com/file.txt"
@ -393,30 +350,27 @@ all =
] ]
, test "Err in String to Result function turns into decode error" <| , test "Err in String to Result function turns into decode error" <|
\() -> \() ->
start startSimple []
[ ( [] (DataSource.Http.unoptimizedRequest
, DataSource.Http.unoptimizedRequest (Secrets.succeed
(Secrets.succeed { url = "https://example.com/file.txt"
{ url = "https://example.com/file.txt" , method = "GET"
, method = "GET" , headers = []
, headers = [] , body = DataSource.emptyBody
, body = DataSource.emptyBody }
} )
) (DataSource.Http.expectString
(DataSource.Http.expectString (\string ->
(\string -> if String.toUpper string == string then
if String.toUpper string == string then Ok string
Ok string
else else
Err "String was not uppercased" Err "String was not uppercased"
)
) )
) )
] )
|> ProgramTest.simulateHttpOk |> simulateHttp
"GET" (Secrets.succeed (get "https://example.com/file.txt"))
"https://example.com/file.txt"
"This is a raw text file." "This is a raw text file."
|> ProgramTest.expectOutgoingPortValues |> ProgramTest.expectOutgoingPortValues
"toJsPort" "toJsPort"
@ -430,22 +384,19 @@ String was not uppercased"""
) )
, test "POST method works" <| , test "POST method works" <|
\() -> \() ->
start startSimple []
[ ( [] (DataSource.Http.request
, DataSource.Http.request (Secrets.succeed
(Secrets.succeed { method = "POST"
{ method = "POST" , url = "https://api.github.com/repos/dillonkearns/elm-pages"
, url = "https://api.github.com/repos/dillonkearns/elm-pages" , headers = []
, headers = [] , body = DataSource.emptyBody
, body = DataSource.emptyBody }
} )
) (Decode.field "stargazer_count" Decode.int)
(Decode.field "stargazer_count" Decode.int) )
) |> simulateHttp
] (Secrets.succeed (post "https://api.github.com/repos/dillonkearns/elm-pages"))
|> ProgramTest.simulateHttpOk
"POST"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86, "unused_field": 123 }""" """{ "stargazer_count": 86, "unused_field": 123 }"""
|> expectSuccess |> expectSuccess
[ ( { method = "POST" [ ( { method = "POST"
@ -458,22 +409,18 @@ String was not uppercased"""
] ]
, test "json is reduced from andThen chains" <| , test "json is reduced from andThen chains" <|
\() -> \() ->
start startSimple []
[ ( [] (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 "stargazer_count" Decode.int) |> DataSource.andThen
|> DataSource.andThen (\_ ->
(\_ -> DataSource.Http.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages-starter") (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) )
) )
) |> simulateHttp
] (Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages"))
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 100, "unused_field": 123 }""" """{ "stargazer_count": 100, "unused_field": 123 }"""
|> ProgramTest.simulateHttpOk |> simulateHttp
"GET" (Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages-starter"))
"https://api.github.com/repos/dillonkearns/elm-pages-starter"
"""{ "stargazer_count": 50, "unused_field": 456 }""" """{ "stargazer_count": 50, "unused_field": 456 }"""
|> expectSuccess |> expectSuccess
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages" [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
@ -485,21 +432,19 @@ String was not uppercased"""
] ]
, test "reduced json is preserved by StaticHttp.map2" <| , test "reduced json is preserved by StaticHttp.map2" <|
\() -> \() ->
start startSimple []
[ ( [] (DataSource.map2 (\_ _ -> ())
, 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") (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))
(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")
|> ProgramTest.simulateHttpOk , """{ "stargazer_count": 100, "unused_field": 123 }"""
"GET" )
"https://api.github.com/repos/dillonkearns/elm-pages" , ( Secrets.succeed (get "https://api.github.com/repos/dillonkearns/elm-pages-starter")
"""{ "stargazer_count": 100, "unused_field": 123 }""" , """{ "stargazer_count": 50, "unused_field": 456 }"""
|> ProgramTest.simulateHttpOk )
"GET" ]
"https://api.github.com/repos/dillonkearns/elm-pages-starter"
"""{ "stargazer_count": 50, "unused_field": 456 }"""
|> expectSuccess |> expectSuccess
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages" [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
, """{"stargazer_count":100}""" , """{"stargazer_count":100}"""
@ -518,16 +463,13 @@ String was not uppercased"""
|> expectSuccess [] |> expectSuccess []
, test "the port sends out when there are duplicate http requests for the same page" <| , test "the port sends out when there are duplicate http requests for the same page" <|
\() -> \() ->
start startSimple []
[ ( [] (DataSource.map2 (\_ _ -> ())
, DataSource.map2 (\_ _ -> ()) (DataSource.Http.get (Secrets.succeed "http://example.com") (Decode.succeed ()))
(DataSource.Http.get (Secrets.succeed "http://example.com") (Decode.succeed ())) (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"))
|> ProgramTest.simulateHttpOk
"GET"
"http://example.com"
"""null""" """null"""
|> expectSuccess |> expectSuccess
[ ( get "http://example.com" [ ( get "http://example.com"
@ -536,14 +478,10 @@ String was not uppercased"""
] ]
, test "an error is sent out for decoder failures" <| , test "an error is sent out for decoder failures" <|
\() -> \() ->
start startSimple [ "elm-pages" ]
[ ( [ "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."))
, 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"))
]
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86 }""" """{ "stargazer_count": 86 }"""
|> ProgramTest.expectOutgoingPortValues |> ProgramTest.expectOutgoingPortValues
"toJsPort" "toJsPort"
@ -561,32 +499,36 @@ I encountered some errors while decoding this JSON:
) )
, test "an error is sent for missing secrets from continuation requests" <| , test "an error is sent for missing secrets from continuation requests" <|
\() -> \() ->
start startSimple
[ ( [ "elm-pages" ] [ "elm-pages" ]
, DataSource.Http.get (DataSource.Http.get
(Secrets.succeed (Secrets.succeed
(\apiKey -> (\apiKey ->
"https://api.github.com/repos/dillonkearns/elm-pages?apiKey=" ++ apiKey "https://api.github.com/repos/dillonkearns/elm-pages?apiKey=" ++ apiKey
)
|> Secrets.with "API_KEY"
) )
Decode.string |> Secrets.with "API_KEY"
|> DataSource.andThen )
(\url -> Decode.string
DataSource.Http.get |> DataSource.andThen
(Secrets.succeed (\url ->
(\missingSecret -> DataSource.Http.get
url ++ "?apiKey=" ++ missingSecret (Secrets.succeed
) (\missingSecret ->
|> Secrets.with "MISSING" url ++ "?apiKey=" ++ missingSecret
) )
(Decode.succeed ()) |> Secrets.with "MISSING"
) )
) (Decode.succeed ())
] )
|> ProgramTest.simulateHttpOk )
"GET" |> simulateHttp
"https://api.github.com/repos/dillonkearns/elm-pages?apiKey=ABCD1234" (Secrets.succeed
(\apiKey ->
get
("https://api.github.com/repos/dillonkearns/elm-pages?apiKey=" ++ apiKey)
)
|> Secrets.with "API_KEY"
)
""" "continuation-url" """ """ "continuation-url" """
|> ProgramTest.expectOutgoingPortValues |> ProgramTest.expectOutgoingPortValues
"toJsPort" "toJsPort"
@ -714,31 +656,26 @@ So maybe MISSING should be API_KEY"""
] ]
, test "validate DataSource is not stored for any pages" <| , test "validate DataSource is not stored for any pages" <|
\() -> \() ->
startWithRoutes [ "hello" ] startSimple [ "hello" ]
[ [ "hello" ] ] (DataSource.succeed "hello"
[] |> DataSource.validate identity
[ ( [ "hello" ] (\word ->
, DataSource.succeed "hello" DataSource.Http.get (Secrets.succeed ("https://api.spellchecker.com?word=" ++ word))
|> DataSource.validate identity (Decode.field "isCorrect" Decode.bool
(\word -> |> Decode.map
DataSource.Http.get (Secrets.succeed ("https://api.spellchecker.com?word=" ++ word)) (\isCorrect ->
(Decode.field "isCorrect" Decode.bool if isCorrect then
|> Decode.map Ok ()
(\isCorrect ->
if isCorrect then
Ok ()
else else
Err "Spelling error" Err "Spelling error"
) )
) )
) )
|> DataSource.map (\_ -> ()) |> DataSource.map (\_ -> ())
) )
] |> simulateHttp
|> ProgramTest.simulateHttpOk (Secrets.succeed (get "https://api.spellchecker.com?word=hello"))
"GET"
"https://api.spellchecker.com?word=hello"
"""{ "isCorrect": true }""" """{ "isCorrect": true }"""
|> ProgramTest.expectOutgoingPortValues |> ProgramTest.expectOutgoingPortValues
"toJsPort" "toJsPort"
@ -1229,6 +1166,10 @@ startLowLevel apiRoutes staticHttpCache pages =
|> ProgramTest.start (flags (Encode.encode 0 encodedFlags)) |> ProgramTest.start (flags (Encode.encode 0 encodedFlags))
startSimple route dataSources =
startWithRoutes route [ route ] [] [ ( route, dataSources ) ]
startWithRoutes : startWithRoutes :
List String List String
-> List (List String) -> List (List String)
@ -1398,7 +1339,7 @@ simulateEffects effect =
|> List.map simulateEffects |> List.map simulateEffects
|> SimulatedEffect.Cmd.batch |> SimulatedEffect.Cmd.batch
Effect.FetchHttp { unmasked } -> Effect.FetchHttp { unmasked, masked } ->
if unmasked.url |> String.startsWith "file://" then if unmasked.url |> String.startsWith "file://" then
let let
filePath : String filePath : String
@ -1430,45 +1371,9 @@ simulateEffects effect =
|> SimulatedEffect.Cmd.map never |> SimulatedEffect.Cmd.map never
else else
Http.request ToJsPayload.DoHttp { masked = masked, unmasked = unmasked }
{ method = unmasked.method |> sendToJsPort
, url = unmasked.url |> SimulatedEffect.Cmd.map never
, headers = unmasked.headers |> List.map (\( key, value ) -> Http.header key value)
, body =
case unmasked.body of
StaticHttpBody.EmptyBody ->
Http.emptyBody
StaticHttpBody.StringBody contentType string ->
Http.stringBody contentType string
StaticHttpBody.JsonBody value ->
Http.jsonBody value
, expect =
PagesHttp.expectString
(\response ->
case response of
Ok okResponse ->
GotDataBatch
[ { request =
{ unmasked = unmasked
, masked = unmasked -- TODO use masked
}
, response = okResponse
}
]
Err _ ->
GotBuildError
{ title = "Static HTTP Error"
, message = []
, fatal = True
, path = ""
}
)
, timeout = Nothing
, tracker = Nothing
}
Effect.SendSinglePage done info -> Effect.SendSinglePage done info ->
SimulatedEffect.Cmd.batch SimulatedEffect.Cmd.batch
@ -1495,14 +1400,14 @@ simulateEffects effect =
expectErrorsPort : String -> List ToJsPayload.ToJsSuccessPayloadNewCombined -> Expect.Expectation expectErrorsPort : String -> List ToJsPayload.ToJsSuccessPayloadNewCombined -> Expect.Expectation
expectErrorsPort expectedPlainString actualPorts = expectErrorsPort expectedPlainString actualPorts =
case actualPorts of case actualPorts |> List.reverse |> List.head of
[ ToJsPayload.Errors actualRichTerminalString ] -> Just (ToJsPayload.Errors actualRichTerminalString) ->
actualRichTerminalString actualRichTerminalString
|> List.map .title |> List.map .title
|> String.join "\n" |> String.join "\n"
|> normalizeErrorExpectEqual expectedPlainString |> normalizeErrorExpectEqual expectedPlainString
[] -> Nothing ->
Expect.fail "Expected single error port. Didn't receive any ports." Expect.fail "Expected single error port. Didn't receive any ports."
_ -> _ ->
@ -1669,6 +1574,24 @@ simulateSubscriptions _ =
] ]
) )
"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
_ -> _ ->
JD.fail "Unexpected subscription tag." JD.fail "Unexpected subscription tag."
) )
@ -1683,3 +1606,115 @@ get url =
, headers = [] , headers = []
, body = DataSource.emptyBody , body = DataSource.emptyBody
} }
post : String -> Request.Request
post url =
{ method = "POST"
, url = url
, headers = []
, body = DataSource.emptyBody
}
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
(ToJsPayload.DoHttp _) :: rest ->
-- 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
)
]
)