testutil

package
v0.2.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2020 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiscardStdout

func DiscardStdout() error

DiscardStdout Discard os.Stdout output Usage:

DiscardStdout()
fmt.Println("Hello, playground")
RestoreStdout()

func MockEnvValue added in v0.1.6

func MockEnvValue(key, val string, fn func(nv string))

MockEnvValue will store old env value, set new val. will restore old value on end.

func MockEnvValues added in v0.1.6

func MockEnvValues(kvMap map[string]string, fn func())

MockEnvValues will store old env value, set new val. will restore old value on end.

func MockRequest

func MockRequest(h http.Handler, method, path string, data *MD) *httptest.ResponseRecorder

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,
	Headers: M{"x-head": "val"}
})
// with data 2
res := MockRequest(handler, "POST", "/path", &MD{
	BodyString: "data string",
	Headers: M{"x-head": "val"}
})

func RestoreStderr added in v0.2.10

func RestoreStderr() (s string)

RestoreStderr restore os.Stderr

func RestoreStdout

func RestoreStdout() (s string)

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 M

type M map[string]string

M short name for map

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL