htest

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2021 License: MIT Imports: 13 Imported by: 0

README

htest

Provides expressive assertion of http handlers that return HTML

Backlog

  • COULD add a build in way to validate html using the official html validator. Or might be worthy of a separate package so it can also run on our css and js. https://github.com/validator/validator
  • COULD allow for session matching with a build in type, to allow for users to test what is set in a session
  • COULD support fuzzing of templates

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTestFail = errors.New("test fail")

ErrTestFail is thrown when the failer fails for testing

View Source
var TruncateBodyLength = 512

TruncateBodyLength is the length after which the rest of the response print will be ignored and replaced with ...

Functions

This section is empty.

Types

type Case

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

Case represents a set of assertions for an http.Handler

func New

func New(tb testing.TB, h http.Handler) *Case

New initiates a test case

func NewCase

func NewCase(fail Failer, h http.Handler) *Case

NewCase will setup a case with a custom failer

func (*Case) GET

func (c *Case) GET(p string) *RequestBuilder

GET will start the building of a GET request

func (*Case) POST

func (c *Case) POST(p string) *RequestBuilder

POST will start the building of a POST request

type Fail

type Fail struct {
	FF func(format string, args ...interface{})
	// contains filtered or unexported fields
}

Fail is a failer implementation that cals FF on fatalf

func NewStdFail

func NewStdFail(tb testing.TB) *Fail

NewStdFail will fail using the standard library tb interface

func NewTestFail

func NewTestFail() (*Fail, *bytes.Buffer)

NewTestFail gives a failer that will panic and write to a buffer

func (*Fail) Copy

func (f *Fail) Copy() Failer

Copy will return a new instance of the failer

func (*Fail) Fatalf

func (f *Fail) Fatalf(format string, args ...interface{})

Fatalf will end the test and fail

func (*Fail) WithHTML

func (f *Fail) WithHTML(sel *goquery.Selection) Failer

WithHTML will configure the fail in context of the selected html

func (*Fail) WithRequest

func (f *Fail) WithRequest(r *http.Request) Failer

WithRequest will configure the fail in context of the request

func (*Fail) WithResponse

func (f *Fail) WithResponse(w *httptest.ResponseRecorder) Failer

WithResponse will configure the fail in context of this response

type Failer

type Failer interface {
	Fatalf(format string, args ...interface{})
	WithRequest(*http.Request) Failer
	WithResponse(*httptest.ResponseRecorder) Failer
	WithHTML(sel *goquery.Selection) Failer
	Copy() Failer
}

Failer is used to fail cases with extra context that should help the developer figure out what went wrong.

type HTMLAsserter

type HTMLAsserter struct {
	Selection *goquery.Selection
	// contains filtered or unexported fields
}

HTMLAsserter asserts (parts of) an HTML document

func (*HTMLAsserter) AssertAll

func (ha *HTMLAsserter) AssertAll(selector string) *HTMLAsserter

AssertAll will return a new asserter that allows for checking ALL elements that match the selector.

func (*HTMLAsserter) Attr

func (ha *HTMLAsserter) Attr(n, exp string) *HTMLAsserter

Attr will assert of the attribute named 'n' has expected value 'exp'

func (*HTMLAsserter) Check added in v0.2.0

func (ha *HTMLAsserter) Check(c HTMLChecker) *HTMLAsserter

Check allows for checking the response using resuable custom logic

func (*HTMLAsserter) Count

func (ha *HTMLAsserter) Count(exp int) *HTMLAsserter

Count asserts how many elements there are to be expected

func (*HTMLAsserter) Text

func (ha *HTMLAsserter) Text(exp string) *HTMLAsserter

Text will assert that the text context of the selection equals 'exp'

func (*HTMLAsserter) TextMatch

func (ha *HTMLAsserter) TextMatch(expr string) *HTMLAsserter

TextMatch will assert that the text in the selection matches the pattern

type HTMLChecker added in v0.2.0

type HTMLChecker interface {
	CheckHTML(f Failer, sel *goquery.Selection)
}

HTMLChecker can be implemented to check HTML through custom logic

type HTMLCheckerFunc added in v0.2.0

type HTMLCheckerFunc func(f Failer, se *goquery.Selection)

HTMLCheckerFunc allows for casting a function to implement the HTMLChecker

func (HTMLCheckerFunc) CheckHTML added in v0.2.0

func (f HTMLCheckerFunc) CheckHTML(fail Failer, se *goquery.Selection)

CheckHTML implements ResponseChecker

type RequestBuilder

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

RequestBuilder allows building a request for a testcase

func (*RequestBuilder) EXPECT

func (rb *RequestBuilder) EXPECT() *ResponseAsserter

EXPECT will create and perform the request on the case's http handler and return an asserter for the http response.

func (*RequestBuilder) WithFormData

func (rb *RequestBuilder) WithFormData(pairs ...string) *RequestBuilder

WithFormData will set the requests' body to the form encoded shape of the provided key value pairs and set the content type to

func (*RequestBuilder) WithHeader

func (rb *RequestBuilder) WithHeader(k, v string) *RequestBuilder

WithHeader sets a header value for the request building

func (*RequestBuilder) WithHeaders

func (rb *RequestBuilder) WithHeaders(pairs ...string) *RequestBuilder

WithHeaders allows adding headers as string pairs

type ResponseAsserter

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

ResponseAsserter allows for asserting an http response

func (*ResponseAsserter) BodySize

func (ra *ResponseAsserter) BodySize(exp int) *ResponseAsserter

BodySize asserts the size of the response body

func (*ResponseAsserter) Check added in v0.2.0

Check allows for checking the response using resuable custom logic

func (*ResponseAsserter) HTML

func (ra *ResponseAsserter) HTML() *HTMLAsserter

HTML will parse the response body as html and return an asserter for it

func (*ResponseAsserter) Header

func (ra *ResponseAsserter) Header(k, exp string) *ResponseAsserter

Header asserts that the header 'k' equals 'exp'

func (*ResponseAsserter) Status

func (ra *ResponseAsserter) Status(c int) *ResponseAsserter

Status asserts that the status equal c

type ResponseChecker added in v0.2.0

type ResponseChecker interface {
	CheckResponse(f Failer, rec *httptest.ResponseRecorder)
}

ResponseChecker can be implemented to check responses through custom logic

type ResponseCheckerFunc added in v0.2.0

type ResponseCheckerFunc func(f Failer, rec *httptest.ResponseRecorder)

ResponseCheckerFunc allows casing a function to implement the ResponseChecker

func (ResponseCheckerFunc) CheckResponse added in v0.2.0

func (f ResponseCheckerFunc) CheckResponse(fail Failer, rec *httptest.ResponseRecorder)

CheckResponse implements ResponseChecker

Jump to

Keyboard shortcuts

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