Skip to content

HTTP ​

mockhttp runs HTTP mock servers for tests, and is the server the vlatte command runs.

go
func TestHello(t *testing.T) {
	m := mockhttp.MustNew()
	m.MustStart()
	t.Cleanup(m.Close)

	scoped := m.MustMock(mockhttp.Getf("/hello").Reply(mockhttp.OK().PlainText("hello world")))

	res, err := http.Get(m.URL("/hello"))
	if err != nil {
		t.Fatal(err)
	}
	defer res.Body.Close()

	body, err := io.ReadAll(res.Body)
	if err != nil {
		t.Fatal(err)
	}
	if string(body) != "hello world" {
		t.Errorf("body = %q", body)
	}

	scoped.AssertCalled(t)
}

mockhttp.New does the same as MustNew but returns an error instead of panicking. Pass mockhttp.Setup() to either one to configure the server: its address, TLS, CORS, proxying or recording, for example.

In this section ​

What HTTP shares with gRPC, such as matchers, scenarios, templates, webhooks and the journal, works the same for both: each has a section of its own in the sidebar.