Documentation
¶
Overview ¶
Package helpers provides test utilities for HTTP handler testing.
Package helpers provides test utilities including mock implementations.
Index ¶
- Constants
- func AddUserIDToRequest(r *http.Request, userID string) *http.Request
- func GenerateTestEmail() string
- func GenerateTestID(prefix string) string
- func GenerateTestName() string
- func GenerateTestPhone() string
- func JSONBody(pairs ...interface{}) map[string]interface{}
- func MockError(code string, message string) *errors.Error
- func TestMoney(amount int64, currency string) sharedModels.Money
- type APIError
- type APIResponse
- type TestRequest
- type TestResponse
- func (r *TestResponse) AssertError(t *testing.T, expectedCode string)
- func (r *TestResponse) AssertHeader(t *testing.T, key, expected string)
- func (r *TestResponse) AssertJSONContains(t *testing.T, path string, expected interface{})
- func (r *TestResponse) AssertStatusCode(t *testing.T, expected int)
- func (r *TestResponse) AssertSuccess(t *testing.T)
- func (r *TestResponse) ParseAPIResponse(t *testing.T) *APIResponse
- func (r *TestResponse) ParseJSON(t *testing.T, target interface{})
- type TestUserClaims
Constants ¶
const TestJWTSecret = "test-jwt-secret-for-testing-only-32chars!"
TestJWTSecret is a secret used for testing JWT tokens.
const UserIDContextKey contextKey = "user_id"
Variables ¶
This section is empty.
Functions ¶
func AddUserIDToRequest ¶
AddUserIDToRequest adds user ID to the request context.
func GenerateTestEmail ¶
func GenerateTestEmail() string
GenerateTestEmail generates a unique test email.
func GenerateTestID ¶
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.
Types ¶
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 ¶
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 ¶
TestUserClaims represents claims for test JWT tokens.