Observe, and make claims about HTTP requests during a spec.
Here's an example:
First, create an HttpResponseStub that defines the response to return when a request is sent by the program.
successStub : HttpResponseStub
successStub =
Spec.Http.Stub.for (Spec.Http.Route.post "http://fake-api.com/api/sports")
|> Spec.Http.Stub.withStatus 201Now, you could write a spec that checks to see if the request body contains a value:
Spec.describe "some request"
[ Spec.scenario "the post is successful" (
Spec.given (
Spec.Setup.init (App.init testFlags)
|> Spec.Setup.withView App.view
|> Spec.Setup.withUpdate App.update
|> Spec.Http.Stub.serve [ successStub ]
)
|> Spec.when "the request is sent"
[ Spec.Markup.target << by [ id "request-button" ]
, Spec.Markup.Event.click
]
|> Spec.it "sends the correct request" (
Spec.Http.observeRequests
(Spec.Http.Route.post "http://fake-api.com/api/sports")
|> Spec.expect (Spec.Claim.isListWhere
[ Spec.Http.hasJsonBody
(Json.field "name" Json.string)
(Spec.Claim.isEqual Debug.toString "bowling")
]
)
)
)
]Represents the body of an HTTP request.
Observe HTTP requests that match the given route.
For example:
Spec.Http.observeRequests (Spec.Http.Route.get "http://fake.com/fake")
|> Spec.expect (Spec.Claim.isList
[ Spec.Http.hasHeader ("Authorization", "Bearer some-fun-token")
]
)Claim that an HTTP request has a header with the given (key, value) tuple.
For example, if the observed request has an Authorization header with the value
Bearer some-fun-token, then the following claim:
Spec.Http.hasHeader ("Authorization", "Bearer some-fun-token")would be accepted.
Claim that the body of an HTTP request is a string that is equal to the given value.
Claim that the body of an HTTP request is a string that can be decoded with the given decoder into a value that satisfies the given claim.
For example, if the body of the observed request was {"sport":"bowling"}, then the following claim:
Spec.Http.hasJsonBody
(Json.Decode.field "sport" Json.Decode.string)
(Spec.Claim.isEqualTo Debug.toString "bowling")would be accepted.
Represents an HTTP request made by the program in the course of the scenario.