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 NewHTTPRequest(method, path string, data *MD) *http.Request
- func RemoveTmpEnvs(tmpKey string)
- func RestoreStderr(printData ...bool) (s string)
- func RestoreStdout(printData ...bool) (s string)
- func RestoreTimeLocal()
- func RevertOSEnv()
- func RewriteStderr()
- func RewriteStdout()
- func RunOnCleanEnv(runFn func())
- func SetOsEnvs(mp map[string]string) string
- func SetTimeLocal(tl *time.Location)
- func SetTimeLocalUTC()
- type Buffer
- type DirEnt
- type EchoReply
- type EchoServer
- type M
- type MD
- type SafeBuffer
- type TestWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearOSEnv ¶ added in v0.5.12
func ClearOSEnv()
ClearOSEnv info for some testing cases.
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 input env map data.
will CLEAR all old ENV data, use given a 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 multi line text string. Will **CLEAR** all old ENV data, use given data map, and will recover old ENV after fn run. see MockCleanOsEnv
Usage:
testutil.MockOsEnvByText(`
APP_COMMAND = login
APP_ENV = dev
APP_DEBUG = true
`, func() {
// do something ...
})
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 NewHTTPRequest ¶ added in v0.6.11
NewHTTPRequest quick create request 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 RemoveTmpEnvs ¶ added in v0.7.0
func RemoveTmpEnvs(tmpKey string)
RemoveTmpEnvs remove test set envs by SetOsEnvs
func RestoreStderr ¶ added in v0.2.10
RestoreStderr restore os.Stderr
func RestoreStdout ¶
RestoreStdout restore os.Stdout
func RestoreTimeLocal ¶ added in v0.6.13
func RestoreTimeLocal()
RestoreTimeLocal restore time.Local
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()
func RunOnCleanEnv ¶ added in v0.7.0
func RunOnCleanEnv(runFn func())
RunOnCleanEnv will CLEAR all old ENV, then run given func. will RECOVER old ENV after fn run.
func SetOsEnvs ¶ added in v0.7.0
SetOsEnvs by map data with a group key. should call RemoveTmpEnvs after tested.
Usage:
tmpKey := testutil.SetOsEnvs(map[string]string{
"APP_COMMAND": "login",
"APP_ENV": "dev",
"APP_DEBUG": "true",
})
defer testutil.RemoveTmpEnvs(tmpKey)
func SetTimeLocal ¶ added in v0.6.13
SetTimeLocal custom time.Local for testing.
func SetTimeLocalUTC ¶ added in v0.6.13
func SetTimeLocalUTC()
SetTimeLocalUTC custom time.Local=UTC for testing.
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 data.
//
// If value is one elem, will return string, otherwise will return []string
//
// Example:
// map[string]any{
// "Connection": "close",
// "Vary": []string{"Accept-Encoding", "Accept-Encoding"},
// }
Headers map[string]any `json:"headers,omitempty"`
// Form data.
//
// If value is one elem, will return string, otherwise will return []string
Form map[string]any `json:"form,omitempty"`
// Body data string from request body
Body string `json:"body,omitempty"`
// JSON data on Content-Type: application/json
// - 通常是 map[string]any 类型数据
JSON any `json:"json,omitempty"`
// Files data.
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
func (*EchoReply) HeaderString ¶ added in v0.7.2
HeaderString get header value as string
type EchoServer ¶ added in v0.6.16
EchoServer for testing http request.
func MockHttpServer ¶ added in v0.7.0
func MockHttpServer() *EchoServer
MockHttpServer create an echo server for testing. alias of NewEchoServer
func NewEchoServer ¶ added in v0.6.10
func NewEchoServer() *EchoServer
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 = s.PrintHttpHost()
m.Run()
}
// in a test case ...
res := http.Get(testSrvAddr + "/get/some-one")
rpl := testutil.ParseRespToReply(res)
// assert ...
func (*EchoServer) HTTPHost ¶ added in v0.6.16
func (s *EchoServer) HTTPHost() string
HTTPHost get http host address. eg: http://127.0.0.1:8999
func (*EchoServer) HostAddr ¶ added in v0.6.16
func (s *EchoServer) HostAddr() string
HostAddr get host address. eg: 127.0.0.1:8999
func (*EchoServer) PrintHttpHost ¶ added in v0.7.0
func (s *EchoServer) PrintHttpHost() string
PrintHttpHost print host address to console
type MD ¶
type MD struct {
// Headers headers
Headers M
// Body reader. eg: strings.NewReader("name=inhere")
Body io.Reader
// BodyString quick adds string body.
BodyString string
// BeforeSend callback
BeforeSend func(req *http.Request)
}
MD simple request data
type SafeBuffer ¶ added in v0.7.2
SafeBuffer Thread-safe buffer for testing
func (*SafeBuffer) ResetGet ¶ added in v0.7.2
func (sb *SafeBuffer) ResetGet() string
ResetGet get buffer content and reset
func (*SafeBuffer) Write ¶ added in v0.7.2
func (sb *SafeBuffer) Write(p []byte) (n int, err error)
Write implements io.Writer
func (*SafeBuffer) WriteString ¶ added in v0.7.2
func (sb *SafeBuffer) WriteString(s string) (n int, err error)
WriteString implements io.StringWriter
type TestWriter ¶ added in v0.5.3
TestWriter struct, useful for testing. alias of fakeobj.Writer
