Documentation ¶
Index ¶
- func NewTestAnyClient[T babyapi.Resource](t *testing.T, api *babyapi.API[T]) (*babyapi.Client[*babyapi.AnyResource], func())
- func NewTestClient[T babyapi.Resource](t *testing.T, api *babyapi.API[T]) (*babyapi.Client[T], func())
- func RunTableTest[T babyapi.Resource](t *testing.T, api *babyapi.API[T], tests []TestCase[*babyapi.AnyResource])
- func TestRequest[T babyapi.Resource](t *testing.T, api *babyapi.API[T], r *http.Request) *httptest.ResponseRecorder
- func TestServe[T babyapi.Resource](t *testing.T, api *babyapi.API[T]) (string, func())
- func TestWithParentRoute[T, P babyapi.Resource](t *testing.T, api *babyapi.API[T], parent P, parentName, parentBasePath string, ...) *httptest.ResponseRecorder
- type ExpectedResponse
- type PreviousResponseGetter
- type RequestFuncTest
- type RequestTest
- type Response
- type Test
- type TestCase
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewTestAnyClient ¶
func NewTestAnyClient[T babyapi.Resource](t *testing.T, api *babyapi.API[T]) (*babyapi.Client[*babyapi.AnyResource], func())
NewTestAnyClient runs the API using TestServe and returns a Client with the correct base URL. It uses AnyClient for an AnyResource so it is compatible with table-driven tests
func NewTestClient ¶
func NewTestClient[T babyapi.Resource](t *testing.T, api *babyapi.API[T]) (*babyapi.Client[T], func())
NewTestClient runs the API using TestServe and returns a Client with the correct base URL
func RunTableTest ¶
func RunTableTest[T babyapi.Resource](t *testing.T, api *babyapi.API[T], tests []TestCase[*babyapi.AnyResource])
RunTableTest will start the provided API and execute all provided tests in-order. This allows the usage of a PreviousResponseGetter in each test to access data from previous tests. The API's ClientMap is used to execute tests with child clients if the test uses ClientName field
func TestRequest ¶
func TestRequest[T babyapi.Resource](t *testing.T, api *babyapi.API[T], r *http.Request) *httptest.ResponseRecorder
TestRequest is meant to be used in external tests to automatically handle setting up routes and using httptest
func TestServe ¶
Test is meant to be used in external tests to automatically handle setting up routes and using httptest
func TestWithParentRoute ¶
func TestWithParentRoute[T, P babyapi.Resource](t *testing.T, api *babyapi.API[T], parent P, parentName, parentBasePath string, r *http.Request) *httptest.ResponseRecorder
TestWithParentRoute allows testing a child API independently with a pre-configured parent resource in the context to mock a middleware
Types ¶
type ExpectedResponse ¶
type ExpectedResponse struct { // NoBody sets the expectation that the response will have an empty body. This is used because leaving Body // empty will just skip the test, not assert the response is empty NoBody bool // Body is the expected response body string Body string // BodyRegexp allows comparing a request body by regex BodyRegexp string // Status is the expected HTTP response code Status int // Error is an expected error string to be returned by the client Error string }
ExpectedResponse sets up the expectations when running a test
type PreviousResponseGetter ¶
type PreviousResponseGetter func(testName string) *Response[*babyapi.AnyResource]
PreviousResponseGetter is used to get the output of previous tests in a TableTest
type RequestFuncTest ¶ added in v0.5.0
type RequestFuncTest[T babyapi.Resource] func(getResponse PreviousResponseGetter, address string) *http.Request
RequestFuncTest is used to create an *http.Request from the provided address and create a response for assertions
func (RequestFuncTest[T]) Run ¶ added in v0.5.0
func (tt RequestFuncTest[T]) Run(t *testing.T, client *babyapi.Client[T], getResponse PreviousResponseGetter) (*Response[T], error)
type RequestTest ¶ added in v0.5.0
type RequestTest[T babyapi.Resource] struct { // HTTP request method/verb Method string // RawQuery is the query params RawQuery string // RawQueryFunc returns query params from a function which can access previous test responses RawQueryFunc func(getResponse PreviousResponseGetter) string // ID is the resource ID used in the request path ID string // IDFunc returns the resource ID from a function which can access previous test responses IDFunc func(getResponse PreviousResponseGetter) string // Body is the request body as a string Body string // BodyFunc returns request body from a function which can access previous test responses BodyFunc func(getResponse PreviousResponseGetter) string // ParentIDs is a list of parent resource IDs, in order ParentIDs []string // ParentIDsFunc returns parent resource IDs from a function which can access previous test responses ParentIDsFunc func(getResponse PreviousResponseGetter) []string }
RequestTest contains the necessary details to make a test request to the API. The Func fields allow dynamically creating parts of the request. When used in a TableTest, a PreviousResponseGetter is provided so you can get IDs from previous responses or use other details. When not used in a table test, this will always be nil
func (RequestTest[T]) Run ¶ added in v0.5.0
func (tt RequestTest[T]) Run(t *testing.T, client *babyapi.Client[T], getResponse PreviousResponseGetter) (*Response[T], error)
type Response ¶ added in v0.5.0
type Response[T babyapi.Resource] struct { *babyapi.Response[T] GetAllResponse *babyapi.Response[*babyapi.ResourceList[T]] CLIOut string }
Response wraps a *babyapi.Response and a *ResourceList response to enable GetAll/List endpoints GetAll/List requests can be made using babytest.MethodGetAll and the response will be in this GetAllResponse field
type Test ¶
type Test[T babyapi.Resource] interface { Run(t *testing.T, client *babyapi.Client[T], getResponse PreviousResponseGetter) (*Response[T], error) }
Test is an interface that allows executing different types of tests before running assertions
type TestCase ¶ added in v0.5.0
type TestCase[T babyapi.Resource] struct { Name string // Test is the runnable test to execute before assertions Test Test[T] // ClientName is the name of the API, or child API, which should be used to execute this test. Leave empty // to use the default provided API Client. When set, CreateClientMap is used to create a map of child clients // This is only available for TableTest because it has the ClientMap ClientName string // Assert allows setting a function for custom assertions after making a request. It is part of the Test instead // of the ExpectedResponse because it needs the type parameter Assert func(*Response[T]) // Expected response to compare ExpectedResponse }
TestCase is a single test step that executes the provided ClientRequest or RequestFunc and compares to the ExpectedResponse