testutil

package
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 13 Imported by: 0

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

View Source
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

func AssertQueryParam(t *testing.T, r *http.Request, key, expected string)

AssertQueryParam fails the test if query parameter key does not equal expected.

func AssertRequestMethod

func AssertRequestMethod(t *testing.T, r *http.Request, expected string)

AssertRequestMethod fails the test if the HTTP method does not match expected.

func AssertRequestPath

func AssertRequestPath(t *testing.T, r *http.Request, expected string)

AssertRequestPath fails the test if the URL path does not match expected.

func CancelledCtx added in v1.0.3

func CancelledCtx(t *testing.T) context.Context

CancelledCtx returns a pre-cancelled context for testing cancellation handling.

func CaptureSlog added in v1.0.3

func CaptureSlog(t *testing.T) *bytes.Buffer

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

func NewTestClient(t *testing.T, handler http.Handler) *gitlabclient.Client

NewTestClient creates a GitLab client pointed at a test HTTP server.

func ParseGraphQLVariables

func ParseGraphQLVariables(r *http.Request) (map[string]any, error)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL