testutil
packageAPI reference for the testutil
package.
Imports
(8)TestHost
TestHost provides a DI container and test HTTP server.
type TestHost struct
Methods
Close shuts down the test server and runs cleanup functions.
func (*TestHost) Close()
{
h.Server.Close()
for _, f := range h.cleanup {
f()
}
}
URL returns the full URL for the given path.
Parameters
Returns
func (*TestHost) URL(path string) string
{
return h.Server.URL + path
}
Get issues a GET request and returns a TestResponse.
Parameters
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 issues a POST request and returns a TestResponse.
Parameters
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() |
NewTestHost
NewTestHost creates a TestHost with a DI container and test server.
Parameters
Returns
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(),
}
}
Resolve
Resolve retrieves a typed dependency from the container.
Parameters
Returns
func Resolve[T any](h *TestHost) T
{
return di.ResolveType[T](h.Container)
}
TestResponse
TestResponse holds an HTTP response for test assertions.
type TestResponse struct
Methods
Decode unmarshals the response body into v.
Parameters
Returns
func (*TestResponse) Decode(v any) error
{
return json.Unmarshal(r.Body, v)
}
String returns the response body as a string.
Returns
func (*TestResponse) String() string
{
return string(r.Body)
}
Fields
| Name | Type | Description |
|---|---|---|
| StatusCode | int | |
| Body | []byte | |
| Headers | http.Header | |
| Error | error |
FakeLogger
FakeLogger collects log entries for test assertions.
type FakeLogger struct
Methods
AssertLogged fails the test if msg was not logged.
Parameters
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 |
NewFakeLogger
NewFakeLogger creates a FakeLogger.
Returns
func NewFakeLogger() *FakeLogger
{
return &FakeLogger{}
}
TestContext
TestContext holds a context and variable map for test use.
type TestContext struct
Fields
| Name | Type | Description |
|---|---|---|
| Ctx | context.Context | |
| Vars | map[string]any |
NewTestContext
NewTestContext creates a TestContext with a background context.
Returns
func NewTestContext() *TestContext
{
return &TestContext{
Ctx: context.Background(),
Vars: make(map[string]any),
}
}