iffy

package
v0.11.2-0...-205a77c Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2021 License: MIT Imports: 9 Imported by: 0

README

iffy helps you test http handlers.

We assume JSON for any marshaling/unmarshaling. If you use something else, that's fine, but the advanced features (responseObject + templating) will not work.

Example:

func TestFoo(t *testing.T) {

        // Instantiate & configure anything that implements http.Handler
        r := gin.Default()
        r.GET("/hello", tonic.Handler(helloHandler, 200))
        r.POST("/foo", tonic.Handler(newFoo, 201))
        r.DELETE("/foo/:fooid", tonic.Handler(delFoo, 204))

        tester := iffy.NewTester(t, r)

        // Variadic list of checker functions = func(r *http.Response, body string, responseObject interface{}) error
        //
        // Checkers can use closures to trap checker-specific configs -> ExpectStatus(200)
        // Some are provided in the iffy package, but you can use your own Checker functions
        tester.AddCall("helloworld", "GET", "/hello?who=world", "").Checkers(iffy.ExpectStatus(200), iffy.ExpectJSONFields("msg", "bla"))
        tester.AddCall("badhello", "GET", "/hello", "").Checkers(iffy.ExpectStatus(400))

        // Optionally, pass an instantiated response object ( &Foo{} )
        // The response body will be unmarshaled into it, then it will be presented to the Checker functions (parameter 'responseObject')
        // That way your custom checkers can directly use your business objects (ExpectValidFoo)
        tester.AddCall("createfoo", "POST", "/foo", `{"bar": "baz"}`).ResponseObject(&Foo{}).Checkers(iffy.ExpectStatus(201), ExpectValidFoo)

        // You can template query string and/or body using partial results from previous calls
        // e.g.: delete the object that was created in a previous step
        tester.AddCall("deletefoo", "DELETE", "/foo/{{.createfoo.id}}", "").Checkers(iffy.ExpectStatus(204))

        tester.Run()
}

For a real-life example, see https://github.com/loopfz/gadgeto/blob/master/tonic/tonic_test.go

Documentation

Overview

Package iffy helps you test http handlers.

We assume JSON for any marshaling/unmarshaling. If you use something else, that's fine, but the advanced features (responseObject + templating) will not work. For a real-life example, see https://github.com/loopfz/gadgeto/blob/master/tonic/tonic_test.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExpectListNonEmpty

func ExpectListNonEmpty(r *http.Response, body string, respObject interface{}) error

Types

type Call

type Call struct {
	Name     string
	Method   string
	QueryStr string
	Body     string
	// contains filtered or unexported fields
}

func (*Call) Checkers

func (c *Call) Checkers(ch ...Checker) *Call

func (*Call) Headers

func (c *Call) Headers(h Headers) *Call

func (*Call) ResponseObject

func (c *Call) ResponseObject(respObject interface{}) *Call

type Checker

type Checker func(r *http.Response, body string, respObject interface{}) error

func DumpResponse

func DumpResponse(t *testing.T) Checker

func ExpectJSONBranch

func ExpectJSONBranch(nodes ...string) Checker

func ExpectJSONFields

func ExpectJSONFields(fields ...string) Checker

func ExpectListLength

func ExpectListLength(length int) Checker

func ExpectStatus

func ExpectStatus(st int) Checker

func UnmarshalResponse

func UnmarshalResponse(i interface{}) Checker

type Headers

type Headers map[string]string

type Tester

type Tester struct {
	Calls []*Call

	Fatal bool
	// contains filtered or unexported fields
}

func NewTester

func NewTester(t *testing.T, r http.Handler, calls ...*Call) *Tester

func (*Tester) AddCall

func (t *Tester) AddCall(name, method, querystr, body string) *Call

func (*Tester) Reset

func (t *Tester) Reset()

func (*Tester) Run

func (it *Tester) Run()

type Values

type Values map[string]interface{}

func (Values) Apply

func (v Values) Apply(templateStr string) ([]byte, error)

Jump to

Keyboard shortcuts

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