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 NewHttpRequest(method, path string, data *MD) *http.Requestdeprecated
- func RestoreStderr(printData ...bool) (s string)
- func RestoreStdout(printData ...bool) (s string)
- func RestoreTimeLocal()
- func RevertOSEnv()
- func RewriteStderr()
- func RewriteStdout()
- func SetTimeLocal(tl *time.Location)
- func SetTimeLocalUTC()
- type Buffer
- type DirEnt
- type EchoReply
- type EchoServer
- 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 multi line text string. will clear all old ENV data, use given data map, and will recover old ENV after fn run.
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 NewHttpRequest
deprecated
added in
v0.3.6
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 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 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 EchoServer ¶ added in v0.6.16
EchoServer for testing http request.
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.HTTPHost() fmt.Println("Test server listen on:", testSrvAddr) m.Run() } // in a test case ... res := http.Get(testSrvAddr) 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
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
TestWriter struct, useful for testing. alias of fakeobj.Writer
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. |