apitest

package module
v0.0.0-...-ebf5a7e Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2018 License: Apache-2.0 Imports: 19 Imported by: 0

README

* Why apitest
API is our promise, so we test API. The API is stable, so testing the API is the least expensive.

* Usage
** init apitest
#+BEGIN_SRC golang
import (
  apitest "github.com/zzyongx/go-apitest"
)

var api *apitest.Api
func init() {
  api = apitest.NewHttpTest("http://127.0.0.1:8080")
}
#+END_SRC

** create apitest
#+BEGIN_SRC golang
import (
  "testing"
)

func TestApiXYZ(t *testing.T) {
	api.Get("/static/config.json").Expect(t).
		Status().Eq(200).
		Headers().Eq("Content-Type", "application/json; charset=utf-8").
		EqAny("Content-Type", "application/json", "application/json; charset=utf-8").
		Exist("Content-Length").ExistAny("NotFound", "Content-Length").
		Json().Eq("$.user", "zzyongx")

	api.Post("/api/login").Form("user", "zzyongx").Form("password", "123456").Expect(t).
		Json().Eq("$.code", 403)

	var token string
	oneYearAfter := time.Now().Add(365 * 24 * time.Hour)
	api.Post("/api/login").Form("user", "zzyongx").Form("password", "123465").Expect(t).
		Cookies("user").Value("zzyongx").Domain("example.com").Expires(time.Now(), oneYearAfter).
		Cookies("token").Value("90#@xw").Domain("example.com").Expires(time.Now(), oneYearAfter).StoreValue(&token).
		Json().Eq("$.code", 0)

	if token != "90#@xw" {
		t.Fatalf("cookie storevalue bug, expect 90#@xw, got %s", token)
	}
}
#+END_SRC

** run apitest
~go test -failfast~

* More example
see [[./t/t_test.go][http]]
[[https://godoc.org/github.com/zzyongx/go-apitest][apidoc]]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustInterfaceToString

func MustInterfaceToString(v interface{}) string

Types

type Api

type Api struct {
	Endpoint string
	Timeout  time.Duration
	Headers  http.Header
	Cookies  map[string]string
	Request  *ApiRequest
}

func NewHttpTest

func NewHttpTest(endpoint string) *Api

func (*Api) Cookie

func (this *Api) Cookie(name, value string) *Api

func (*Api) Delete

func (this *Api) Delete(path string, params ...interface{}) *Api

func (*Api) Expect

func (this *Api) Expect(t *testing.T) *ApiExpect

func (*Api) Form

func (this *Api) Form(key string, value interface{}) *Api

func (*Api) Get

func (this *Api) Get(path string, params ...interface{}) *Api

func (*Api) Header

func (this *Api) Header(name, value string) *Api

func (*Api) Json

func (this *Api) Json(json string) *Api

func (*Api) Param

func (this *Api) Param(key string, value interface{}) *Api

func (*Api) Post

func (this *Api) Post(path string, params ...interface{}) *Api

func (*Api) Put

func (this *Api) Put(path string, params ...interface{}) *Api

type ApiExpect

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

func (*ApiExpect) Cookies

func (this *ApiExpect) Cookies(name string) *CookiesExpect

func (*ApiExpect) Fatalf

func (this *ApiExpect) Fatalf(format string, args ...interface{})

func (*ApiExpect) Headers

func (this *ApiExpect) Headers() *HeadersExpect

func (*ApiExpect) Json

func (this *ApiExpect) Json() *JsonExpect

func (*ApiExpect) Plain

func (this *ApiExpect) Plain() *PlainExpect

func (*ApiExpect) Status

func (this *ApiExpect) Status() *StatusExpect

type ApiRequest

type ApiRequest struct {
	Timeout    time.Duration
	Method     string
	Path       string
	PathParams []string
	Params     url.Values

	Headers http.Header
	Cookies map[string]string

	Forms url.Values
	Json  string
}

func NewApiRequest

func NewApiRequest() *ApiRequest

type CookiesExpect

type CookiesExpect struct {
	*ApiExpect
	// contains filtered or unexported fields
}

func (*CookiesExpect) Domain

func (this *CookiesExpect) Domain(domain string) *CookiesExpect

func (*CookiesExpect) Expires

func (this *CookiesExpect) Expires(startAt, endAt time.Time) *CookiesExpect

func (*CookiesExpect) StoreValue

func (this *CookiesExpect) StoreValue(value *string) *CookiesExpect

func (*CookiesExpect) Test

func (this *CookiesExpect) Test(test func(t *testing.T, cookies []*http.Cookie)) *CookiesExpect

func (*CookiesExpect) Value

func (this *CookiesExpect) Value(value string) *CookiesExpect

type Db

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

func MustNewDbTest

func MustNewDbTest(cnf *IniCnf, keys ...string) *Db

func NewDbTest

func NewDbTest(cnf *IniCnf, keys ...string) (*Db, error)

func (*Db) Exec

func (this *Db) Exec(name string, sql string, params ...interface{}) *Db

func (*Db) Expect

func (this *Db) Expect(t *testing.T) *DbExpect

func (*Db) Query

func (this *Db) Query(name string, sql string, params ...interface{}) *Db

type DbExpect

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

func (*DbExpect) Eq

func (this *DbExpect) Eq(field string, value interface{}) *DbExpect

func (*DbExpect) Fatalf

func (this *DbExpect) Fatalf(format string, args ...interface{})

func (*DbExpect) RowNumEq

func (this *DbExpect) RowNumEq(num int) *DbExpect

func (*DbExpect) RowNumGe

func (this *DbExpect) RowNumGe(num int) *DbExpect

func (*DbExpect) RowNumGt

func (this *DbExpect) RowNumGt(num int) *DbExpect

func (*DbExpect) Test

func (this *DbExpect) Test(test func(t *testing.T, rows []*DbRow)) *DbExpect

type DbRequest

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

func NewDbReqest

func NewDbReqest(op string, name string, db *sql.DB, sql string, params []interface{}) *DbRequest

type DbRow

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

func NewDbRow

func NewDbRow(fields []string, values []interface{}) *DbRow

func (*DbRow) GetFloat

func (this *DbRow) GetFloat(f string) (float64, error)

func (*DbRow) GetInt

func (this *DbRow) GetInt(f string) (int64, error)

func (*DbRow) GetString

func (this *DbRow) GetString(f string) (string, error)

func (*DbRow) MustGetFloat

func (this *DbRow) MustGetFloat(f string) float64

func (*DbRow) MustGetInt

func (this *DbRow) MustGetInt(f string) int64

func (*DbRow) MustGetString

func (this *DbRow) MustGetString(f string) string

type HeadersExpect

type HeadersExpect struct {
	*ApiExpect
	// contains filtered or unexported fields
}

func (*HeadersExpect) Eq

func (this *HeadersExpect) Eq(name, v string) *HeadersExpect

func (*HeadersExpect) EqAny

func (this *HeadersExpect) EqAny(name string, vs ...string) *HeadersExpect

func (*HeadersExpect) Exist

func (this *HeadersExpect) Exist(name string) *HeadersExpect

func (*HeadersExpect) ExistAny

func (this *HeadersExpect) ExistAny(names ...string) *HeadersExpect

func (*HeadersExpect) NotExist

func (this *HeadersExpect) NotExist(name string) *HeadersExpect

type IniCnf

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

func MustNewIniCnf

func MustNewIniCnf(file string) *IniCnf

func NewIniCnf

func NewIniCnf(file string) (*IniCnf, error)

func (*IniCnf) GetString

func (this *IniCnf) GetString(key string) string

type InterfaceMatchFunc

type InterfaceMatchFunc func(interface{}, interface{}) bool

type JsonExpect

type JsonExpect struct {
	*ApiExpect
	// contains filtered or unexported fields
}

func (*JsonExpect) Contains

func (this *JsonExpect) Contains(jpath string, value interface{}) *JsonExpect

func (*JsonExpect) ContainsString

func (this *JsonExpect) ContainsString(jpath string, value string) *JsonExpect

func (*JsonExpect) Eq

func (this *JsonExpect) Eq(jpath string, value interface{}) *JsonExpect

func (*JsonExpect) EqAnyFloat

func (this *JsonExpect) EqAnyFloat(jpath string, values ...float64) *JsonExpect

func (*JsonExpect) EqAnyInt

func (this *JsonExpect) EqAnyInt(jpath string, values ...int64) *JsonExpect

func (*JsonExpect) EqAnyString

func (this *JsonExpect) EqAnyString(jpath string, values ...string) *JsonExpect

func (*JsonExpect) FloatContains

func (this *JsonExpect) FloatContains(jpath string, value float64) *JsonExpect

func (*JsonExpect) FloatOnlyContains

func (this *JsonExpect) FloatOnlyContains(jpath string, value float64) *JsonExpect

func (*JsonExpect) Ge

func (this *JsonExpect) Ge(jpath string, value int64) *JsonExpect

func (*JsonExpect) Gt

func (this *JsonExpect) Gt(jpath string, value int64) *JsonExpect

func (*JsonExpect) IntContains

func (this *JsonExpect) IntContains(jpath string, value int64) *JsonExpect

func (*JsonExpect) IntOnlyContains

func (this *JsonExpect) IntOnlyContains(jpath string, value int64) *JsonExpect

func (*JsonExpect) IsNull

func (this *JsonExpect) IsNull(jpath string) *JsonExpect

func (*JsonExpect) Le

func (this *JsonExpect) Le(jpath string, value int64) *JsonExpect

func (*JsonExpect) Lt

func (this *JsonExpect) Lt(jpath string, value int64) *JsonExpect

func (*JsonExpect) Match

func (this *JsonExpect) Match(jpath string, matchFunc InterfaceMatchFunc, value interface{}) *JsonExpect

func (*JsonExpect) MatchAnyString

func (this *JsonExpect) MatchAnyString(jpath string, matchFunc StringMatchFunc, values ...string) *JsonExpect

func (*JsonExpect) MustJsonPahAsFloatArray

func (this *JsonExpect) MustJsonPahAsFloatArray(jpath string) []float64

func (*JsonExpect) MustJsonPahAsInt

func (this *JsonExpect) MustJsonPahAsInt(jpath string) int64

func (*JsonExpect) MustJsonPahAsIntArray

func (this *JsonExpect) MustJsonPahAsIntArray(jpath string) []int64

func (*JsonExpect) MustJsonPahAsStringArray

func (this *JsonExpect) MustJsonPahAsStringArray(jpath string) []string

func (*JsonExpect) MustJsonPathLookup

func (this *JsonExpect) MustJsonPathLookup(jpath string) interface{}

func (*JsonExpect) NotNull

func (this *JsonExpect) NotNull(jpath string) *JsonExpect

func (*JsonExpect) OnlyContains

func (this *JsonExpect) OnlyContains(jpath string, value interface{}) *JsonExpect

func (*JsonExpect) StoreFloat

func (this *JsonExpect) StoreFloat(jpath string, ret *float64) *JsonExpect

func (*JsonExpect) StoreInt

func (this *JsonExpect) StoreInt(jpath string, ret *int64) *JsonExpect

func (*JsonExpect) StoreString

func (this *JsonExpect) StoreString(jpath string, ret *string) *JsonExpect

func (*JsonExpect) StringContains

func (this *JsonExpect) StringContains(jpath string, value string) *JsonExpect

func (*JsonExpect) StringOnlyContains

func (this *JsonExpect) StringOnlyContains(jpath string, value string) *JsonExpect

func (*JsonExpect) Test

func (this *JsonExpect) Test(jpath string, test func(t *testing.T, v interface{})) *JsonExpect

type PlainExpect

type PlainExpect struct {
	*ApiExpect
	// contains filtered or unexported fields
}

func (*PlainExpect) Contains

func (this *PlainExpect) Contains(restr string) *PlainExpect

func (*PlainExpect) NotContains

func (this *PlainExpect) NotContains(restr string) *PlainExpect

type StatusExpect

type StatusExpect struct {
	*ApiExpect
	// contains filtered or unexported fields
}

func (*StatusExpect) Eq

func (this *StatusExpect) Eq(code int) *StatusExpect

func (*StatusExpect) EqAny

func (this *StatusExpect) EqAny(codes ...int) *StatusExpect

func (*StatusExpect) NotEq

func (this *StatusExpect) NotEq(code int) *StatusExpect

type StringMatchFunc

type StringMatchFunc func(string, string) bool

Directories

Path Synopsis
t

Jump to

Keyboard shortcuts

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