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 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 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 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 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.