Documentation ¶
Overview ¶
Package testkit provides all sorts of test helpers.
Index ¶
- Variables
- func AbsPath(t T, pth string) string
- func AssertErrPrefix(t T, err error, prefix string)
- func CloneHTTPRequest(t T, req *http.Request) *http.Request
- func CreateDir(t T, name string) string
- func CreateFile(t T, name string) *os.File
- func ErrReader(r io.Reader, n int, err error) io.Reader
- func ErrWriter(w io.Writer, n int, err error) io.Writer
- func FileSize(t T, fil *os.File) int64
- func FromJSON(t T, data []byte, v interface{})
- func FromJSONReader(t T, r io.Reader, v interface{})
- func Getwd(t T) string
- func HasNetConn() bool
- func ModTime(t T, name string) time.Time
- func NoNetworkSkip(t T)
- func OpenFile(t T, name string) *os.File
- func RandFileName(dir, prefix, ext string) string
- func RandStr(n int) string
- func ReadAll(t T, r io.Reader) []byte
- func ReadAllFromStart(t T, rs io.ReadSeeker) []byte
- func ReadAllString(t T, r io.Reader) string
- func ReadFile(t T, filename string) []byte
- func Readdirnames(t T, fil *os.File) []string
- func ReplaceAllInFile(t T, filename, old, new string)
- func TempDir(t T, dir string, prefix string) string
- func TempFile(t T, dir, pattern string) *os.File
- func ToJSON(t T, v interface{}) []byte
- func ToJSONReader(t T, v interface{}) io.Reader
- func Wait(max time.Duration, fn func() bool)
- func WaitTh(max, th time.Duration, fn func() bool)
- func WriteTempFile(t T, dir string, r io.Reader) string
- type HTTPServer
- func (tst *HTTPServer) Body(n int) []byte
- func (tst *HTTPServer) Close() error
- func (tst *HTTPServer) Headers(n int) http.Header
- func (tst *HTTPServer) ReqCount() int
- func (tst *HTTPServer) Request(n int) *http.Request
- func (tst *HTTPServer) Rsp(status int, rsp []byte) *HTTPServer
- func (tst *HTTPServer) URL() string
- func (tst *HTTPServer) Values(n int) url.Values
- type T
Constants ¶
This section is empty.
Variables ¶
var ErrTestError = errors.New("testkit test error")
ErrTestError is general error used in tests,it is returned by some helpers.
Functions ¶
func AssertErrPrefix ¶
AssertErrPrefix asserts error message has prefix.
func CloneHTTPRequest ¶
CloneHTTPRequest clones HTTP request with body, URL.Host and URL.Scheme. Calls t.Fatal() on error.
func CreateDir ¶
CreateDir creates directory with name returns passed name so it can be used in place.
MyFunction(CreateDir(t, "name"))
Calls t.Fatal() on error.
func CreateFile ¶
CreateFile is a wrapper around os.Create() which calls t.Fatal() on error.
func ErrReader ¶
ErrReader wraps reader r. The returned reader reads at most n bytes and returns error err. If err is set to nil ErrTestError will be used. Error other then err will be returned if underlying reader returns an error before n bytes are read.
func ErrWriter ¶
ErrWriter wraps writer w in a writer which writes at most n bytes and returns error err. If err is set to nil then ErrTestError will be used. Error other then err will be returned if underlying writer returns an error before n bytes are written.
func FromJSONReader ¶
FromJSONReader reads JSON from r and unmarshalls them to v. Calls t.Fatal() on error.
func HasNetConn ¶
func HasNetConn() bool
HasNetConn returns true if the Internet connection is present.
func NoNetworkSkip ¶
func NoNetworkSkip(t T)
NoNetworkSkip skips test if there is no Internet connection.
func RandFileName ¶
RandFileName returns random file name. If prefix is empty string it will be set to "file-". If ext is empty string it will be set to ".txt".
func ReadAllFromStart ¶
func ReadAllFromStart(t T, rs io.ReadSeeker) []byte
ReadAllFromStart seeks to the beginning of rs and reads it till gets io.EOF or any other error. Then seeks back to the position where rs was before the call. Calls t.Fatal() on error.
func ReadAllString ¶
ReadAllString is a wrapper around ioutil.ReadAll which returns string instead of byte slice. Calls t.Fatal() on error.
func Readdirnames ¶
Readdirnames returns slice returned by Readdirnames(0) called on fil instance. Calls t.Fatal() on error.
func ReplaceAllInFile ¶
ReplaceAllInFile replaces all occurrences of old sting with new in the filename. Calls t.Fatal() on error.
func ToJSONReader ¶
ToJSONReader marshals v to JSON and returns io.Reader for it. Calls t.Fatal() on error.
Types ¶
type HTTPServer ¶
type HTTPServer struct {
// contains filtered or unexported fields
}
HTTPServer represents very simple HTTP server recording all the requests it receives, responding with responses added with Rsp() method.
The server instance provides methods to analyse the received requests.
func NewHTTPServer ¶
func NewHTTPServer(t T) *HTTPServer
NewHTTPServer returns new instance of HTTPServer.
func (*HTTPServer) Body ¶
func (tst *HTTPServer) Body(n int) []byte
Body returns body of the nth received request. Calls t.Fatal() if n is greater then number or received requests.
func (*HTTPServer) Close ¶
func (tst *HTTPServer) Close() error
Close stops the test server and does cleanup. May be called multiple times.
func (*HTTPServer) Headers ¶
func (tst *HTTPServer) Headers(n int) http.Header
Headers returns headers for given request index. Calls t.Fatal() if n is greater then number or received requests.
func (*HTTPServer) ReqCount ¶
func (tst *HTTPServer) ReqCount() int
ReqCount returns number of requests recorded by test server.
func (*HTTPServer) Request ¶
func (tst *HTTPServer) Request(n int) *http.Request
Request returns clone of the nth received request. Calls t.Fatal() if n is greater then number or received requests.
func (*HTTPServer) Rsp ¶
func (tst *HTTPServer) Rsp(status int, rsp []byte) *HTTPServer
Rsp adds response with status and body to the list of responses to return.
Every time server receives a request it returns predefined response. The responses are returned in the order they were added. If there is no more responses the t.Fatal() will be called.
type T ¶
type T interface { // Cleanup registers a function to be called when the test and all its // subtests complete. Cleanup functions will be called in last added, // first called order. Cleanup(func()) // Error is equivalent to Log followed by Fail. Error(args ...interface{}) // Errorf is equivalent to Logf followed by Fail. Errorf(format string, args ...interface{}) // Fatal is equivalent to Log followed by FailNow. Fatal(args ...interface{}) // Fatalf is equivalent to Logf followed by FailNow. Fatalf(format string, args ...interface{}) // Helper marks the calling function as a test helper function. // When printing file and line information, that function will be skipped. // Helper may be called simultaneously from multiple goroutines. Helper() // Skip is equivalent to Log followed by SkipNow. Skip(args ...interface{}) }
T is a subset of testing.TB interface. It's primarily used to test the testkit package but can be used to implement custom actions to be taken on errors.