Companion libraries
Modules that add assertions and report formats to apitest.
apitest has no dependencies. Features that need one are published as separate modules, so you only add what you use.
JSONPath
github.com/steinfletcher/apitest-jsonpath asserts on parts of JSON responses, and matches JSON bodies sent to mocks.
go get github.com/steinfletcher/apitest-jsonpathSee the JSONPath guide.
CSS selectors
github.com/steinfletcher/apitest-css-selector asserts on HTML responses with CSS selectors, for server rendered pages.
go get github.com/steinfletcher/apitest-css-selectorapitest.Handler(handler).
Get("/products/gopher-plush").
Expect(t).
Status(http.StatusOK).
Assert(selector.TextExists("Gopher plush")).
Assert(selector.FirstTextValue(`h1.title`, "Gopher plush")).
Assert(selector.ContainsTextValue(selector.DataTestID("price"), "£12.99")).
Assert(selector.NthTextValue(1, `.reviews li`, "Soft")).
Assert(selector.Exists(`.reviews`)).
Assert(selector.NotExists(`.out-of-stock`)).
End()| Assertion | Passes when |
|---|---|
selector.TextExists(text) | The text appears anywhere in the document. |
selector.FirstTextValue(sel, text) | The first element matching the selector has the text. |
selector.NthTextValue(n, sel, text) | The nth element matching the selector, counting from zero, has the text. |
selector.ContainsTextValue(sel, text) | An element matching the selector contains the text. |
selector.Exists(sel...), selector.NotExists(sel...) | Elements matching the selectors exist, or do not. |
selector.Selection(sel, func(*goquery.Selection) error) | Your function, given the matching elements, returns nil. |
selector.DataTestID(id) | Builds the selector [data-test-id="id"]. |
PlantUML
github.com/steinfletcher/apitest-plantuml formats reports as PlantUML sequence diagrams, which can be rendered in documentation tools and wikis.
go get github.com/steinfletcher/apitest-plantumlapitest.New("search user").
Report(plantuml.NewFormatter(writer)).
Mocks(getUser, getPreferences).
Handler(handler).
Post("/user/search").
JSON(`{"name": "jon"}`).
Expect(t).
Status(http.StatusOK).
End()DynamoDB
github.com/steinfletcher/apitest-dynamodb records DynamoDB calls made with the AWS SDK, so they appear in sequence diagrams alongside HTTP calls.
Writing your own
Assertions are func(*http.Response, *http.Request) error, mock matchers are func(*http.Request, *apitest.MockRequest) error, and report formatters implement ReportFormatter. See custom assertions, custom matchers and custom reports.