celeritytest

package
v0.0.0-...-a613a2c Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2018 License: MIT Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RequestOptions

type RequestOptions struct {
	Method string
	Path   string
	Header http.Header
	Data   []byte
}

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

func Delete(s *celerity.Server, path string, data []byte) (*Response, error)

Delete creates a TestServer for the given celerity.Server and makes a Delete request against it using the TestServer.Delete function.

func Get

func Get(s *celerity.Server, path string) (*Response, error)

Get creates a TestServer for the given celerity.Server and makes a GET request against it using the TestServer.Get function.

func Patch

func Patch(s *celerity.Server, path string, data []byte) (*Response, error)

Patch creates a TestServer for the given celerity.Server and makes a Patch request against it using the TestServer.Patch function.

func Post

func Post(s *celerity.Server, path string, data []byte) (*Response, error)

Post creates a TestServer for the given celerity.Server and makes a POST request against it using the TestServer.Post function.

func Put

func Put(s *celerity.Server, path string, data []byte) (*Response, error)

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

func (r *Response) AssertBool(path string, value bool) (bool, bool)

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

func (r *Response) AssertInt(path string, value int) (bool, int)

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

func (r *Response) AssertString(path, value string) (bool, string)

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) Exists

func (r *Response) Exists(path string) bool

Exists checks if a value exists at a given JSON path

func (*Response) Extract

func (r *Response) Extract(obj interface{}) error

Extract unmarshals the JSON into a struct

func (*Response) ExtractAt

func (r *Response) ExtractAt(path string, obj interface{}) error

ExtractAt Unmarshals JSON at a path into a struct.

func (*Response) GetLength

func (r *Response) GetLength(path string) int

GetLength returns the length of an array in a at a given JSON path.

func (*Response) GetResult

func (r *Response) GetResult(path string) gjson.Result

GetResult returns a result object at a given path.

func (*Response) Validate

func (r *Response) Validate(vs interface{}) error

Validate validates the response data against a validation structure

func (*Response) ValidateAt

func (r *Response) ValidateAt(path string, vs interface{}) error

ValidateAt validates a portion of the the response data against a validation structure.

type TestServer

type TestServer struct {
	Server *celerity.Server
}

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.

Jump to

Keyboard shortcuts

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