goitest

package module
v0.0.0-...-9b6c9f6 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: MIT Imports: 33 Imported by: 0

README ΒΆ

πŸ› οΈ Go Integration Test

tag Go Version GoDoc Build Status Go report Coverage Contributors License

Features

  • Generate http request in golang
  • Generate curl command for http request
  • [] Generate integration test cases (and curl)
    • Assert Rule
    • [] Gen integration rules api+data(curl+request data) - integration rule struct - setParamsFrom: rule.name rule.output(jq)
    • [] integration test server
    • [] integration test ui

Unittest with gonic


func CreateTestCtx(req *http.Request) (resp *httptest.ResponseRecorder, ctx *gin.Context) {
	resp = httptest.NewRecorder()
	ctx, _ = gin.CreateTestContext(resp)
	ctx.Request = req
	return
}

func TestGonicApi(t *testing.T) {
	// 1. build request
	req, _ := goitest.R().
		SetQueryParams(map[string]string{
			"job_id":   "1234",
		}).
		SetReq("GET", "http://any/api/v1/spark/job").
		GenRequest()
	curl := goitest.GenCurlCommand(req, nil)
	println(curl)
	resp, ctx := CreateTestCtx(req)

	// 2. execute
	sparkServer := GetGonicSparkServer()
	sparkServer.GetJobInfo(ctx)
	if resp.Code != http.StatusOK {
		errors := ctx.Errors.Errors()
		fmt.Println("output", errors)
		t.Errorf("Expect code 200, but get %d body:%v", resp.Code, resp.Body)
	} else {
        data := map[string]string{}
		goitest.BuildResponse(resp.Result()).Json(&data)
		if data["status"] == "" {
			t.Fatalf("Bad response: %v", data)
		}
	}
}

todo

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func BuildResponse ΒΆ

func BuildResponse(response *http.Response) *httpRes

func GenCurlCommand ΒΆ

func GenCurlCommand(req *http.Request, httpCookiejar http.CookieJar) (curlString string)

Types ΒΆ

type AssertError ΒΆ

type AssertError struct {
	Expected any
	Actual   any
	Err      error
}

func (*AssertError) Error ΒΆ

func (e *AssertError) Error() string

type ContentType ΒΆ

type ContentType string
const (
	ContentTypeNone       ContentType = ""
	ContentTypeFormEncode ContentType = "application/x-www-form-urlencoded"
	ContentTypeFormData   ContentType = "multipart/form-data"
	ContentTypeJson       ContentType = "application/json"
	ContentTypePlain      ContentType = "text/plain"
)

type ExpectTestType ΒΆ

type ExpectTestType string
const (
	ExpectTestHeaderEqual    ExpectTestType = "header-equal"
	ExpectTestHeaderContains ExpectTestType = "header-contains"
	ExpectTestStatusBetween  ExpectTestType = "status-between"
	ExpectTestBodyEqual      ExpectTestType = "body-equal"
	ExpectTestBodyContains   ExpectTestType = "body-contains"
	ExpectTestBodyJqEqual    ExpectTestType = "body-jq-equal"
	ExpectTestBodyJqContains ExpectTestType = "body-jq-contains"
)

type HeaderType ΒΆ

type HeaderType string
const (
	HeaderContentType   HeaderType = "Content-Type"
	HeaderAuthorization HeaderType = "Authorization"
	HeaderCookie        HeaderType = "Cookie"
)

type ReqItem ΒΆ

type ReqItem struct {
	Name   string
	Url    string
	Method string
	// QueryParam  url.Values
	// FormData    url.Values
	IsMultiPart   bool
	InputHeaders  map[string]string
	InputParams   map[string]string
	InputFormData map[string]string
	InputBody     string
	Json          any
	OutputHeaders map[string]string
	OutputBody    string
	// request 樑板
	Tpl       *RequestTpl
	Testrules []TestRule
}

type RequestTeser ΒΆ

type RequestTeser struct {

	// Response
	Response *http.Response
	// contains filtered or unexported fields
}

func R ΒΆ

func R(t *testing.T, name string) *RequestTeser

func (*RequestTeser) AddCookieKV ΒΆ

func (r *RequestTeser) AddCookieKV(name, value string) *RequestTeser

func (*RequestTeser) AddCookies ΒΆ

func (r *RequestTeser) AddCookies(cookies []*http.Cookie) *RequestTeser

func (*RequestTeser) AddFile ΒΆ

func (r *RequestTeser) AddFile(fieldname, path string) *RequestTeser

************* params ********************* ************* file *********************

func (*RequestTeser) AddFileHeader ΒΆ

func (r *RequestTeser) AddFileHeader(fieldname, filename string, content []byte) *RequestTeser

func (*RequestTeser) AssertBodyContains ΒΆ

func (r *RequestTeser) AssertBodyContains(substr string) *RequestTeser

func (*RequestTeser) AssertBodyJqEqual ΒΆ

func (r *RequestTeser) AssertBodyJqEqual(expr string, expectedVal string) *RequestTeser

func (*RequestTeser) AssertHeaderContains ΒΆ

func (r *RequestTeser) AssertHeaderContains(prop HeaderType, substr string) *RequestTeser

ExpectHeaderContains("Cookie", "session=123")

func (*RequestTeser) AssertHeaderEqual ΒΆ

func (r *RequestTeser) AssertHeaderEqual(prop HeaderType, val string) *RequestTeser

func (*RequestTeser) AssertRules ΒΆ

func (r *RequestTeser) AssertRules()

func (*RequestTeser) AssertStatusBetween ΒΆ

func (r *RequestTeser) AssertStatusBetween(start, end int) *RequestTeser

func (*RequestTeser) CreateGinContext ΒΆ

func (r *RequestTeser) CreateGinContext() (ctx *gin.Context)

func (*RequestTeser) EnableTrace ΒΆ

func (r *RequestTeser) EnableTrace(ctx context.Context) *RequestTeser

func (*RequestTeser) FromCurl ΒΆ

func (r *RequestTeser) FromCurl(curl string)

func (*RequestTeser) GenCurlCommand ΒΆ

func (r *RequestTeser) GenCurlCommand() (curl string, err error)

func (*RequestTeser) GenRequest ΒΆ

func (rb *RequestTeser) GenRequest() (*http.Request, error)

Generate a http.Request

func (*RequestTeser) GetRawreq ΒΆ

func (r *RequestTeser) GetRawreq() *http.Request

************* utils *********************

func (*RequestTeser) SetAuthBasic ΒΆ

func (r *RequestTeser) SetAuthBasic(username, password string) *RequestTeser

func (*RequestTeser) SetAuthBearer ΒΆ

func (r *RequestTeser) SetAuthBearer(token string) *RequestTeser

func (*RequestTeser) SetBody ΒΆ

func (r *RequestTeser) SetBody(body []byte) *RequestTeser

************* body(bytes) *********************

func (*RequestTeser) SetContentType ΒΆ

func (r *RequestTeser) SetContentType(ct ContentType) *RequestTeser

func (*RequestTeser) SetCtx ΒΆ

func (r *RequestTeser) SetCtx(ctx context.Context) *RequestTeser

func (*RequestTeser) SetFormData ΒΆ

func (r *RequestTeser) SetFormData(data map[string]string) *RequestTeser

func (*RequestTeser) SetFormDataFromValues ΒΆ

func (r *RequestTeser) SetFormDataFromValues(data url.Values) *RequestTeser

SetFormDataFromValues method appends multiple form parameters with multi-value

SetFormDataFromValues(url.Values{"words": []string{"book", "glass", "pencil"},})

func (*RequestTeser) SetHeader ΒΆ

func (r *RequestTeser) SetHeader(key, value string) *RequestTeser

*****************header ************************

func (*RequestTeser) SetHost ΒΆ

func (r *RequestTeser) SetHost(host string) *RequestTeser

*****************host************************

func (*RequestTeser) SetIsMultiPart ΒΆ

func (r *RequestTeser) SetIsMultiPart(b bool) *RequestTeser

************* body(form) ********************* Set Form data(encode or multipart)

func (*RequestTeser) SetJson ΒΆ

func (r *RequestTeser) SetJson(data any) *RequestTeser

************* body(json) *********************

func (*RequestTeser) SetQueryParam ΒΆ

func (r *RequestTeser) SetQueryParam(param, value string) *RequestTeser

func (*RequestTeser) SetQueryParamTpl ΒΆ

func (r *RequestTeser) SetQueryParamTpl(k, jqExpr string) *RequestTeser

func (*RequestTeser) SetQueryParams ΒΆ

func (r *RequestTeser) SetQueryParams(params map[string]string) *RequestTeser

************* params *********************

func (*RequestTeser) SetQueryParamsFromValues ΒΆ

func (r *RequestTeser) SetQueryParamsFromValues(params url.Values) *RequestTeser

func (*RequestTeser) SetQueryParamsTpl ΒΆ

func (r *RequestTeser) SetQueryParamsTpl(params map[string]string) *RequestTeser

func (*RequestTeser) SetReq ΒΆ

func (r *RequestTeser) SetReq(method string, url string) *RequestTeser

func (*RequestTeser) SetResponse ΒΆ

func (r *RequestTeser) SetResponse(resp *httptest.ResponseRecorder) *RequestTeser

func (*RequestTeser) SetUrl ΒΆ

func (r *RequestTeser) SetUrl(url string) *RequestTeser

func (*RequestTeser) Sync ΒΆ

func (r *RequestTeser) Sync()

func (*RequestTeser) Test ΒΆ

func (r *RequestTeser) Test(name string, f func(*RequestTeser))

type RequestTpl ΒΆ

type RequestTpl struct {
	QueryParam map[string]string
	FormData   map[string]any
	Json       any
}

type TestRule ΒΆ

type TestRule struct {
	AssertType         ExpectTestType
	PropOrExpr         string
	ExpectedVal        string
	ExpectedBetweenInt [2]int
}

type TraceInfo ΒΆ

type TraceInfo struct {
	// DNSLookup is a duration that transport took to perform
	// DNS lookup.
	DNSLookup time.Duration

	// ConnTime is a duration that took to obtain a successful connection.
	ConnTime time.Duration

	// TCPConnTime is a duration that took to obtain the TCP connection.
	TCPConnTime time.Duration

	// TLSHandshake is a duration that TLS handshake took place.
	TLSHandshake time.Duration

	// ServerTime is a duration that server took to respond first byte.
	ServerTime time.Duration

	// ResponseTime is a duration since first response byte from server to
	// request completion.
	ResponseTime time.Duration

	// TotalTime is a duration that total request took end-to-end.
	TotalTime time.Duration

	// IsConnReused is whether this connection has been previously
	// used for another HTTP request.
	IsConnReused bool

	// IsConnWasIdle is whether this connection was obtained from an
	// idle pool.
	IsConnWasIdle bool

	// ConnIdleTime is a duration how long the connection was previously
	// idle, if IsConnWasIdle is true.
	ConnIdleTime time.Duration

	// RequestAttempt is to represent the request attempt made during a Resty
	// request execution flow, including retry count.
	RequestAttempt int

	// RemoteAddr returns the remote network address.
	RemoteAddr net.Addr
}

TraceInfo struct is used provide request trace info such as DNS lookup duration, Connection obtain duration, Server processing duration, etc.

Since v2.0.0

Directories ΒΆ

Path Synopsis
Package shellescape provides the shellescape.Quote to escape arbitrary strings for a safe use as command line arguments in the most common POSIX shells.
Package shellescape provides the shellescape.Quote to escape arbitrary strings for a safe use as command line arguments in the most common POSIX shells.

Jump to

Keyboard shortcuts

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