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
- type KeyValues
- type Request
- func (r *Request) AddCookie(key, value string) *Request
- func (r *Request) AddHeader(key, value string) *Request
- func (r *Request) MarshalBody(assert audit.Assertion, contentType string, data interface{}) *Request
- func (r *Request) RenderTemplate(assert audit.Assertion, contentType string, templateSource string, ...) *Request
- func (r *Request) SetRequestProcessor(processor RequestProcessor) *Request
- type RequestProcessor
- type Response
- func (r *Response) AssertBodyContains(expected string)
- func (r *Response) AssertBodyGrep(pattern string) []string
- func (r *Response) AssertBodyMatches(pattern string)
- func (r *Response) AssertCookie(key string) string
- func (r *Response) AssertCookieContains(key, expected string)
- func (r *Response) AssertCookieEquals(key, expected string)
- func (r *Response) AssertHeader(key string) string
- func (r *Response) AssertHeaderContains(key, expected string)
- func (r *Response) AssertHeaderEquals(key, expected string)
- func (r *Response) AssertStatusEquals(expected int)
- func (r *Response) AssertUnmarshalledBody(data interface{})
- func (r *Response) AssertUnmarshalledFeedback() rest.Feedback
- type TestServer
Constants ¶
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 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 ¶
NewRequest creates a new test request with the given method and path.
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 ¶
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 ¶
AssertBodyContains checks if the body contains a string.
func (*Response) AssertBodyGrep ¶
AssertBodyGrep greps content out of the body.
func (*Response) AssertBodyMatches ¶
AssertBodyMatches checks if the body matches a regular expression.
func (*Response) AssertCookie ¶
AssertCookie checks if a cookie exists and retrieves it.
func (*Response) AssertCookieContains ¶
AssertCookieContains checks if a cookie exists and looks for an expected part.
func (*Response) AssertCookieEquals ¶
AssertCookieEquals checks if a cookie exists and compares it to an expected one.
func (*Response) AssertHeader ¶
AssertHeader checks if a header exists and retrieves it.
func (*Response) AssertHeaderContains ¶
AssertHeaderContains checks if a header exists and looks for an expected part.
func (*Response) AssertHeaderEquals ¶
AssertHeaderEquals checks if a header exists and compares it to an expected one.
func (*Response) AssertStatusEquals ¶
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 ¶
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