Mock an HTTP response from within Elm.
Specify exactly what you'd like the response of an HTTP request to be. The actual response of the HTTP request is ignored - the response will be exactly what you want to be mocked!
Here's some ways this can be useful.
Not sure how your code handles an HTTP request that results in a Timeout? Test it by mocking a Timeout response!
import Http
import Http.Mock
type Msg
= MyResponseHandler (Result Http.Error String)
testTimeout =
Http.get
{ url = "https://fakeurl.com"
, expect = Http.Mock.expectString Http.Timeout_ MyResponseHandler
}Your update logic doesn't change - mock a Timeout response and make sure your application handles it correctly!
Notice that we put in a dummy URL here - It doesn't matter request you make, as the response of the request will be exactly what you specify.
Need to quickly mock an API locally? Don't waste time setting up a fake HTTP server for testing - just mock the response directly from within Elm!
import Http
import Http.Mock
type Msg
= MyResponseHandler (Result Http.Error String)
-- This is our mocked response.
-- You would actually put metadata and a body!
mockResponse =
Http.GoodStatus_ <metadata> <body>
sendRequestWithMockedResponse =
Http.get
{ url = "https://fakeurl.com"
, expect = Http.Mock.expectString mockResponse MyResponseHandler
}Again, your update logic should not change, and it doesn't matter what type of request you make - the response is discarded and replaced with the mocked response.
When using this module, it would be a good idea to store all your mock responses in a separate file!
The API is designed so that usage of this module is almost identical to using elm/http. Simply specify exactly what you want the response to be - everything else looks the same.
The Result is identical to the ones in elm/http. You can use the Transform functions in Detailed
if you'd like the detailed responses.