Documentation
¶
Index ¶
- Constants
- func CallEndpoint(method string, f func(w http.ResponseWriter, r *http.Request), ...) (body string, response *http.Response, err error)
- func CallMiddleware(method string, middleware func(next http.Handler) http.Handler, ...) (body string, response *http.Response, err error)
- func CallStringEndpoint(method string, f func(w http.ResponseWriter, r *http.Request) string, ...) (body string, response *http.Response, err error)
- func CleanupTestEnvironment(config *TestConfig)
- func CloseTestDB(db *sql.DB) error
- func CreateTestTable(db *sql.DB, tableName string, schema string) error
- func DropTestTable(db *sql.DB, tableName string) error
- func ExecuteSQL(db *sql.DB, sql string) error
- func ExecuteSQLWithArgs(db *sql.DB, sql string, args ...interface{}) error
- func NewRequest(method string, url string, opts NewRequestOptions) (*http.Request, error)
- func NewTestDB(config *DBConfig) (*sql.DB, error)
- func SetupTestEnvironment(config *TestConfig)
- func StringResponse(w http.ResponseWriter, r *http.Request, body string)
- func TestKey(dbDriver, dbHost, dbPort, dbName, dbUser, dbPass string) string
- type DBConfig
- type NewRequestOptions
- type StringHandler
- type TestConfig
- type TestHTTPRequest
- func (r *TestHTTPRequest) Execute(handler http.Handler) *httptest.ResponseRecorder
- func (r *TestHTTPRequest) WithBody(body string) *TestHTTPRequest
- func (r *TestHTTPRequest) WithFormBody(formBody string) *TestHTTPRequest
- func (r *TestHTTPRequest) WithHeader(key, value string) *TestHTTPRequest
- func (r *TestHTTPRequest) WithJSONBody(jsonBody string) *TestHTTPRequest
- type TestHTTPServer
- func (s *TestHTTPServer) Close()
- func (s *TestHTTPServer) Do(req *http.Request) (*http.Response, error)
- func (s *TestHTTPServer) Get(path string) (*http.Response, error)
- func (s *TestHTTPServer) Post(path string, contentType string, body io.Reader) (*http.Response, error)
- func (s *TestHTTPServer) URL() string
Constants ¶
const ( EnvDevelopment = "development" EnvLocal = "local" EnvProduction = "production" EnvStaging = "staging" EnvTesting = "testing" )
Environment constants
Variables ¶
This section is empty.
Functions ¶
func CallEndpoint ¶
func CallEndpoint(method string, f func(w http.ResponseWriter, r *http.Request), options NewRequestOptions) (body string, response *http.Response, err error)
func CallMiddleware ¶
func CallStringEndpoint ¶
func CleanupTestEnvironment ¶
func CleanupTestEnvironment(config *TestConfig)
CleanupTestEnvironment unsets all environment variables set by SetupTestEnvironment
func CloseTestDB ¶
CloseTestDB safely closes a test database connection
func CreateTestTable ¶
CreateTestTable creates a test table in the database
func DropTestTable ¶
DropTestTable drops a test table from the database
func ExecuteSQL ¶
ExecuteSQL executes a SQL statement on the database
func ExecuteSQLWithArgs ¶
ExecuteSQLWithArgs executes a SQL statement with arguments on the database
func NewRequest ¶
NewRequest creates a new Request for testing, but adds RequestURI as the default imlemented in GoLang does not add the RequestURI and leaves it to the end user to implement
func NewTestDB ¶
NewTestDB creates a new test database connection By default, it creates an in-memory SQLite database
func SetupTestEnvironment ¶
func SetupTestEnvironment(config *TestConfig)
SetupTestEnvironment configures the environment variables for testing based on the provided configuration
func StringResponse ¶
func StringResponse(w http.ResponseWriter, r *http.Request, body string)
StringResponse - responds with the string body
Types ¶
type DBConfig ¶
type DBConfig struct {
Driver string
Host string
Port string
Database string
Username string
Password string
}
DBConfig contains configuration for test database
func DefaultDBConfig ¶
func DefaultDBConfig() *DBConfig
DefaultDBConfig returns a default SQLite in-memory database configuration. To use the default SQLite driver, ensure you import a compatible driver package (for example, via a blank import in your test setup) before calling NewTestDB.
type NewRequestOptions ¶
type NewRequestOptions struct {
// Body use this to set the request body
Body string
// Context allows setting the request context.
Context map[any]any
// ContentType allows setting the Content-Type header
ContentType string
// Headers allows setting the request headers
Headers map[string]string
// GetValues use this to set the GET values, it will be converted to url.Values
// and set as the request query
// Deprecated: use QueryValues
GetValues urlpkg.Values
// PostValues use this to set the POST values, it will be converted to url.Values
// and set as the request body, with Content-Type: application/x-www-form-urlencoded
// Deprecated: use FormValues
PostValues urlpkg.Values
// JSONData sets the request body as application/json.
// If set, Body and FormValues will be ignored.
JSONData any
// QueryParams sets the URL query parameters.
QueryParams urlpkg.Values
// FormValues sets the request body as application/x-www-form-urlencoded.
// If set, Body and JSONData will be ignored.
FormValues urlpkg.Values
}
NewRequest options for the new request
type StringHandler ¶
type StringHandler func(w http.ResponseWriter, r *http.Request) string
func (StringHandler) ServeHTTP ¶
func (h StringHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type TestConfig ¶
type TestConfig struct {
// Application settings
AppName string
AppURL string
AppEnv string
// Database settings
DbDriver string
DbHost string
DbPort string
DbDatabase string
DbUsername string
DbPassword string
// Server settings
ServerHost string
ServerPort string
// Mail settings
MailDriver string
MailHost string
MailPort string
MailUsername string
MailPassword string
EmailFrom string
EmailName string
// Security settings
EnvEncryptionKey string
VaultKey string
// Additional settings can be added as needed
AdditionalEnvVars map[string]string
}
TestConfig contains configuration options for setting up a test environment
func DefaultTestConfig ¶
func DefaultTestConfig() *TestConfig
DefaultTestConfig returns a default test configuration suitable for most test cases
type TestHTTPRequest ¶
TestHTTPRequest represents a test HTTP request
func NewTestHTTPRequest ¶
func NewTestHTTPRequest(method, path string) *TestHTTPRequest
NewTestHTTPRequest creates a new test HTTP request
func (*TestHTTPRequest) Execute ¶
func (r *TestHTTPRequest) Execute(handler http.Handler) *httptest.ResponseRecorder
Execute executes the request against the provided handler
func (*TestHTTPRequest) WithBody ¶
func (r *TestHTTPRequest) WithBody(body string) *TestHTTPRequest
WithBody sets the request body
func (*TestHTTPRequest) WithFormBody ¶
func (r *TestHTTPRequest) WithFormBody(formBody string) *TestHTTPRequest
WithFormBody sets the request body as form data and adds the appropriate content type header
func (*TestHTTPRequest) WithHeader ¶
func (r *TestHTTPRequest) WithHeader(key, value string) *TestHTTPRequest
WithHeader adds a header to the request
func (*TestHTTPRequest) WithJSONBody ¶
func (r *TestHTTPRequest) WithJSONBody(jsonBody string) *TestHTTPRequest
WithJSONBody sets the request body as JSON and adds the appropriate content type header
type TestHTTPServer ¶
TestHTTPServer is a wrapper around httptest.Server for testing HTTP servers
func NewTestHTTPServer ¶
func NewTestHTTPServer(handler http.Handler) *TestHTTPServer
NewTestHTTPServer creates a new test HTTP server with the provided handler
func (*TestHTTPServer) Get ¶
func (s *TestHTTPServer) Get(path string) (*http.Response, error)
Get performs a GET request to the test server
func (*TestHTTPServer) Post ¶
func (s *TestHTTPServer) Post(path string, contentType string, body io.Reader) (*http.Response, error)
Post performs a POST request to the test server
func (*TestHTTPServer) URL ¶
func (s *TestHTTPServer) URL() string
URL returns the base URL of the test server