Documentation
¶
Overview ¶
Package apitesting contains integration testing framework functions
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APITest ¶
type APITest struct { // GetHTTPHandler returns an http.Handler (e.g. *gin.Engine) which will be bootstrapped with httptest // Only one of GetHTTPHandler and APIEndpointURL should be provided GetHTTPHandler func(ctx context.Context, t *testing.T) http.Handler // APIEndpointURL is the HTTP endpoint to send the HTTP calls to // Only one of GetHTTPHandler and APIEndpointURL should be provided APIEndpointURL string // // A function that will run once before any tests execute. SetupTest func(ctx context.Context, t *testing.T) // A function that will run once after all tests have run (optional) TeardownTest func(ctx context.Context, t *testing.T) // A function that will run before each test case (i.e. before each element in the []ApiTestCase passed to RunTests) SetupTestCase func(ctx context.Context, t *testing.T, tc APITestCase) // A function that will run after each test case TeardownTestCase func(ctx context.Context, t *testing.T, tc APITestCase) // A Context object that can be used to maintain context between tests/test cases Ctx context.Context // contains filtered or unexported fields }
APITest is a structure used to define some environmental aspects of a test run (like Setup/Teardown functions) It also holds some internal context. Usage: using httptest as API endpoint
apitest := apitesting.APITest{ GetHTTPHandler: func(ctx context.Context, t *testing.T) http.Handler { return initializer.Init() // assumes Init() returns http.Hanlder },
Usage: using actual HTTP endpoint
apitest := apitesting.APITest{ APIEndpointURL: "http://some.host.com:8000" Then you created test cases via the ApiTestCase structure: tests := []apitesting.APITestCase{ { Name: "Create a product with invalid sstoken", ReqType: "POST", ResourcePath: func(ctx context.Context, t *testing.T) string { return "/someurl" }, PopulateHeaders: func(ctx context.Context, t *testing.T) map[string]string { return map[string]string{"Authorization": "a_token"} }, PopulateJSON: func(ctx context.Context, t *testing.T) interface{} { return models.testdata }, ExpHTTPStatus: http.StatusOK, BeforeRequest: func(ctx context.Context, t *testing.T, e *httpexpect.Expect) { // do something before the request is sent }, ValidateResponse: func(t *testing.T, resp *httpexpect.Response) context.Context { // validate the response object }, AfterValidate: func(ctx context.Context, t *testing.T, e *httpexpect.Expect) { // do something just before this testcase is torn down }, } Then you run the test cases: apitest.RunTests(t, tests)
type APITestCase ¶
type APITestCase struct { // Description of the test case Name string // HTTP Request type (e.g. "POST", "GET") ReqType string // A function that returns the path to the resource to call (e.g. /v1/product or /v1/product/<uuid> etc) ResourcePath func(ctx context.Context, t *testing.T) string // A function that will be run immediately before the testcase request is sent BeforeRequest func(ctx context.Context, t *testing.T, e *httpexpect.Expect) // A function that returns the request headers (will be sent via "WithHeaders") PopulateHeaders func(ctx context.Context, t *testing.T) map[string]string // A function that returns the request query parameters (will be sent via "WithQuery") PopulateQueryParams func(ctx context.Context, t *testing.T) map[string]string // A function that returns the request cookies (will be sent via "WithCookies") PopulateCookies func(ctx context.Context, t *testing.T) map[string]string // Expected HTTP status code from the API call ExpHTTPStatus int // A function that returns a struct to send to the request via "WithJSON". Leave out if you don't need a JSON body PopulateJSON func(ctx context.Context, t *testing.T) interface{} // A function that validates the response. This should call t.Error as appropriate. Leave out if you don't need to validate. ValidateResponse func(ctx context.Context, t *testing.T, resp *httpexpect.Response) context.Context // A function that will be run immediately after the testcase validation AfterValidate func(ctx context.Context, t *testing.T, e *httpexpect.Expect) }
APITestCase is a structure used to define the specific test cases to be run via RunTests.
Click to show internal directories.
Click to hide internal directories.