Documentation ¶
Index ¶
- func EnsureCompletion(t *testing.T, wg *sync.WaitGroup, options ...EnsureOption) bool
- func ExpectCalled(t TestingT, ctx *gin.Engine, path string, options ...ExpectOption) *sync.WaitGroup
- func PrepareRequest(t TestingT, options ...RequestOption) (*gin.Context, *httptest.ResponseRecorder)
- func Response(t TestingT, result any, code int, res *http.Response) bool
- type EnsureOption
- type ExpectOption
- type RequestOption
- func WithBody(data []byte) RequestOption
- func WithHeaders(headers http.Header) RequestOption
- func WithJsonBody(t TestingT, object any) RequestOption
- func WithMethod(method string) RequestOption
- func WithQueryParams(queryParams map[string]any) RequestOption
- func WithUrl(reqUrl string) RequestOption
- func WithUrlParams(parameters map[string]any) RequestOption
- type TestingT
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnsureCompletion ¶
EnsureCompletion ensures that the waitgroup completes within a specified duration or else fails
func ExpectCalled ¶
func ExpectCalled(t TestingT, ctx *gin.Engine, path string, options ...ExpectOption) *sync.WaitGroup
ExpectCalled can be used on a gin endpoint to express an expectation that the endpoint will be called some time in the future. In combination with a test can wait for this expectation to be true or fail after some predetermined amount of time
Example (WithVarargs) ¶
arguments can configure the expected amount of times and endpoint is called or re-use an existing expectation
t := new(testing.T) ginContext := gin.Default() // create expectation expectation := ExpectCalled(t, ginContext, "/hello-world", TimesCalled(2)) expectation = ExpectCalled(t, ginContext, "/other-path", Expectation(expectation)) // create endpoints on ginContext for _, endpoint := range []string{"/hello-world", "/other-path"} { ginContext.GET(endpoint, func(context *gin.Context) { context.Status(http.StatusOK) }) } // create webserver ts := httptest.NewServer(ginContext) // Send request to webserver path _, _ = http.Get(fmt.Sprintf("%s/hello-world", ts.URL)) _, _ = http.Get(fmt.Sprintf("%s/hello-world", ts.URL)) _, _ = http.Get(fmt.Sprintf("%s/other-path", ts.URL)) // Wait for expectation in bounded time if ok := EnsureCompletion(t, expectation); !ok { // do something }
Output:
Example (WithoutVarargs) ¶
without arguments expect called assumes that the endpoint is only called once and creates a new expectation
t := new(testing.T) ginContext := gin.Default() // create expectation expectation := ExpectCalled(t, ginContext, "/hello-world") // create endpoints on ginContext ginContext.GET("/hello-world", func(context *gin.Context) { context.Status(http.StatusOK) }) // create webserver ts := httptest.NewServer(ginContext) // Send request to webserver path _, _ = http.Get(fmt.Sprintf("%s/hello-world", ts.URL)) // Wait for expectation in bounded time if ok := EnsureCompletion(t, expectation); !ok { // do something }
Output:
func PrepareRequest ¶
func PrepareRequest(t TestingT, options ...RequestOption) (*gin.Context, *httptest.ResponseRecorder)
PrepareRequest Formulate a request with optional properties. This returns a *gin.Context which can be used in controller unit-tests. Use the returned *httptest.ResponseRecorder to perform assertions on the response.
Example ¶
// Arrange t := new(mockT) myController := &MyController{} context, writer := PrepareRequest(t, WithMethod(http.MethodGet), WithUrl("https://ing.net"), WithJsonBody(t, MyObject{ID: 5}), WithUrlParams(map[string]any{"one": "two", "three": []string{"four", "five"}}), WithQueryParams(map[string]any{"id": map[string]any{"a": "b"}}), ) // Act myController.Post(context) // Assert assert.Equal(t, "...", writer.Body.String())
Output:
func Response ¶
Response checks the status code and unmarshalls it to the given type. If you don't care about the response, Use nil. If the return code is 204 or 304, the response body is not converted.
Example ¶
// Arrange t := new(mockT) expected := []MyObject{{ID: 5}} myController := &MyController{} context, writer := PrepareRequest(t) defer writer.Result().Body.Close() // Act myController.Get(context) var result []MyObject // Assert ok := Response(t, &result, http.StatusOK, writer.Result()) if assert.True(t, ok) { assert.Equal(t, expected[0].ID, result[0].ID) }
Output:
Types ¶
type EnsureOption ¶
type EnsureOption func(*ensureConfig)
EnsureOption allows various options to be supplied to EnsureCompletion
func WithTimeout ¶
func WithTimeout(timeout time.Duration) EnsureOption
WithTimeout is used to set a timeout for EnsureCompletion
type ExpectOption ¶
type ExpectOption func(*calledConfig)
ExpectOption allows various options to be supplied to Expect* functions
func Expectation ¶
func Expectation(expectation *sync.WaitGroup) ExpectOption
Expectation is used to have a global wait group to wait for when asserting multiple calls made
func TimesCalled ¶
func TimesCalled(times int) ExpectOption
TimesCalled is used to expect an invocation an X amount of times
type RequestOption ¶
type RequestOption func(*requestConfig)
RequestOption are functions used in PrepareRequest to configure a request using the Functional Option pattern.
func WithBody ¶
func WithBody(data []byte) RequestOption
WithBody allows you to define a custom body for the request
func WithHeaders ¶ added in v0.0.1
func WithHeaders(headers http.Header) RequestOption
WithHeaders specifies the headers of the request
func WithJsonBody ¶
func WithJsonBody(t TestingT, object any) RequestOption
WithJsonBody specifies the request body using json.Marshal, will report an error on marshal failure
func WithMethod ¶
func WithMethod(method string) RequestOption
WithMethod specifies the method to use, defaults to Get
func WithQueryParams ¶
func WithQueryParams(queryParams map[string]any) RequestOption
WithQueryParams adds query parameters to the request. The value can be either: - string - []string - fmt.Stringer (anything with a String() method) - map[string]any
func WithUrl ¶
func WithUrl(reqUrl string) RequestOption
WithUrl specifies the url to use, defaults to https://example.com
func WithUrlParams ¶
func WithUrlParams(parameters map[string]any) RequestOption
WithUrlParams adds url parameters to the request. The value can be either: - string - []string - fmt.Stringer (anything with a String() method)