tenuki

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2021 License: MIT Imports: 16 Imported by: 1

README

tenuki

test helper for web api testing

features

  • api request helper
  • capture transport
api request helper

plain old code is here

func Test(t *testing.T) {
	handler := func(w http.ResponseWriter, r *http.Request) {
		tenuki.Render(w, r).JSON(200, map[string]string{"message": "hello world"})
	}
	ts := httptest.NewServer(http.HandlerFunc(handler))

	f := tenuki.New(t)
	req := f.NewRequest("GET", ts.URL+"/hello", nil)
	res := f.Do(req)

	if want, got := 200, res.StatusCode; want != got {
		t.Errorf("status code\nwant\n\t%d\nbut\n\t%d", want, got)
	}

	want := map[string]string{"message": "hello world"}
	var got map[string]string
	f.Extract().JSON(res, &got)
	if !reflect.DeepEqual(want, got) {
		t.Errorf("response body\nwant\n\t%+v\nbut\n\t%+v", want, got)
	}
}

Requesting via tenkuki.Facade.Do(), capture transport is automatically activated. (If stopping this feature, please go test with NOCAPTURE=1 envvar)

capture transport

output example.

=== RUN   TestCapture/request_2
    request:
        POST / HTTP/1.1
        Host: 127.0.0.1:59424
        
        {"me": "foo"}
    response:
        HTTP/1.1 200 OK
        Content-Length: 21
        Content-Type: text/plain; charset=utf-8
        Date: Sun, 27 Sep 2020 14:14:55 GMT
        
        {"message": "hello"}

code example.

func TestCapture(t *testing.T) {
	transport := &tenuki.CapturedTransport{}

	ts := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprintln(w, `{"message": "hello"}`)
		},
	))
	defer ts.Close()

	client := &http.Client{Transport: transport}

    // GET request
	t.Run("request 1", func(t *testing.T) {
		defer transport.Capture(t)()

		client.Get(ts.URL)
	})

    // POST request
	t.Run("request 2", func(t *testing.T) {
		defer transport.Capture(t)()

		req, _ := http.NewRequest("POST", ts.URL, strings.NewReader(`{"me": "foo"}`))
		client.Do(req)
	})
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CaptureEnabledDefault   bool   = true
	CaptureWriteFileBaseDir string = ""

	CaptureCountEnabledDefault bool = false
	DefaultLayout              *capture.Layout
)

Functions

func DecodeJSON

func DecodeJSON(r io.Reader, ob interface{}) error

func NewCloseConnectionHandler added in v0.0.5

func NewCloseConnectionHandler(t *testing.T) http.HandlerFunc

NewCloseConnectionHandler is a handler that closes the connecion suddenly. this is one of the test utilities.

func NewTimeoutHandler added in v0.0.5

func NewTimeoutHandler(t *testing.T, timeout time.Duration) http.HandlerFunc

NewTimeoutHandler returns a handler that closes the connecion suddenly. this is one of the test utilities.

func NewTimeoutHandlerWithClient added in v0.0.5

func NewTimeoutHandlerWithClient(t *testing.T, timeouts ...time.Duration) (http.HandlerFunc, *http.Client)

NewTimeoutHandlerWithClient returns a handler that timeout is occured. this is one of the test utilities.

func WithLayout added in v0.0.5

func WithLayout(layout *capture.Layout) func(*Config)

func WithPrinter added in v0.0.5

func WithPrinter(printer printer) func(*Config)

func WithWriteFile added in v0.0.4

func WithWriteFile(basedir string) func(*Config)

func WithoutCapture added in v0.0.4

func WithoutCapture() func(*Config)

Types

type AssertOption

type AssertOption func(*Assertion)

func AssertError added in v0.0.5

func AssertError(handler func(err error) error) AssertOption

func AssertStatus

func AssertStatus(code int) AssertOption

type Assertion

type Assertion struct {
	StatusCode  int
	Checks      []func(t *testing.T, res *http.Response)
	ExpectError func(err error) error
}

func NewAssertion

func NewAssertion() *Assertion

type CapturedTransport

type CapturedTransport struct {
	T         *testing.T
	Transport http.RoundTripper
}

func NewCaptureTransport added in v0.0.5

func NewCaptureTransport(t *testing.T, transport http.RoundTripper, options ...func(*Config)) *CapturedTransport

func (*CapturedTransport) Capture

func (ct *CapturedTransport) Capture(t *testing.T) func()

func (*CapturedTransport) GetPrefix added in v0.0.5

func (ct *CapturedTransport) GetPrefix() string

func (*CapturedTransport) Printf added in v0.0.3

func (ct *CapturedTransport) Printf(fmt string, args ...interface{})

func (*CapturedTransport) RoundTrip

func (ct *CapturedTransport) RoundTrip(req *http.Request) (*http.Response, error)

type Config added in v0.0.5

type Config struct {
	// contains filtered or unexported fields
}

func DefaultConfig added in v0.0.5

func DefaultConfig(options ...func(*Config)) *Config

func (*Config) NewCaptureTransport added in v0.0.5

func (c *Config) NewCaptureTransport(transport http.RoundTripper, getPrefix func() string) http.RoundTripper

type ExtractFacade

type ExtractFacade struct {
	T *testing.T
	// contains filtered or unexported fields
}

func (*ExtractFacade) BindJSON added in v0.0.5

func (f *ExtractFacade) BindJSON(res *http.Response, ob interface{})

func (*ExtractFacade) Bytes

func (f *ExtractFacade) Bytes(res *http.Response) []byte

func (*ExtractFacade) JSON

func (f *ExtractFacade) JSON(res *http.Response) interface{}

func (*ExtractFacade) String added in v0.0.5

func (f *ExtractFacade) String(res *http.Response) string

type Facade

type Facade struct {
	T *testing.T
	*Config

	Client *http.Client
	// contains filtered or unexported fields
}

func New

func New(t *testing.T, options ...func(*Config)) *Facade

func (*Facade) Do

func (f *Facade) Do(
	req *http.Request,
	options ...AssertOption,
) *http.Response

func (*Facade) DoHandler added in v0.0.4

func (f *Facade) DoHandler(
	handler http.Handler,
	req *http.Request,
	options ...AssertOption,
) *http.Response

func (*Facade) DoHandlerFunc added in v0.0.4

func (f *Facade) DoHandlerFunc(
	handler http.HandlerFunc,
	req *http.Request,
	options ...AssertOption,
) *http.Response

func (*Facade) Extract

func (f *Facade) Extract() *ExtractFacade

func (*Facade) NewJSONRequest added in v0.0.5

func (f *Facade) NewJSONRequest(
	method, url string, body io.Reader,
) *http.Request

func (*Facade) NewRequest

func (f *Facade) NewRequest(
	method, url string, body io.Reader,
) *http.Request

type HandlerTripper

type HandlerTripper struct {
	Before  func(*http.Request)
	Handler http.Handler
	After   func(*http.Response, *http.Request)
}

func HandlerTripperFunc

func HandlerTripperFunc(handle func(w http.ResponseWriter, r *http.Request)) *HandlerTripper

func (*HandlerTripper) RoundTrip

func (t *HandlerTripper) RoundTrip(r *http.Request) (*http.Response, error)

type RenderFacade

type RenderFacade struct {
	// contains filtered or unexported fields
}

func NewRender added in v0.0.5

func NewRender(w http.ResponseWriter, r *http.Request) *RenderFacade

func Render

func (*RenderFacade) JSON

func (f *RenderFacade) JSON(code int, v interface{})

func (*RenderFacade) JSONArray

func (f *RenderFacade) JSONArray(code int, v interface{})

func (*RenderFacade) SetNGStatus

func (f *RenderFacade) SetNGStatus(code int) *RenderFacade

type RoundTripFunc

type RoundTripFunc func(*http.Request) (*http.Response, error)

func NewErrorTransport added in v0.0.5

func NewErrorTransport(t *testing.T, genErr func() error) RoundTripFunc

NewErrorTransport returns a transport that returns error. this is one of the test utilities.

func (RoundTripFunc) RoundTrip

func (f RoundTripFunc) RoundTrip(r *http.Request) (*http.Response, error)

type TokenAuthorization added in v0.0.5

type TokenAuthorization struct {
	Token string
}

func (*TokenAuthorization) Apply added in v0.0.5

func (a *TokenAuthorization) Apply(req *http.Request)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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