restaudit

package
v2.15.5+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2017 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package restaudit of Tideland GoREST is a little helper package for the unit testing of the rest package and the resource handlers. Requests can easily be created, marshalling data based on the content-type is done automatically. Response also provides assert methods for the tests.

So first step is to create a test server and register the handler(s) to test. Could best be done with a little helper function, depending on own needs, e.g. when the context shall contain more information.

assert := audit.NewTestingAssertion(t, true)
cfgStr := "{etc {basepath /}{default-domain testing}{default-resource index}}"
cfg, err := etc.ReadString(cfgStr)
assert.Nil(err)
mux := rest.NewMultiplexer(context.Background(), cfg)
ts := restaudit.StartServer(mux, assert)
defer ts.Close()
err := mux.Register("my-domain", "my-resource", NewMyHandler())
assert.Nil(err)

During the tests you create the requests with

req := restaudit.NewRequest("GET", "/my-domain/my-resource/4711")
req.AddHeader(restaudit.HeaderAccept, restaudit.ApplicationJSON)

The request the is done with

resp := ts.DoRequest(req)
resp.AssertStatusEquals(200)
rest.AssertHeaderContains(restaudit.HeaderContentType, restaudit.ApplicationJSON)
resp.AssertBodyContains(`"ResourceID":"4711"`)

Also data can be marshalled including setting the content type and the response can be unmarshalled based on that type.

req.MarshalBody(assert, restaudit.ApplicationJSON, myInData)
...
var myOutData MyType
resp.AssertUnmarshalledBody(&myOutData)
assert.Equal(myOutData.MyField, "foo")

There are more helpers for a convenient test, but the fields of Request and Response can also be accessed directly.

Index

Constants

View Source
const (
	HeaderAccept      = "Accept"
	HeaderContentType = "Content-Type"

	ApplicationGOB  = "application/vnd.tideland.gob"
	ApplicationJSON = "application/json"
	ApplicationXML  = "application/xml"
)

Request and response header fields and values for testing purposes.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyValues

type KeyValues map[string]string

KeyValues handles keys and values for request headers and cookies.

type Request

type Request struct {
	Method           string
	Path             string
	Header           KeyValues
	Cookies          KeyValues
	Body             []byte
	RequestProcessor RequestProcessor
}

Request wraps all infos for a test request.

func NewRequest

func NewRequest(method, path string) *Request

NewRequest creates a new test request with the given method and path.

func (*Request) AddCookie

func (r *Request) AddCookie(key, value string) *Request

AddCookie adds or overwrites a request header.

func (*Request) AddHeader

func (r *Request) AddHeader(key, value string) *Request

AddHeader adds or overwrites a request header.

func (*Request) MarshalBody

func (r *Request) MarshalBody(
	assert audit.Assertion,
	contentType string,
	data interface{},
) *Request

MarshalBody sets the request body based on the type and the marshalled data.

func (*Request) RenderTemplate

func (r *Request) RenderTemplate(
	assert audit.Assertion,
	contentType string,
	templateSource string,
	data interface{},
) *Request

RenderTemplate renders the passed data into the template and assigns it to the request body. The content type will be set too.

func (*Request) SetRequestProcessor

func (r *Request) SetRequestProcessor(processor RequestProcessor) *Request

SetRequestProcessor sets the pre-processor.

type RequestProcessor

type RequestProcessor func(req *http.Request) *http.Request

RequestProcessor is for pre-processing HTTP requests.

type Response

type Response struct {
	Status  int
	Header  KeyValues
	Cookies KeyValues
	Body    []byte
	// contains filtered or unexported fields
}

Response wraps all infos of a test response.

func (*Response) AssertBodyContains

func (r *Response) AssertBodyContains(expected string)

AssertBodyContains checks if the body contains a string.

func (*Response) AssertBodyGrep

func (r *Response) AssertBodyGrep(pattern string) []string

AssertBodyGrep greps content out of the body.

func (*Response) AssertBodyMatches

func (r *Response) AssertBodyMatches(pattern string)

AssertBodyMatches checks if the body matches a regular expression.

func (*Response) AssertCookie

func (r *Response) AssertCookie(key string) string

AssertCookie checks if a cookie exists and retrieves it.

func (*Response) AssertCookieContains

func (r *Response) AssertCookieContains(key, expected string)

AssertCookieContains checks if a cookie exists and looks for an expected part.

func (*Response) AssertCookieEquals

func (r *Response) AssertCookieEquals(key, expected string)

AssertCookieEquals checks if a cookie exists and compares it to an expected one.

func (*Response) AssertHeader

func (r *Response) AssertHeader(key string) string

AssertHeader checks if a header exists and retrieves it.

func (*Response) AssertHeaderContains

func (r *Response) AssertHeaderContains(key, expected string)

AssertHeaderContains checks if a header exists and looks for an expected part.

func (*Response) AssertHeaderEquals

func (r *Response) AssertHeaderEquals(key, expected string)

AssertHeaderEquals checks if a header exists and compares it to an expected one.

func (*Response) AssertStatusEquals

func (r *Response) AssertStatusEquals(expected int)

AssertStatusEquals checks if the status is the expected one.

func (*Response) AssertUnmarshalledBody

func (r *Response) AssertUnmarshalledBody(data interface{})

AssertUnmarshalledBody retrieves the body based on the content type and unmarshals it accordingly.

func (*Response) AssertUnmarshalledFeedback

func (r *Response) AssertUnmarshalledFeedback() rest.Feedback

AssertUnmarshalledFeedback retrieves a rest.Feedback as body out of response and returns it for further tests.

type TestServer

type TestServer interface {
	// Close shuts down the server and blocks until all outstanding
	// requests have completed.
	Close()

	// DoRequest performs a request against the test server.
	DoRequest(req *Request) *Response

	// DoUpload is a special request for uploading a file.
	DoUpload(path, fieldname, filename, data string) *Response
}

TestServer defines the test server with methods for requests and uploads.

func StartServer

func StartServer(handler http.Handler, assert audit.Assertion) TestServer

StartServer starts a test server using the passed handler

Jump to

Keyboard shortcuts

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