Skip to content

DefaultFormatter.fillGeneral tends to panic if called outside a unit test context #410

@paul-at-cybr

Description

@paul-at-cybr

This line causes a panic if you run httpexpect outside a standard go unit test context, since the testing package hasn't been imported and its init-function hasn't run:

panic: testing: Verbose called before Parse

goroutine 1 [running]:
testing.Verbose(...)
        /usr/local/go/src/testing/testing.go:659
github.com/gavv/httpexpect/v2.(*DefaultFormatter).fillGeneral(0x0?, 0xc0000e3a40, 0xc0002e2930)
        /home/myuser/go/pkg/mod/github.com/gavv/httpexpect/v2@v2.15.0/formatter.go:322 +0x19d
github.com/gavv/httpexpect/v2.(*DefaultFormatter).buildFormatData(0xc0000d28f0?, 0xc0002e28c0?, 0xc00009edc0)
        /home/myuser/go/pkg/mod/github.com/gavv/httpexpect/v2@v2.15.0/formatter.go:259 +0x4a
github.com/gavv/httpexpect/v2.(*DefaultFormatter).applyTemplate(0x1?, {0x9e5d21, 0xf}, {0xa0571b, 0x589}, 0x40f088?, 0xc0002e2930?, 0x70?)
        /home/myuser/go/pkg/mod/github.com/gavv/httpexpect/v2@v2.15.0/formatter.go:237 +0x45
github.com/gavv/httpexpect/v2.(*DefaultFormatter).FormatFailure(0xc00031fae8?, 0x8aafa6?, 0x7f2f3604c108?)
        /home/myuser/go/pkg/mod/github.com/gavv/httpexpect/v2@v2.15.0/formatter.go:110 +0x7b
github.com/gavv/httpexpect/v2.(*DefaultAssertionHandler).Failure(0xc0000fef00, 0x1?, 0x1?)
        /home/myuser/go/pkg/mod/github.com/gavv/httpexpect/v2@v2.15.0/assertion.go:273 +0x4f
github.com/gavv/httpexpect/v2.(*chain).leave(0xc00031fb18?)
        /home/myuser/go/pkg/mod/github.com/gavv/httpexpect/v2@v2.15.0/chain.go:392 +0xbd
github.com/gavv/httpexpect/v2.(*Request).Expect(0xc0000e7040)
        /home/myuser/go/pkg/mod/github.com/gavv/httpexpect/v2@v2.15.0/request.go:1957 +0x175
github.com/my-org/my-project/tests.BfpDevTest(0xc00009e8c0?)
        /home/myuser/my-project/tests/bfpdev.go:42 +0x79
github.com/my-org/my-project/tester.(*Caddy).Run(0xc00009e8c0)
        /home/myuser/my-project/tester/caddy.go:64 +0x172
main.main()
        /home/myuser/my-project/cmd/test/main.go:20 +0x12f
exit status 2

The issue can be replicated by creating a custom logger that implements the httpexpect.TestingTB interface, and passing it to httpexpect.Default:

func NewHTTPExpect(name string, baseURL string) (*httpexpect.Expect, *Log) {
	tb := &Log{TestName: name}
	return httpexpect.Default(tb, baseURL), tb
}

The issue can be sidestepped by instead using httpexpect.WithConfig, and passing a httpexpect.DefaultFormatter that uses ColorModeAlways or ColorModeNever:

e := httpexpect.WithConfig(httpexpect.Config{
		TestName: name,
		BaseURL:  baseURL,
		Reporter: httpexpect.NewAssertReporter(tb),
		Formatter: &httpexpect.DefaultFormatter{
			ColorMode: httpexpect.ColorModeNever,
		},
	})

One way to fix this would be to check flag.Parsed() before calling testing.Verbose():

data.EnableColors = ctx.TestingTB && flag.Parsed() && testing.Verbose()

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions