Search results

There are no results.

std.net.http.test

Testing of HTTP clients and servers.

This module provides types for testing HTTP clients and HTTP servers implemented using the types of the std.net.http.server module.

Testing HTTP clients

Testing an HTTP client is done by creating a Server, defining the expected requests and their responses, configuring a Client to redirect its requests to the mock server, and finally setting expectations on the results using the usual testing methods.

The following is an example of a mock server that defines an expected GET request to / that responds with the body "hello":

import std.net.http (Status)
import std.net.http.client (Client)
import std.net.http.server (Response)
import std.net.http.test (Server)
import std.test (Tests)
import std.uri (Uri)

type async Main {
  fn async main {
    let tests = Tests.new

    tests.test('Example test', fn (t) {
      let server = Server.new(t, fn (srv) {
        srv.get('/').then(fn { Response.new.string('hello') })
      })

      let client = Client.new

      server.prepare_client(client)

      let uri = Uri.parse('http://example.com').or_panic
      let resp = client.get(uri).send.or_panic
      let body = ByteArray.new
      let _ = resp.body.read_all(body).or_panic

      t.equal(resp.status, Status.ok)
      t.equal(body.to_string, 'hello')
    })

    tests.run
  }
}

An important detail here is the expression server.prepare_client(client). This ensures the Client "redirects" its requests to the mock server instead of whatever server would normally receive the requests.

When a Server is dropped it automatically verifies that all expected requests have been received with the right data (e.g. HTTP headers), and that no unexpected requests have been received.

For more information, refer to the documentation of the various methods of the Server and Mock types.

Testing HTTP servers

Testing HTTP servers is done by using the RequestBuilder type to create and send a request to a type that implements std.net.http.server.Handle. For example:

import std.net.http (Status)
import std.net.http.server (Handle, Request, Response)
import std.net.http.test (RequestBuilder)
import std.test (Tests)

type Handler {}

impl Handle for Handler {
  fn pub mut handle(request: mut Request) -> Response {
    Response.new.string('hello')
  }
}

type async Main {
  fn async main {
    let tests = Tests.new

    tests.test('Example test', fn (t) {
      let handler = Handler()
      let resp = RequestBuilder.get('/').send(handler)
      let body = ByteArray.new

      t.equal(resp.status, Status.ok)
      t.true(resp.body.reader.read_all(body).ok?)
      t.equal(body.to_string, 'hello')
    })

    tests.run
  }
}

Types

Mock

A type for defining an expected request and its response.

RequestBuilder

A type for building a std.net.http.server.Request to use for testing an HTTP server.

RunningServer

A handle to a running mock HTTP server.

Server

An HTTP server for generating mock responses and recording unmocked requests.