testutil

package
v0.0.0-...-754e97a Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2020 License: MIT Imports: 6 Imported by: 0

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

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

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 NewHttpRequest

func NewHttpRequest(method, path string, data *MD) *http.Request

NewHttpRequest for http testing Usage:

req := NewHttpRequest(handler, "GET", "/path", nil)

// with data 1
body := strings.NewReader("string ...")
req := NewHttpRequest(handler, "POST", "/path", &MD{
	Body: body,
	Headers: M{"x-head": "val"}
})

// with data 2
req := NewHttpRequest(handler, "POST", "/path", &MD{
	BodyString: "data string",
	Headers: M{"x-head": "val"}
})

func RestoreStderr

func RestoreStderr() (s string)

RestoreStderr restore os.Stderr

func RestoreStdout

func RestoreStdout() (s string)

RestoreStdout restore os.Stdout

func RewriteStderr

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