Documentation
¶
Overview ¶
Package stubs provides test stubs.
Index ¶
- Constants
- Variables
- func AsString[T any](v T) string
- func Host(t *testing.T, _url string) string
- func Port(t *testing.T, _url string) string
- func Replace(src string, replacements map[string]string) string
- func Sanitize(expected, actual string, replacements map[string]string) (string, string)
- func SanitizeSlice(expectedSlice, actualSlice []string, replacements map[string]string) ([]string, []string)
- type CapturedTable
- type HitCounts
- type StubFS
- func (s StubFS) Chmod(name string, mode os.FileMode) error
- func (s StubFS) Chown(name string, uid, gid int) error
- func (s StubFS) Chtimes(name string, atime time.Time, mtime time.Time) error
- func (s StubFS) Create(name string) (afero.File, error)
- func (s StubFS) Mkdir(name string, perm os.FileMode) error
- func (s StubFS) MkdirAll(path string, perm os.FileMode) error
- func (s StubFS) Name() string
- func (s StubFS) Open(name string) (afero.File, error)
- func (s StubFS) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error)
- func (s StubFS) Remove(name string) error
- func (s StubFS) RemoveAll(path string) error
- func (s StubFS) Rename(oldname, newname string) error
- func (s StubFS) Stat(name string) (os.FileInfo, error)
- type StubFile
- func (s StubFile) Close() error
- func (s StubFile) Name() string
- func (s StubFile) Read(p []byte) (int, error)
- func (s StubFile) ReadAt(p []byte, off int64) (int, error)
- func (s StubFile) Readdir(count int) ([]os.FileInfo, error)
- func (s StubFile) Readdirnames(n int) ([]string, error)
- func (s StubFile) Seek(offset int64, whence int) (int64, error)
- func (s StubFile) Stat() (os.FileInfo, error)
- func (s StubFile) Sync() error
- func (s StubFile) Truncate(size int64) error
- func (s StubFile) Write(p []byte) (int, error)
- func (s StubFile) WriteAt(p []byte, off int64) (int, error)
- func (s StubFile) WriteString(st string) (int, error)
- type StubHTTPBodyOption
- type StubHTTPEndpoint
- func GetStubApplicationsEndpoints(config StubHTTPPartialGetConfig) []StubHTTPEndpoint
- func GetStubAuthEndpoints() []StubHTTPEndpoint
- func GetStubDataplaneEndpoints(config StubHTTPPartialGetConfig) []StubHTTPEndpoint
- func GetStubNodepoolsEndpoints(config StubHTTPPartialGetConfig) []StubHTTPEndpoint
- func GetStubServicesEndpoints(config StubHTTPPartialGetConfig) []StubHTTPEndpoint
- func GetStubWorkspaceEndpoints(config StubHTTPPartialGetConfig) []StubHTTPEndpoint
- type StubHTTPEndpoints
- type StubHTTPPartialGetConfig
- type StubHTTPPartialPostConfig
- type StubHTTPRequest
- type StubHTTPRequests
- type StubHTTPResponse
- type StubHTTPResponses
- type StubUI
- func (u *StubUI) Ask(question string, args ...interface{}) string
- func (u *StubUI) AskForPassword(question string, args ...interface{}) string
- func (u *StubUI) Display(message string, args ...interface{})
- func (u *StubUI) Error(message string, args ...interface{})
- func (u *StubUI) Table(headers []string) ui.Table
- func (u *StubUI) Warn(message string, args ...interface{})
Constants ¶
const ExpectedDeviceAuthDisplay = "Open this URL in your browser," +
" complete the authorization, and verify the code 'user-code':\n" +
"${URL}/authorization?client_id=discovered_spa_client_id&organization=org-1&" +
"redirect_uri=verification_uri_complete&response_type=code&scope=openid+profile+offline_access"
ExpectedDeviceAuthDisplay is the expected display when using the stub auth endpoint.
Variables ¶
var ( // StubAuthHeaders represents the headers expected from test clients when // making requests to Workbench API endpoints. StubAuthHeaders = map[string]string{ constants.AuthorizationHeaderKey: "Bearer access-token", constants.PostgrestCSRFTokenHeaderKey: "csrf-token", constants.CookieHeaderKey: constants.PostgrestCSRFTokenKey + "=csrf-token;", } )
Functions ¶
func Replace ¶
Replace returns a copy fo the given src string with all instances of keys in the given replacements map with the corresponding values.
func Sanitize ¶
Sanitize is a testing utility function. It takes an expected string and actual string and a list of name/value pairs. First, any occurrences of {SANITIZE:regexp:replacement} are found in the expected string. Any matches of the regexp in the actual string are replaced with the replacement value. Then the entire {SANITIZE:regexp:replacement} string in the expected string is replaced with the replacement value. Lastly, any occurrences of the keys of the replacements map are replaced in the expected string with the corresponding values. See TestExampleSanitizeUsage for an example.
func SanitizeSlice ¶
func SanitizeSlice(expectedSlice, actualSlice []string, replacements map[string]string) ([]string, []string)
SanitizeSlice calls Sanitize on each pair of strings in the expectedSlice and actualSlice slices. If there are a different number of elements in each slice then they are returned unchanged.
Types ¶
type CapturedTable ¶
CapturedTable records all table interactions for assertions in tests.
func (*CapturedTable) Add ¶
func (t *CapturedTable) Add(values ...string)
Add appends a row to the captured table.
func (*CapturedTable) String ¶
func (t *CapturedTable) String() string
String is an exported method.
type HitCounts ¶
HitCounts maps an endpoint key to the number of times it was invoked in tests.
func NewStubHTTPServer ¶
func NewStubHTTPServer( t *testing.T, endpoints []StubHTTPEndpoint, ) (*httptest.Server, *HitCounts)
NewStubHTTPServer returns a new httptest.Server configured with the given endpoints.
func NewStubHTTPServerWithMultipleResponsesPerEndpoint ¶
func NewStubHTTPServerWithMultipleResponsesPerEndpoint( t *testing.T, endpoints []StubHTTPEndpoints, ) (*httptest.Server, *HitCounts)
NewStubHTTPServerWithMultipleResponsesPerEndpoint returns a new httptest.Server configured with the given endpoints. nolint:cyclop
func (*HitCounts) CountPrefix ¶
CountPrefix returns the total hits for keys that begin with the provided prefix.
type StubFS ¶
type StubFS struct {
afero.Fs
CreateFunc func(afero.Fs, string) (afero.File, error)
MkdirFunc func(afero.Fs, string, os.FileMode) error
MkdirAllFunc func(afero.Fs, string, os.FileMode) error
OpenFunc func(afero.Fs, string) (afero.File, error)
OpenFileFunc func(afero.Fs, string, int, os.FileMode) (afero.File, error)
RemoveFunc func(afero.Fs, string) error
RemoveAllFunc func(afero.Fs, string) error
RenameFunc func(afero.Fs, string, string) error
StatFunc func(afero.Fs, string) (os.FileInfo, error)
NameFunc func(afero.Fs) string
ChmodFunc func(afero.Fs, string, os.FileMode) error
ChownFunc func(afero.Fs, string, int, int) error
ChtimesFunc func(afero.Fs, string, time.Time, time.Time) error
}
StubFS is a test filesystem that delegates to injected functions when set.
func (StubFS) MkdirAll ¶
MkdirAll creates a directory tree, using the injected function when present.
func (StubFS) OpenFile ¶
OpenFile opens a file with flags, using the injected function when present.
type StubFile ¶
type StubFile struct {
afero.File
CloseFunc func(afero.File) error
ReadFunc func(afero.File, []byte) (int, error)
ReadAtFunc func(afero.File, []byte, int64) (int, error)
SeekFunc func(afero.File, int64, int) (int64, error)
WriteFunc func(afero.File, []byte) (int, error)
WriteAtFunc func(afero.File, []byte, int64) (int, error)
NameFunc func(afero.File) string
ReaddirFunc func(afero.File, int) ([]os.FileInfo, error)
ReaddirnamesFunc func(afero.File, int) ([]string, error)
StatFunc func(afero.File) (os.FileInfo, error)
SyncFunc func(afero.File) error
TruncateFunc func(afero.File, int64) error
WriteStringFunc func(afero.File, string) (int, error)
}
StubFile is a test file that delegates to injected functions when set.
func (StubFile) Readdir ¶
Readdir reads directory entries, using the injected function when present.
func (StubFile) Readdirnames ¶
Readdirnames reads directory entry names, using the injected function when present.
type StubHTTPBodyOption ¶
StubHTTPBodyOption mutates or formats a response body before it is written. Receives the request and the current body and must return the body to send.
func StubHTTPBodyReplaceHOST ¶
func StubHTTPBodyReplaceHOST() StubHTTPBodyOption
StubHTTPBodyReplaceHOST returns a StubHTTPBodyOption that replaces ${HOST} placeholders in the body with the request Host value.
type StubHTTPEndpoint ¶
type StubHTTPEndpoint struct {
Request StubHTTPRequest
Response StubHTTPResponse
}
StubHTTPEndpoint pairs a single expected request with the response to return.
func GetStubApplicationsEndpoints ¶
func GetStubApplicationsEndpoints(config StubHTTPPartialGetConfig) []StubHTTPEndpoint
GetStubApplicationsEndpoints returns an application endpoint. The first request/response is configured with the given config.
func GetStubAuthEndpoints ¶
func GetStubAuthEndpoints() []StubHTTPEndpoint
GetStubAuthEndpoints returns default endpoints that mock an authentication server. Used when the auth server endpoints themselves are not under test.
func GetStubDataplaneEndpoints ¶
func GetStubDataplaneEndpoints(config StubHTTPPartialGetConfig) []StubHTTPEndpoint
GetStubDataplaneEndpoints returns a dataplane endpoint. The first request/response is configured with the given config.
func GetStubNodepoolsEndpoints ¶
func GetStubNodepoolsEndpoints(config StubHTTPPartialGetConfig) []StubHTTPEndpoint
GetStubNodepoolsEndpoints returns a nodepools endpoint. The first request/response is configured with the given config.
func GetStubServicesEndpoints ¶
func GetStubServicesEndpoints(config StubHTTPPartialGetConfig) []StubHTTPEndpoint
GetStubServicesEndpoints returns a services endpoint. The first request/response is configured with the given config.
func GetStubWorkspaceEndpoints ¶
func GetStubWorkspaceEndpoints(config StubHTTPPartialGetConfig) []StubHTTPEndpoint
GetStubWorkspaceEndpoints returns endpoints that represent workspaces. If the provided config is empty then two workspaces are represented, one for a home dir and one for a regular dir. The home dir must be requested first.
type StubHTTPEndpoints ¶
type StubHTTPEndpoints struct {
Request StubHTTPRequests
Response StubHTTPResponses
}
StubHTTPEndpoints groups request expectations and response configurations for a single route.
type StubHTTPPartialGetConfig ¶
type StubHTTPPartialGetConfig struct {
ExpectedQuery string
ResponseStatus int
ResponseBody string
}
StubHTTPPartialGetConfig represents a single get request and response that does not require additional config like headers.
type StubHTTPPartialPostConfig ¶
type StubHTTPPartialPostConfig struct {
ExpectedRequestBody string
ResponseStatus int
ResponseBody string
}
StubHTTPPartialPostConfig represents a single post request and response that does not require additional config like headers.
type StubHTTPRequest ¶
type StubHTTPRequest struct {
Method string
URL string
ExpectedQuery string
ExpectedHeaders map[string]string
ExpectedBody string
}
StubHTTPRequest contains expectations for a single HTTP request in the stub harness.
type StubHTTPRequests ¶
type StubHTTPRequests struct {
Method string
URL string
ExpectedQueries []string
ExpectedHeaders []map[string]string
ExpectedRequestBodies []string
}
StubHTTPRequests describes expectations for incoming HTTP requests in the test stub.
type StubHTTPResponse ¶
type StubHTTPResponse struct {
Body string
Headers map[string][]string
Status int
BodyOptions []StubHTTPBodyOption
}
StubHTTPResponse describes a single HTTP response returned by a stub endpoint.
type StubHTTPResponses ¶
type StubHTTPResponses struct {
Bodies []string
Headers []map[string][]string
Statuses []int
BodyOptions [][]StubHTTPBodyOption
}
StubHTTPResponses configures the responses returned by a stub endpoint across multiple calls.
type StubUI ¶
type StubUI struct {
Displays []string
Warns []string
Errors []string
// Recorded questions
Asks []string
AskForPasswords []string
// Configured responses
AskResponses []string
AskForPasswordResponses []string
// Captured tables, one entry per Table(...) call.
Tables []*CapturedTable
}
StubUI is a test UI implementation that records display and warning messages. Configure AskResponses and AskForPasswordResponses with queued return values for Ask calls (FIFO; empty queue returns "").
func (*StubUI) AskForPassword ¶
AskForPassword records a password prompt and returns the next queued response.