Documentation ¶
Overview ¶
Package tester implements a library that makes testing HTTP based API clients easier.
Example test:
func setup() *tester.Tester { return tester.NewTester(map[string]tester.TestCategory{ "users": tester.TestCategory{ Scenarios: map[string]tester.TestScenario{ "create": TestScenario{ Request: TestRequest{ Method: "POST", Path: "/user", Headers: map[string]string{"Content-Type": "application/json"}, Body: strings.NewReader("{\"username\":\"user1\"}"), }, Response: TestResponse{ Code: 200, Headers: map[string]string{"Content-Type": "application/json"}, Body: strings.NewReader("{\"id\":1,\"username\":\"user1\"}"), }, }, }, } }) } func TestUserCreate(t *testing.T) { api := setup() defer api.Close() api.Setup(t, "user", "create") c := NewClient(api.URL) user, err := c.CreateUser("test") if err != nil { t.Fatal(err) } assert.Equal(t, 1, user.ID) assert.Equal(t, "user1", user.Name) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompareRequests ¶
func CompareRequests(t *testing.T, expected *TestRequest, actual *http.Request)
CompareRequests compare TestScenario to an actual HTTP request
func WriteResponse ¶
func WriteResponse(t *testing.T, resp *TestResponse, w http.ResponseWriter)
WriteResponse writes TestResponse to an actual HTTP ResponseWriter
Types ¶
type TestCategory ¶
type TestCategory struct {
Scenarios map[string]TestScenario
}
TestCategory contains the scenarios for particular category
type TestRequest ¶
TestRequest contains expected request from the API client being tested
type TestResponse ¶
TestResponse contains mock response from the API
type TestScenario ¶
type TestScenario struct { Request TestRequest Response TestResponse }
TestScenario contains expected request and mock response
type Tester ¶
type Tester struct { Categories map[string]TestCategory URL string // contains filtered or unexported fields }
Tester provides methods to test a HTTP API client.
func NewTester ¶
func NewTester(categories map[string]TestCategory) *Tester
NewTester creates a new Tester
categories := map[string]tester.TestCategory{ "users": tester.TestCategory{ Scenarios: map[string]tester.TestScenario{ "create": TestScenario{ Request: TestRequest{ Method: "POST", Path: "/user", Headers: map[string]string{"Content-Type": "application/json"}, Body: strings.NewReader("{\"username\":\"user1\"}"), }, Response: TestResponse{ Code: 200, Headers: map[string]string{"Content-Type": "application/json"}, Body: strings.NewReader("{\"id\":1,\"username\":\"user1\"}"), }, }, }, } } api := tester.NewTester(categories)
func (*Tester) Close ¶
func (tester *Tester) Close()
Close stops the HTTP server.
Use defer api.Close() in each test case to ensure the server is closed and resources are released.
Example:
api := tester.NewTester(categories) defer api.Close()
func (*Tester) Do ¶
Do creates a route for the handler function provided.
Example:
offline, err := api.Scenario("user", "offline") if err != nil { t.Fatal(err) } online, err := api.Scenario("user", "online") if err != nil { t.Fatal(err) } scenarios := []*tester.TestScenario{offline, online} current := 0 api.Do(offline.Path, func(w http.ResponseWriter, req *http.Request) { tester.WriteResponse(t, &scenarios[current].Response, w) current++ }) err := c.WaitUserOnline("user1") if err != nil { t.Fatal(err) } assert.Equal(t, 2, current)