helpers

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package helpers provides common test utilities for integration and unit tests. Following Go 2025 best practices, all helper functions use t.Helper() to ensure proper error reporting in test output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoRequest

func DoRequest(t *testing.T, baseURL string, opts RequestOptions) *http.Response

DoRequest creates and executes an HTTP request with common test settings. It automatically handles CSRF protection by setting the Origin header for state-changing requests (POST, PUT, DELETE, PATCH).

Usage:

resp := helpers.DoRequest(t, ts.Server.URL, helpers.RequestOptions{
    Method: http.MethodPost,
    Path: "/api/v1/refresh",
    Token: "test-token",
})
defer resp.Body.Close()

Types

type RequestOptions

type RequestOptions struct {
	Method      string
	Path        string
	Body        io.Reader
	Token       string
	SetOrigin   bool // Automatically set Origin header for CSRF protection
	ExtraHeader map[string]string
}

RequestOptions configures HTTP request creation

type TestServer

type TestServer struct {
	Server *httptest.Server
	Config jobs.Config
	API    *api.Server
}

TestServer wraps a test HTTP server with its configuration

func NewTestServer

func NewTestServer(t *testing.T, opts TestServerOptions) *TestServer

NewTestServer creates a new test HTTP server with the given configuration. It automatically marks the calling function as a test helper.

Usage:

ts := helpers.NewTestServer(t, helpers.TestServerOptions{
    DataDir: t.TempDir(),
    APIToken: "test-token",
})
defer ts.Close()

func NewTestServerWithConfig

func NewTestServerWithConfig(t *testing.T, cfg jobs.Config) *TestServer

NewTestServerWithConfig creates a test server from an existing config. Use this when you need full control over the config structure.

func (*TestServer) Close

func (ts *TestServer) Close()

Close closes the test server and cleans up resources

type TestServerOptions

type TestServerOptions struct {
	DataDir    string
	OWIBase    string
	StreamPort int
	APIToken   string
	Bouquet    string
}

TestServerOptions configures the test server setup

Jump to

Keyboard shortcuts

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