test

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2021 License: MIT Imports: 20 Imported by: 0

README

Golang unit testing tools

Build Status codecov Go Report Card
GoDoc

go get github.com/goapt/test

HTTP Mock

var httpSuites = []test.HttpClientSuite{
    {
        URI: "/test",
        ResponseBody: `{"retcode":200}`,
    },
}

func TestLoginHandle(t *testing.T) {
	httpClient := test.NewHttpClientSuite(httpSuites)
    resp, err :=  httpClient.Post("https://dummy.impl/test",test.JsonContentType,strings.NewReader(""))
    assert.NoError(t, err)
    if err == nil {
        body, err := ioutil.ReadAll(resp.Body)
        assert.NoError(t, err)
        assert.Equal(t, `{"retcode":200}`, string(body))
    }
}

Redis memory server

Based on the github.com/alicebob/miniredis

rds := test.NewRedis()

Gee handler test

req := test.NewRequest("/test", func(c *gee.Context) gee.Response {
    return c.String("ok")
})

Documentation

Index

Constants

View Source
const (
	JsonContentType = "application/json; charset=utf-8"
	FormContentType = "application/x-www-form-urlencoded; charset=utf-8"
	XmlContentType  = "application/xml; charset=utf-8"
	TextContentType = "text/plain; charset=utf-8"
)

Variables

View Source
var (
	// 专供测试使用的RSA密钥对
	RSAPublicKey = `` /* 450-byte string literal not displayed */

	RSAPrivateKey = `` /* 1674-byte string literal not displayed */

)
View Source
var Conf *viper.Viper

Functions

func Config

func Config() *viper.Viper

Config 加载测试私密文件

func NewHttpClient

func NewHttpClient(fn RoundTripFunc) *http.Client

NewTestHttpClient returns *http.Client with Transport replaced to avoid making real calls

func NewHttpClientSuite

func NewHttpClientSuite(suite []HttpClientSuite) *http.Client

NewHttpClientSuite quickly define HTTP Response for mock

func NewRedis

func NewRedis() *redis.Redis

Types

type HttpClientSuite

type HttpClientSuite struct {
	URI          string
	MatchBody    map[string]interface{}
	MatchQuery   map[string]interface{}
	ResponseBody string
	StatusCode   int
	Header       http.Header
	Error        error
}

HttpClientSuite is test set for http client URI is need to match request uri MatchBody is need to match request body, The key of the Map is the JSON path, map["goods_defail.goods_id":"1"] MatchQuery is need to match request query params ResponseBody is http return body StatusCode is http status Header is response headers Error mock request exception

type Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

func NewRequest

func NewRequest(url string, handlers ...gee.HandlerFunc) *Request

func NewRequestWithPath

func NewRequestWithPath(path, url string, handlers ...gee.HandlerFunc) *Request

func (*Request) BeforeHook

func (r *Request) BeforeHook(fn func(req *http.Request))

func (*Request) Form

func (r *Request) Form(values *url.Values) (*Response, error)

func (*Request) Get

func (r *Request) Get() (*Response, error)

func (*Request) JSON

func (r *Request) JSON(data interface{}) (*Response, error)

func (*Request) Post

func (r *Request) Post(contentType string, body io.Reader) (*Response, error)

func (*Request) XML

func (r *Request) XML(body string) (*Response, error)

type Response

type Response struct {
	*httptest.ResponseRecorder
}

func (*Response) GetBody

func (r *Response) GetBody() []byte

func (*Response) GetBodyString

func (r *Response) GetBodyString() string

func (*Response) GetJson added in v1.0.1

func (r *Response) GetJson() gjson.Result

func (*Response) GetJsonPath added in v1.0.1

func (r *Response) GetJsonPath(path string) gjson.Result

type RoundTripFunc

type RoundTripFunc func(req *http.Request) (*http.Response, error)

RoundTripFunc .

func (RoundTripFunc) RoundTrip

func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip .

Jump to

Keyboard shortcuts

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