testutil API

testutil

package

API reference for the testutil package.

S
struct

TestHost

TestHost provides a DI container and test HTTP server.

pkg/testutil/testutil.go:16-21
type TestHost struct

Methods

Close
Method

Close shuts down the test server and runs cleanup functions.

func (*TestHost) Close()
{
	h.Server.Close()
	for _, f := range h.cleanup {
		f()
	}
}
URL
Method

URL returns the full URL for the given path.

Parameters

path string

Returns

string
func (*TestHost) URL(path string) string
{
	return h.Server.URL + path
}
Get
Method

Get issues a GET request and returns a TestResponse.

Parameters

path string

Returns

func (*TestHost) Get(path string) *TestResponse
{
	resp, err := h.Client.Get(h.URL(path))
	if err != nil {
		return &TestResponse{Error: err}
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	return &TestResponse{
		StatusCode: resp.StatusCode,
		Body:       body,
		Headers:    resp.Header,
	}
}
Post
Method

Post issues a POST request and returns a TestResponse.

Parameters

path string
contentType string
body io.Reader

Returns

func (*TestHost) Post(path string, contentType string, body io.Reader) *TestResponse
{
	resp, err := h.Client.Post(h.URL(path), contentType, body)
	if err != nil {
		return &TestResponse{Error: err}
	}
	defer resp.Body.Close()
 responseBody, _ := io.ReadAll(resp.Body)
	return &TestResponse{
		StatusCode: resp.StatusCode,
		Body:       responseBody,
		Headers:    resp.Header,
	}
}

Fields

Name Type Description
Container *di.Container
Server *httptest.Server
Client *http.Client
cleanup []func()
F
function

NewTestHost

NewTestHost creates a TestHost with a DI container and test server.

Parameters

setup
func(b *di.Builder, app *srv.Server)

Returns

pkg/testutil/testutil.go:24-41
func NewTestHost(setup func(b *di.Builder, app *srv.Server)) *TestHost

{
	b := di.NewBuilder()
	app := srv.New()
	setup(b, app)

	container, err := b.Build()
	if err != nil {
		panic(err)
	}
	_ = err
	ts := httptest.NewServer(app)

	return &TestHost{
		Container: container,
		Server:    ts,
		Client:    ts.Client(),
	}
}
F
function

Resolve

Resolve retrieves a typed dependency from the container.

Parameters

Returns

T
pkg/testutil/testutil.go:87-89
func Resolve[T any](h *TestHost) T

{
	return di.ResolveType[T](h.Container)
}
S
struct

TestResponse

TestResponse holds an HTTP response for test assertions.

pkg/testutil/testutil.go:92-97
type TestResponse struct

Methods

Decode
Method

Decode unmarshals the response body into v.

Parameters

v any

Returns

error
func (*TestResponse) Decode(v any) error
{
	return json.Unmarshal(r.Body, v)
}
String
Method

String returns the response body as a string.

Returns

string
func (*TestResponse) String() string
{
	return string(r.Body)
}

Fields

Name Type Description
StatusCode int
Body []byte
Headers http.Header
Error error
S
struct

FakeLogger

FakeLogger collects log entries for test assertions.

pkg/testutil/testutil.go:110-112
type FakeLogger struct

Methods

AssertLogged
Method

AssertLogged fails the test if msg was not logged.

Parameters

msg string
func (*FakeLogger) AssertLogged(t *testing.T, msg string)
{
	t.Helper()
	found := false
	for _, e := range l.Entries {
		if e == msg {
			found = true
			break
		}
	}
	if !found {
		t.Errorf("expected log %q, not found", msg)
	}
}

Fields

Name Type Description
Entries []string
F
function

NewFakeLogger

NewFakeLogger creates a FakeLogger.

Returns

pkg/testutil/testutil.go:115-117
func NewFakeLogger() *FakeLogger

{
	return &FakeLogger{}
}
S
struct

TestContext

TestContext holds a context and variable map for test use.

pkg/testutil/testutil.go:135-138
type TestContext struct

Fields

Name Type Description
Ctx context.Context
Vars map[string]any
F
function

NewTestContext

NewTestContext creates a TestContext with a background context.

Returns

pkg/testutil/testutil.go:141-146
func NewTestContext() *TestContext

{
	return &TestContext{
		Ctx:  context.Background(),
		Vars: make(map[string]any),
	}
}