Documentation ¶
Overview ¶
Package celeritytest provides helpers for testing Celerity based applications.
Its purpose is to make it easy to write full integration tests for endpoints in the server. You can easily make requests against a testing server and get back a response object which contains various helper methods to parse the output.
Making Request ¶
Internally celeritytest boots up a test server and and makes a request to the given endpoint. The return value is a celeritytest. Response object which has helper methods to validate the JSON response given from the server.
func TestExample(t *testing.T) { svr := celerity.New() svr.Route(celerity.GET, "/foo", func(c celerity.Context) celerity.Response { d := map[string]string{"firstName": "alice"} return c.R(d) }) r, _ := celeritytest.Get(svr, "/foo") if ok, v := r.AssertString("firstName", "alice"); !ok { t.Errorf("first name not valid: %s", v) } }
Using RequestOptions ¶
For more complicated requests the Request function can be used. This function accepts a RequestOptions stuct that allows for more configuration.
func TestExample(t *testing.T) { svr := celerity.New() svr.Route(celerity.GET, "/foo", func(c celerity.Context) celerity.Response { d := map[string]string{"firstName": "alice"} return c.R(d) }) opts := RequestOptions { Path: "/foo", Method: celerity.GET, Headers: http.Header(map[string]string{"Authorization": "1234567"}), } r, _ := celeritytest.Request(svr, opts) if ok, v := r.AssertString("firstName", "alice"); !ok { t.Errorf("first name not valid: %s", v) } }
Index ¶
- type RequestOptions
- type Response
- func Delete(s *celerity.Server, path string, data []byte) (*Response, error)
- func Get(s *celerity.Server, path string) (*Response, error)
- func Patch(s *celerity.Server, path string, data []byte) (*Response, error)
- func Post(s *celerity.Server, path string, data []byte) (*Response, error)
- func Put(s *celerity.Server, path string, data []byte) (*Response, error)
- func Request(s *celerity.Server, opts RequestOptions) (*Response, error)
- func (r *Response) AssertBool(path string, value bool) (bool, bool)
- func (r *Response) AssertInt(path string, value int) (bool, int)
- func (r *Response) AssertString(path, value string) (bool, string)
- func (r *Response) Exists(path string) bool
- func (r *Response) Extract(obj interface{}) error
- func (r *Response) ExtractAt(path string, obj interface{}) error
- func (r *Response) GetLength(path string) int
- func (r *Response) GetResult(path string) gjson.Result
- func (r *Response) Validate(vs interface{}) error
- func (r *Response) ValidateAt(path string, vs interface{}) error
- type TestServer
- func (ts *TestServer) Delete(path string, data []byte) (*Response, error)
- func (ts *TestServer) Get(path string) (*Response, error)
- func (ts *TestServer) Patch(path string, data []byte) (*Response, error)
- func (ts *TestServer) Post(path string, data []byte) (*Response, error)
- func (ts *TestServer) Put(path string, data []byte) (*Response, error)
- func (ts *TestServer) Request(reqOpts RequestOptions) (*Response, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RequestOptions ¶
RequestOptions are used by the TestServer.Request function can be used with a RequestOptions structure when more advanced request customization is needed. Such as configuring headers.
type Response ¶
type Response struct { StatusCode int Data string Header http.Header // contains filtered or unexported fields }
Response is returend when a request is made against the test server It contains helper methods to validate the resulting JSON and check things like the HTTP status.
func Delete ¶
Delete creates a TestServer for the given celerity.Server and makes a Delete request against it using the TestServer.Delete function.
func Get ¶
Get creates a TestServer for the given celerity.Server and makes a GET request against it using the TestServer.Get function.
func Patch ¶
Patch creates a TestServer for the given celerity.Server and makes a Patch request against it using the TestServer.Patch function.
func Post ¶
Post creates a TestServer for the given celerity.Server and makes a POST request against it using the TestServer.Post function.
func Put ¶
Put creates a TestServer for the given celerity.Server and makes a PUT request against it using the TestServer.Put function.
func Request ¶
func Request(s *celerity.Server, opts RequestOptions) (*Response, error)
Request creates a TestServer for the given celerity.Server and makes a request against it using the TestServer.Request function.
func (*Response) AssertBool ¶
AssertBool checks a boolean value in the returning JSON at a given path.
r := celeritytest.Get(svr, "/foo") if ok, _ := r.AssertBool("data.isAdmin", ); !ok { t.Errrof("admin should be true") }
func (*Response) AssertInt ¶
AssertInt checks an integer value in the returning JSON at a given path.
r := celeritytest.Get(svr, "/foo") if ok, v := r.AssertString("data.age", 19); !ok { t.Errrof("age was not returned correctly: %d", v) }
func (*Response) AssertString ¶
AssertString checks a string value in the returning JSON at a given path.
r := celeritytest.Get(svr, "/foo") if ok, v := r.AssertString("data.firstName", "alice"); !ok { t.Errrof("first name not returned correctly: %s", v) }
func (*Response) ValidateAt ¶
ValidateAt validates a portion of the the response data against a validation structure.
type TestServer ¶
TestServer can be used to make calls against a managed test version of the http server. This is internally used by the Request, Get, and Post package level functions.
func (*TestServer) Delete ¶
func (ts *TestServer) Delete(path string, data []byte) (*Response, error)
Delete makes a DELETE request against the test server. This function is called by the package level Delete function in cases where you want to make a one off request.
func (*TestServer) Get ¶
func (ts *TestServer) Get(path string) (*Response, error)
Get - Makes a GET request against the test server. This function is called by the package level Get function in cases where you want to make a one off request.
func (*TestServer) Patch ¶
func (ts *TestServer) Patch(path string, data []byte) (*Response, error)
Patch makes a PATCH request against the test server. This function is called by the package level Patch function in cases where you want to make a one off request.
func (*TestServer) Post ¶
func (ts *TestServer) Post(path string, data []byte) (*Response, error)
Post makes a POST request against the test server. This function is called by the package level Post function in cases where you want to make a one off request.
func (*TestServer) Put ¶
func (ts *TestServer) Put(path string, data []byte) (*Response, error)
Put makes a PUT request against the test server. This function is called by the package level Put function in cases where you want to make a one off request.
func (*TestServer) Request ¶
func (ts *TestServer) Request(reqOpts RequestOptions) (*Response, error)
Request makes a request against the test server. This function is called by the package level Request function for one off requests. This function can be used for more customization when making requests than the Get and Post functions provide.