brian-watkins/elm-spec/2.1.0

NOTE: the latest version is 3.3.2

Spec.Http.Stub

Define and set up stubs for HTTP requests made during a spec.

Register Stubs

Represents the stubbed response for a particular HTTP request.

serve : List HttpResponseStub -> Setup model msg -> Setup model msg

Set up a fake HTTP server with the given HttpResponseStubs.

When a matching HTTP request is made, the relevant stubbed response will be returned.

nowServe : List HttpResponseStub -> Context model -> Command msg

Reconfigure the fake HTTP server to serve the given HttpResponseStubs.

Use this function if you want to change the stubs during a scenario.

For example, suppose you are writing a scenario that describes an application that polls some HTTP endpoint every 5 seconds. You could change the stubbed response during the scenario like so:

Spec.scenario "polling" (
  Spec.given (
    Spec.Setup.init (App.init testFlags)
      |> Spec.Setup.withView App.view
      |> Spec.Setup.withUpdate App.update
      |> Stub.serve [ pollFailsStub ]
  )
  |> when "time passes"
    [ Spec.Time.tick 5000
    , Spec.Time.tick 5000
    ]
  |> when "the poll succeeds"
    [ Spec.Http.Stub.nowServe [ pollSucceedsStub ]
    , Spec.Time.tick 5000
    ]
  |> it "does the right thing" (
    ...
  )
)

Note that Spec.Http.Stub.nowServe will clear any existing stubs and register only the ones provided.

Define Stubs

Build a stubbed response for requests made to the given route.

By default, the stubbed response has:

  • 200 status code
  • no headers
  • no body

Supply a body for the stubbed response.

Supply a status code for the stubbed response.

withHeader : ( String, String ) -> HttpResponseStub -> HttpResponseStub

Supple a header (key, value) for the stubbed response.

Set the stubbed response to trigger a network error.

Set the stubbed response to trigger a timeout error.

Abstain from responding to requests to the route defined for the given HttpResponseStub.

It can be useful to abstain from responding if you want to describe the behavior of your program while waiting for a response.