Documentation
¶
Overview ¶
Package testutil provides shared test utilities for MCP tool tests. It includes a test GitLab client factory, JSON response helpers, and pagination header utilities used across all domain tool test files. It also includes helpers for GraphQL test responses and assertions for embedded MCP resource content.
HTTP Test Pattern ¶
Most tool tests create an httptest server with package-specific routing, build a GitLab client with NewTestClient, then call the handler directly. Response helpers keep JSON, pagination headers, and GraphQL envelopes consistent across domain packages.
Index ¶
- Constants
- func AssertEmbeddedResource(t *testing.T, ctx context.Context, session *mcp.ClientSession, name string, ...)
- func AssertQueryParam(t *testing.T, r *http.Request, key, expected string)
- func AssertRequestMethod(t *testing.T, r *http.Request, expected string)
- func AssertRequestPath(t *testing.T, r *http.Request, expected string)
- func CancelledCtx(t *testing.T) context.Context
- func CaptureSlog(t *testing.T) *bytes.Buffer
- func GraphQLHandler(handlers map[string]http.HandlerFunc) http.Handler
- func NewTestClient(t *testing.T, handler http.Handler) *gitlabclient.Client
- func ParseGraphQLVariables(r *http.Request) (map[string]any, error)
- func RespondGraphQL(w http.ResponseWriter, status int, data string)
- func RespondGraphQLError(w http.ResponseWriter, status int, message string)
- func RespondJSON(w http.ResponseWriter, status int, body string)
- func RespondJSONWithPagination(w http.ResponseWriter, status int, body string, p PaginationHeaders)
- type EmbedToggle
- type PaginationHeaders
Constants ¶
const MsgErrEmptyProjectID = "expected error for empty project_id, got nil"
MsgErrEmptyProjectID is the shared assertion message for tests that expect an error when project_id is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertEmbeddedResource ¶
func AssertEmbeddedResource(t *testing.T, ctx context.Context, session *mcp.ClientSession, name string, args map[string]any, wantURI string, toggle EmbedToggle)
AssertEmbeddedResource invokes the named tool with args twice: first with the embed toggle enabled (expecting an *mcp.EmbeddedResource block whose URI matches wantURI and MIME type is application/json), then with the toggle disabled (expecting no EmbeddedResource blocks). The toggle is always restored to enabled (the production default) on test exit.
func AssertQueryParam ¶
AssertQueryParam fails the test if query parameter key does not equal expected.
func AssertRequestMethod ¶
AssertRequestMethod fails the test if the HTTP method does not match expected.
func AssertRequestPath ¶
AssertRequestPath fails the test if the URL path does not match expected.
func CancelledCtx ¶
CancelledCtx returns a pre-cancelled context for testing cancellation handling.
func CaptureSlog ¶
CaptureSlog redirects slog output to a buffer for the duration of the test. The original default logger is restored via t.Cleanup. NOT safe for t.Parallel — acquires captureSlogMu for the test lifetime.
func GraphQLHandler ¶
func GraphQLHandler(handlers map[string]http.HandlerFunc) http.Handler
GraphQLHandler creates an http.Handler that routes GraphQL POST requests by matching the query body against handler keys. It reads the request body, checks if the query contains each key string, and dispatches to the first matching handler.
Keys should be GraphQL operation identifiers (type names, field names, or mutation names) that uniquely identify the query. For example:
testutil.GraphQLHandler(map[string]http.HandlerFunc{
"vulnerabilities": handleListVulnerabilities,
"vulnerabilityDismiss": handleDismissVulnerability,
})
If no handler matches, it responds with 400 Bad Request.
func NewTestClient ¶
NewTestClient creates a GitLab client pointed at a test HTTP server.
func ParseGraphQLVariables ¶
ParseGraphQLVariables reads the request body and returns the Variables map from the GraphQL request. Useful for asserting input parameters in test handlers.
func RespondGraphQL ¶
func RespondGraphQL(w http.ResponseWriter, status int, data string)
RespondGraphQL writes a GraphQL JSON envelope response with the given data payload. It wraps the data in {"data": ...} as expected by the GitLab GraphQL API client.
testutil.RespondGraphQL(w, http.StatusOK, `{"project":{"name":"foo"}}`)
produces: {"data":{"project":{"name":"foo"}}}
func RespondGraphQLError ¶
func RespondGraphQLError(w http.ResponseWriter, status int, message string)
RespondGraphQLError writes a GraphQL error response with the given message.
testutil.RespondGraphQLError(w, http.StatusOK, "not found")
produces: {"data":null,"errors":[{"message":"not found"}]}
func RespondJSON ¶
func RespondJSON(w http.ResponseWriter, status int, body string)
RespondJSON writes a JSON response with the given status code and body.
func RespondJSONWithPagination ¶
func RespondJSONWithPagination(w http.ResponseWriter, status int, body string, p PaginationHeaders)
RespondJSONWithPagination writes a JSON response with GitLab pagination headers.
Types ¶
type EmbedToggle ¶
type EmbedToggle func(bool)
EmbedToggle is the signature of the toolutil.EnableEmbeddedResources setter, declared here so testutil can drive the toggle without importing toolutil (which would create an import cycle through other packages).