Skip to content

http.mock

Mock HTTP server, for testing code that makes requests through the http module.

A Server binds a random local port; registering a mock on it makes one matcher/response pair live, and the request under test goes over a real socket to it.

http.mock.Server do |server|
  server.mock
    - method: GET
      path: /users/42
      respond:
        status: 200
        json: {id: 42, name: "Alice"}
    do |handle|
      let resp = http.get (server.url / "/users/42")
      assert_eq $resp.json()["name"] "Alice"

Matchers and responses can also be computed from the request itself:

server.mock
  - path: /echo
    respond: do |req| $
      status: 200
      body: $req.body

Types

TypeDescription
Mock A handle to the mocks one Server.mock call registered.
Request A request the mock server received.
Server A running mock HTTP server, bound to a random local port.