avh4 / elm-program-test / Test.Http

Convenience functions for testing HTTP requests. Pull requests are welcome to add more useful functions.

Expectations

These functions provide some convenient checks that can be used with ProgramTest.expectHttpRequest.

expectJsonBody : Json.Decode.Decoder requestBody -> (requestBody -> Expectation) -> HttpRequest x a -> Expectation

A convenient way to check something about the request body of a pending HTTP request.

...
    |> ProgramTest.expectHttpRequest "POST"
        "https://example.com/ok"
        (Test.Http.expectJsonBody
            (Json.Decode.field "version" Json.Decode.string)
            (Expect.equal "3.1.5")
        )


type alias HttpRequest x a =
{ method : String
, url : String
, body : String
, headers : List ( String
, String )
, onRequestComplete : Http.Response String -> SimulatedEffect.SimulatedTask x a 
}

hasHeader : String -> String -> HttpRequest x a -> Expectation

Assert that the given HTTP request has the specified header.

...
    |> ProgramTest.expectHttpRequest "POST"
        "https://example.com/ok"
        (Test.Http.hasHeader "Content-Type" "application/json")

Responses

These are ways to easily make Http.Response values for use with ProgramTest.simulateHttpResponse.

timeout : Http.Response body

This is the same as Http.Timeout_, but is exposed here so that your test doesn't need to import both Http and Test.Http.

networkError : Http.Response body

This is the same as Http.NetworkError_, but is exposed here so that your test doesn't need to import both Http and Test.Http.

httpResponse : { statusCode : Basics.Int, headers : List ( String, String ), body : body } -> Http.Response body

This is a more convenient way to create Http.BadStatus_ and Http.GoodStatus_ values.

Following the logic in elm/http, this will produce Http.GoodStatus_ if the given status code is in the 200 series, otherwise it will produce Http.BadStatus_.