brian-watkins/elm-spec/3.0.1

NOTE: the latest version is 3.3.2

Spec.Http.Stub

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

If an HTTP request matches none of the stubs currently served, then elm-spec will respond with a 404 status code.

Create and Register Stubs

Represents the stubbed response for a particular HTTP request.

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

By default, the stubbed response has:

  • 200 status code
  • no headers
  • no body
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 -> Step model msg

A step that reconfigures 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.

Stub the Response Body

Represents the body of an HTTP response.

Supply an HttpResponseBody for the stubbed response.

Create an HttpResponseBody composed of the given text.

Create an HttpResponseBody composed of the given JSON value, stringified.

Create an HttpResponseBody composed of the given bytes.

Create an HttpResponseBody composed of the text from the file at the given path.

The path is typically relative to the current working directory of the elm-spec runner (but check the docs for the runner you are using).

Create an HttpResponseBody composed of the bytes from the file at the given path.

The path is typically relative to the current working directory of the elm-spec runner (but check the docs for the runner you are using).

Stub Progress of an In-Flight Request

Represents progress on an in-flight HTTP request.

Specify the number of bytes that have been uploaded to the server so far.

Note that the total number of bytes is determined by the HTTP request body.

Specify the number of bytes received from the server so far.

Note that the total number of bytes is determined by the stubbed HTTP response body.

Specify the number of bytes received from the server so far, when the total content length is not known.

Set the progress on an in-flight HTTP request.

When the request is processed, an appropriate progress event will be triggered.

No further processing of this request will take place after the progress event is triggered. So, use this function to describe a program's behavior when an HTTP request is not yet complete.

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.

Note: This function is equivalent to withProgress <| received 0

Stub Response Metadata

Supply a status code for the stubbed response.

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

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

Stub Errors

Set the stubbed response to trigger a network error.

Set the stubbed response to trigger a timeout error.