tester

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2020 License: MIT Imports: 9 Imported by: 0

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

type TestRequest struct {
	Method  string
	Path    string
	Headers map[string]string
	Body    io.ReadSeeker
}

TestRequest contains expected request from the API client being tested

type TestResponse

type TestResponse struct {
	Code    int
	Status  string
	Headers map[string]string
	Body    io.ReadSeeker
}

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

func (tester *Tester) Do(pattern string, handler func(http.ResponseWriter, *http.Request))

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)

func (*Tester) Scenario

func (tester *Tester) Scenario(category string, scenario string) (*TestScenario, error)

Scenario returns a configured TestScenario.

func (*Tester) Setup

func (tester *Tester) Setup(t *testing.T, category string, scenario string)

Setup configures a route for the TestScenario.

Example:

api.Setup(t, "user", "online")

err := c.WaitUserOnline("user1")
if err != nil {
	t.Fatal(err)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL