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.
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 NewEmbedTestSession(t *testing.T, handler http.Handler, register RegisterFn) (*mcp.ClientSession, context.Context)
- 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
- type RegisterFn
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 ¶ added in v1.3.2
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 ¶ added in v1.0.3
CancelledCtx returns a pre-cancelled context for testing cancellation handling.
func CaptureSlog ¶ added in v1.0.3
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 NewEmbedTestSession ¶ added in v1.3.2
func NewEmbedTestSession(t *testing.T, handler http.Handler, register RegisterFn) (*mcp.ClientSession, context.Context)
NewEmbedTestSession bootstraps an in-memory MCP session for embed-resource integration tests. It builds a mock GitLab client backed by handler, instantiates an MCP server, invokes register to wire the package's tools, connects an in-memory client, and returns the live session. The session is closed via t.Cleanup so callers do not need to track it.
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 ¶ added in v1.3.2
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).
type PaginationHeaders ¶
type PaginationHeaders struct {
Page string
PerPage string
Total string
TotalPages string
NextPage string
PrevPage string
}
PaginationHeaders holds GitLab pagination header values for test mocks.
type RegisterFn ¶ added in v1.3.2
type RegisterFn func(server *mcp.Server, client *gitlabclient.Client)
RegisterFn is the per-package tool registration callback. Every domain sub-package exposes a RegisterTools(server, client) function with this shape; tests pass it to NewEmbedTestSession so the helper does not have to import any specific tool sub-package.