test

package
v0.0.0-...-c194ff1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2017 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Provides helper to test http handlers.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	//Handler that will be executed for test cases.
	Service http.HandlerFunc
	//Test context
	T *testing.T
)

Functions

func Error

func Error(t *testing.T, a ...Assert)

Test each assert and error on each failed. Using only plain == to compare expected and actual values. Error message has format: "Expected [%expected] %description but got [%actual]".

Example
package main

import (
	"errors"
	"github.com/dooman87/kolibri/test"
	"testing"
)

func main() {
	var t = new(testing.T)
	test.Error(t,
		//Error message will be: Expected [1] apples but got [6]
		test.Equal(1, 6, "apples"),
		//Error message will be: Expected price to be not equal to 2.99
		test.NotEqual(2.99, 2.99, "price"),
		//Error message will be: Expected error to be nil
		test.Nil(errors.New("ERROR"), "error"),
		//Error message will be: Expected student name to not be nil
		test.NotNil(nil, "student name"),
	)
}
Output:

func NewRequest

func NewRequest(method string, url string, body io.Reader) *http.Request

Wraps http.NewRequest to process error case. If http.NewRequest returns error then execution interrupts with T.Fatal()

func RunRequests

func RunRequests(testCases []TestCase)

Runs all test cases. Will fail if test.T is nil.

Example
package main

import (
	"github.com/dooman87/kolibri/test"
	"net/http"
	"net/http/httptest"
	"testing"
)

func main() {
	//This is service that we want to test. It should be a type of http.HandlerFunc.
	test.Service = func(resp http.ResponseWriter, req *http.Request) {
		resp.Write([]byte("OK"))
	}
	test.T = &testing.T{}

	//Creating set of tests that we want to run.
	//Each test is a struct that contains endpoint, expected response status, description, optional handler.
	testCases := []test.TestCase{
		{
			Url:         "http://localhost:8080",
			Description: "Should return 200",
		},
		{
			Url:         "http://localhost:8080",
			Description: "Should return OK in body",
			Handler: func(w *httptest.ResponseRecorder, t *testing.T) {
				if w.Body.String() != "OK" {
					t.Errorf("Expected %s but got %s", "OK", w.Body.String())
				}
			},
		},
		{
			Request:     test.NewRequest("GET", "http://localhost:8080", nil),
			Description: "Should return 200",
		},
	}

	//Running all test cases.
	test.RunRequests(testCases)
}
Output:

Types

type Assert

type Assert interface {
	Exec(t *testing.T)
}

func Equal

func Equal(expected interface{}, actual interface{}, description string) Assert

Creates new equal assert object.

func Nil

func Nil(actual interface{}, description string) Assert

Creates new nil assert object.

func NotEqual

func NotEqual(notExpected interface{}, actual interface{}, description string) Assert

Creates new non-equal assert object.

func NotNil

func NotNil(actual interface{}, description string) Assert

Creates new not nil assert object.

type ResponseHandler

type ResponseHandler func(w *httptest.ResponseRecorder, t *testing.T)

type TestCase

type TestCase struct {
	//You can use this field in case you need simple GET request
	//or pass request using Request field.
	Url string
	//Request to execute. If nil then Url will be used and GET request will be executed.
	Request *http.Request
	//Expected respose code. If not passed then will check for 200.
	ExpectedCode int
	Description  string
	//Optional handler that will be called at the end of the test case.
	Handler ResponseHandler
}

Describes test case. Each test case will be executed as http request against Service handler.

Jump to

Keyboard shortcuts

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