test

package
v0.0.0-...-63db3c6 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2021 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(tb testing.TB, condition bool, msg string, v ...interface{})

Assert fails the test if the condition is false.

func Equals

func Equals(tb testing.TB, exp, act interface{})

Equals fails the test if exp is not equal to act.

func Includes

func Includes(tb testing.TB, exp string, act ...string)

Includes fails if expected string is NOT included in the actual string

func IncludesI

func IncludesI(tb testing.TB, exp string, act ...string)

IncludesI fails if expected string is NOT included in the actuall string (ignore case)

func IncludesMap

func IncludesMap(tb testing.TB, exp, act interface{})

IncludesMap fails if all of expected map entries are NOT included in the actuall map

func IncludesSlice

func IncludesSlice(tb testing.TB, exp, act interface{})

IncludesSlice fails if all of expected items is NOT included in the actual slice

func NewMockServer

func NewMockServer(rec MockRecorder, procedures ...MockServerProcedure) *httptest.Server

NewMockServer return a mock HTTP server to test requests

func Nil

func Nil(tb testing.TB, something interface{})

Nil fails the test if something is NOT nil.

func NotIncludes

func NotIncludes(tb testing.TB, exp string, act ...string)

NotIncludes fails if expected string is included in the actual string

func NotNil

func NotNil(tb testing.TB, anything interface{})

NotNil fails the test if anything is nil.

func NotZero

func NotZero(tb testing.TB, anything interface{})

NotZero fails the test if anything is NOT nil.

func OK

func OK(tb testing.TB, err error)

OK fails the test if an err is not nil.

func Zero

func Zero(tb testing.TB, anything interface{})

Zero fails the test if anything is NOT nil.

Types

type MockAssertion

type MockAssertion struct {
	// contains filtered or unexported fields
}

MockAssertion represents a common assertion for requests

func (*MockAssertion) Body

func (m *MockAssertion) Body(uri, method string) [][]byte

Body returns request body

Example
package main

import (
	"bytes"
	"fmt"
	"net/http"

	"git.sr.ht/~ewintr/go-kit/test"
)

func main() {
	var record test.MockAssertion
	uri := "/"

	server := test.NewMockServer(&record, test.MockServerProcedure{
		URI:        uri,
		HTTPMethod: http.MethodPost,
	})

	http.Post(server.URL, "text/plain", bytes.NewBufferString("hi there"))

	for _, b := range record.Body(uri, http.MethodPost) {
		fmt.Println(string(b))
	}
}
Output:

hi there

func (*MockAssertion) Headers

func (m *MockAssertion) Headers(uri, method string) []http.Header

Headers returns a slice of request headers

Example
package main

import (
	"fmt"
	"net/http"

	"git.sr.ht/~ewintr/go-kit/test"
)

func main() {
	var record test.MockAssertion
	uri := "/"

	server := test.NewMockServer(&record, test.MockServerProcedure{
		URI:        uri,
		HTTPMethod: http.MethodPost,
	})

	http.Post(server.URL, "application/json", nil)

	fmt.Println(record.Headers(uri, http.MethodPost))
}
Output:

[map[Content-Type:[application/json]]]

func (*MockAssertion) Hits

func (m *MockAssertion) Hits(uri, method string) int

Hits returns the number of hits for a uri and method

Example
package main

import (
	"fmt"
	"net/http"

	"git.sr.ht/~ewintr/go-kit/test"
)

func main() {
	var record test.MockAssertion
	uri := "/"

	server := test.NewMockServer(&record, test.MockServerProcedure{
		URI:        uri,
		HTTPMethod: http.MethodGet,
	})

	http.Get(server.URL)

	fmt.Println(record.Hits(uri, http.MethodGet))
}
Output:

1

func (*MockAssertion) Record

func (m *MockAssertion) Record(r *http.Request)

Record records request hit information

func (*MockAssertion) Reset

func (m *MockAssertion) Reset() error

Reset sets all unexpected properties to their zero value

type MockRecorder

type MockRecorder interface {
	Record(r *http.Request)
}

MockRecorder provides a way to record request information from every successful request.

type MockResponse

type MockResponse struct {
	StatusCode int
	Headers    http.Header
	Body       []byte
}

MockResponse represents a response for the mock server to serve

type MockServerProcedure

type MockServerProcedure struct {
	URI        string
	HTTPMethod string
	Response   MockResponse
}

Jump to

Keyboard shortcuts

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