helpers

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package helpers provides test utilities for HTTP handler testing.

Package helpers provides test utilities including mock implementations.

Index

Constants

View Source
const TestJWTSecret = "test-jwt-secret-for-testing-only-32chars!"

TestJWTSecret is a secret used for testing JWT tokens.

View Source
const UserIDContextKey contextKey = "user_id"

Variables

This section is empty.

Functions

func AddUserIDToRequest

func AddUserIDToRequest(r *http.Request, userID string) *http.Request

AddUserIDToRequest adds user ID to the request context.

func GenerateTestEmail

func GenerateTestEmail() string

GenerateTestEmail generates a unique test email.

func GenerateTestID

func GenerateTestID(prefix string) string

GenerateTestID generates a unique test ID with a prefix.

func GenerateTestName

func GenerateTestName() string

GenerateTestName generates a unique test name.

func GenerateTestPhone

func GenerateTestPhone() string

GenerateTestPhone generates a unique test phone number.

func JSONBody

func JSONBody(pairs ...interface{}) map[string]interface{}

JSONBody is a helper to create a map[string]interface{} body.

func MockError

func MockError(code string, message string) *errors.Error

MockError creates a mock error for testing.

func TestMoney

func TestMoney(amount int64, currency string) sharedModels.Money

TestMoney creates a Money value for testing.

Types

type APIError

type APIError struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

APIError represents an error response.

type APIResponse

type APIResponse struct {
	Success bool            `json:"success"`
	Data    json.RawMessage `json:"data,omitempty"`
	Error   *APIError       `json:"error,omitempty"`
}

APIResponse is the standard API response structure.

func (*APIResponse) ParseData

func (resp *APIResponse) ParseData(t *testing.T, target interface{})

ParseData parses the Data field from an API response into the target struct.

type TestRequest

type TestRequest struct {
	Method      string
	Path        string
	Body        interface{}
	Headers     map[string]string
	QueryParams map[string]string
	AuthToken   string
}

TestRequest represents a test HTTP request configuration.

type TestResponse

type TestResponse struct {
	StatusCode int
	Body       []byte
	Headers    http.Header
}

TestResponse represents the parsed test HTTP response.

func MakeRequest

func MakeRequest(t *testing.T, handler http.Handler, req TestRequest) *TestResponse

MakeRequest creates and executes a test HTTP request against a handler.

func (*TestResponse) AssertError

func (r *TestResponse) AssertError(t *testing.T, expectedCode string)

AssertError asserts that the response indicates an error with the expected code.

func (*TestResponse) AssertHeader

func (r *TestResponse) AssertHeader(t *testing.T, key, expected string)

AssertHeader asserts a header value.

func (*TestResponse) AssertJSONContains

func (r *TestResponse) AssertJSONContains(t *testing.T, path string, expected interface{})

AssertJSONContains asserts that the response body contains a JSON field.

func (*TestResponse) AssertStatusCode

func (r *TestResponse) AssertStatusCode(t *testing.T, expected int)

AssertStatusCode asserts the expected HTTP status code.

func (*TestResponse) AssertSuccess

func (r *TestResponse) AssertSuccess(t *testing.T)

AssertSuccess asserts that the response indicates success.

func (*TestResponse) ParseAPIResponse

func (r *TestResponse) ParseAPIResponse(t *testing.T) *APIResponse

ParseAPIResponse parses the response as a standard API response.

func (*TestResponse) ParseJSON

func (r *TestResponse) ParseJSON(t *testing.T, target interface{})

ParseJSON parses the response body as JSON into the target struct.

type TestUserClaims

type TestUserClaims struct {
	UserID      string
	Email       string
	AccountType string
}

TestUserClaims represents claims for test JWT tokens.

Jump to

Keyboard shortcuts

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