brian-watkins/elm-spec/2.2.0

NOTE: the latest version is 3.3.2

Spec.Http

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 201

Now, 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.jsonBody
              (Json.field "name" Json.string)
              (Spec.Claim.isEqual Debug.toString "bowling")
          ]
        )
    )
  )
]

Observe HTTP Requests

Represents an HTTP request made by the program in the course of the scenario.

observeRequests : HttpRoute -> Observer model (List HttpRequest)

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.isListWhere
    [ Spec.Http.header "Authorization" <|
      Spec.Claim.isSomethingWhere <|
      Spec.Claim.isStringContaining 1 "some-fun-token"
    ]
  )
clearRequestHistory : Context model -> Command msg

A step that clears the history of HTTP requests received.

Any HTTP requests made prior to executing this step will not be observed.

It can be useful to clear the HTTP request history when a scenario results in many HTTP requests, but you care about observing only those that occur after a certain point.

Make Claims About HTTP Requests

url : Claim String -> Claim HttpRequest

Claim that the url of an HTTP request satisfies the given claim.

In the example below, we claim that there is one GET request to a url containing fake.com and that url has the query parameter sport=bowling.

Spec.Http.observeRequests (Spec.Http.Route.route "GET" <| Matching "fake\\.com")
  |> Spec.expect (Spec.Claim.isListWhere
    [ Spec.Http.url <|
      Spec.Claim.isStringContaining 1 "sport=bowling"
    ]
  )

This claims makes the most sense when observing requests that match a route defined with a regular expression, as in the example above.

stringBody : Claim String -> Claim HttpRequest

Claim that the body of an HTTP request is a string that satisfies the given claim.

Note: If you create a request with Http.emptyBody then the given claim will be evaluated against the empty string.

jsonBody : Decoder a -> Claim a -> Claim HttpRequest

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 would be accepted:

Spec.Http.jsonBody
  (Json.Decode.field "sport" Json.Decode.string)
  (Spec.Claim.isEqualTo Debug.toString "bowling")

Debug

logRequests : Context model -> Command msg

A step that logs to the console any HTTP requests received prior to executing this step.

You might use this step to help debug a rejected observation.