Documentation
¶
Overview ¶
Package testutil provide some test help util functions. eg: http test, mock ENV value
Index ¶
- func ClearOSEnv()
- func DiscardStdout() error
- func MockCleanOsEnv(mp map[string]string, fn func())
- func MockEnvValue(key, val string, fn func(nv string))
- func MockEnvValues(kvMap map[string]string, fn func())
- func MockOsEnv(mp map[string]string, fn func())
- func MockOsEnvByText(envText string, fn func())
- func MockRequest(h http.Handler, method, path string, data *MD) *httptest.ResponseRecorder
- func NewBuffer() *byteutil.Buffer
- func NewDirEnt(fpath string, isDir ...bool) *fakeobj.DirEntry
- func NewEchoServer() *httptest.Server
- func NewHttpRequest(method, path string, data *MD) *http.Request
- func RestoreStderr(printData ...bool) (s string)
- func RestoreStdout(printData ...bool) (s string)
- func RevertOSEnv()
- func RewriteStderr()
- func RewriteStdout()
- type Buffer
- type DirEnt
- type EchoReply
- type M
- type MD
- type TestWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearOSEnv ¶ added in v0.5.12
func ClearOSEnv()
ClearOSEnv info.
Usage:
testutil.ClearOSEnv() defer testutil.RevertOSEnv() // do something ...
func DiscardStdout ¶
func DiscardStdout() error
DiscardStdout Discard os.Stdout output
Usage:
DiscardStdout()
fmt.Println("Hello, playground")
RestoreStdout()
func MockCleanOsEnv ¶ added in v0.5.5
MockCleanOsEnv by env map data.
will clear all old ENV data, use given data map. will recover old ENV after fn run.
func MockEnvValue ¶ added in v0.1.6
MockEnvValue will store old env value, set new val. will restore old value on end.
func MockEnvValues ¶ added in v0.1.6
MockEnvValues will store old env value, set new val. will restore old value on end.
func MockOsEnvByText ¶ added in v0.3.13
func MockOsEnvByText(envText string, fn func())
MockOsEnvByText by env text string. will clear all old ENV data, use given data map. will recover old ENV after fn run.
func MockRequest ¶
MockRequest mock an HTTP Request
Usage:
handler := router.New()
res := MockRequest(handler, "GET", "/path", nil)
// with data 1
body := strings.NewReader("string ...")
res := MockRequest(handler, "POST", "/path", &MD{
Body: body,
HeaderM: M{"x-head": "val"}
})
// with data 2
res := MockRequest(handler, "POST", "/path", &MD{
BodyString: "data string",
HeaderM: M{"x-head": "val"}
})
func NewEchoServer ¶ added in v0.6.10
NewEchoServer create an echo server for testing.
Usage on testing:
var testSrvAddr string
func TestMain(m *testing.M) {
// create server
s := testutil.NewEchoServer()
defer s.Close()
testSrvAddr = "http://" + s.Listener.Addr().String()
fmt.Println("Test server listen on:", testSrvAddr)
m.Run()
}
// in a test case ...
res := http.Get(testSrvAddr)
rpl := testutil.ParseRespToReply(res)
// assert ...
func NewHttpRequest ¶ added in v0.3.6
NewHttpRequest for http testing Usage:
req := NewHttpRequest("GET", "/path", nil)
// with data 1
body := strings.NewReader("string ...")
req := NewHttpRequest("POST", "/path", &MD{
Body: body,
Headers: M{"x-head": "val"}
})
// with data 2
req := NewHttpRequest("POST", "/path", &MD{
BodyString: "data string",
Headers: M{"x-head": "val"}
})
func RestoreStderr ¶ added in v0.2.10
RestoreStderr restore os.Stderr
func RestoreStdout ¶
RestoreStdout restore os.Stdout
func RewriteStderr ¶ added in v0.2.10
func RewriteStderr()
RewriteStderr rewrite os.Stderr
Usage:
RewriteStderr() fmt.Fprintln(os.Stderr, "Hello, playground") msg := RestoreStderr()
func RewriteStdout ¶
func RewriteStdout()
RewriteStdout rewrite os.Stdout
Usage:
RewriteStdout()
fmt.Println("Hello, playground")
msg := RestoreStdout()
Types ¶
type EchoReply ¶ added in v0.6.10
type EchoReply struct {
Origin string `json:"origin"`
Url string `json:"url"`
Method string `json:"method"`
// Query data
Query map[string]any `json:"query,omitempty"`
Headers map[string]any `json:"headers,omitempty"`
Form map[string]any `json:"form,omitempty"`
// Body data string
Body string `json:"body,omitempty"`
Json any `json:"json,omitempty"`
Files map[string]any `json:"files,omitempty"`
}
EchoReply http response data reply model
func BuildEchoReply ¶ added in v0.6.10
BuildEchoReply build reply body data
func ParseBodyToReply ¶ added in v0.6.10
func ParseBodyToReply(bd io.ReadCloser) *EchoReply
ParseBodyToReply parse http body to reply
func ParseRespToReply ¶ added in v0.6.10
ParseRespToReply parse http response to reply
func (*EchoReply) ContentType ¶ added in v0.6.10
ContentType get content type
type MD ¶
type MD struct {
// Headers headers
Headers M
// Body body. eg: strings.NewReader("name=inhere")
Body io.Reader
// BodyString quick add body.
BodyString string
// BeforeSend callback
BeforeSend func(req *http.Request)
}
MD simple request data
type TestWriter ¶ added in v0.5.3
type TestWriter struct {
Buffer
// ErrOnWrite return error on write, useful for testing
ErrOnWrite bool
// ErrOnFlush return error on flush, useful for testing
ErrOnFlush bool
// ErrOnClose return error on close, useful for testing
ErrOnClose bool
}
TestWriter struct, useful for testing
func (*TestWriter) SetErrOnClose ¶ added in v0.5.6
func (w *TestWriter) SetErrOnClose() *TestWriter
SetErrOnClose method
func (*TestWriter) SetErrOnFlush ¶ added in v0.5.6
func (w *TestWriter) SetErrOnFlush() *TestWriter
SetErrOnFlush method
func (*TestWriter) SetErrOnWrite ¶ added in v0.5.6
func (w *TestWriter) SetErrOnWrite() *TestWriter
SetErrOnWrite method
Directories
¶
| Path | Synopsis |
|---|---|
|
Package assert Provides commonly asserts functions for help write Go testing.
|
Package assert Provides commonly asserts functions for help write Go testing. |
|
Package fakeobj provides a fake object for testing.
|
Package fakeobj provides a fake object for testing. |
