testing

package
v1.1.13 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertBadRequest

func AssertBadRequest(t *testing.T, response *Response)

AssertBadRequest 断言HTTP状态码为400 Bad Request

func AssertBodyContains

func AssertBodyContains(t *testing.T, response *Response, substring string)

AssertBodyContains 断言响应体包含特定字符串

func AssertBodyEquals

func AssertBodyEquals(t *testing.T, response *Response, expected string)

AssertBodyEquals 断言响应体等于特定字符串

func AssertCreated

func AssertCreated(t *testing.T, response *Response)

AssertCreated 断言HTTP状态码为201 Created

func AssertForbidden

func AssertForbidden(t *testing.T, response *Response)

AssertForbidden 断言HTTP状态码为403 Forbidden

func AssertHeader

func AssertHeader(t *testing.T, response *Response, key, expectedValue string)

AssertHeader 断言HTTP头部值

func AssertJSON

func AssertJSON(t *testing.T, response *Response, expectedJSON interface{})

AssertJSON 断言JSON响应体

func AssertJSONPath

func AssertJSONPath(t *testing.T, response *Response, path string, expectedValue interface{})

AssertJSONPath 断言JSON响应体中特定路径的值

func AssertJSONSchema

func AssertJSONSchema(t *testing.T, response *Response, requiredFields []string)

AssertJSONSchema 断言JSON响应体符合指定的JSON Schema(简化版,仅检查必填字段)

func AssertNoContent

func AssertNoContent(t *testing.T, response *Response)

AssertNoContent 断言HTTP状态码为204 No Content

func AssertNotFound

func AssertNotFound(t *testing.T, response *Response)

AssertNotFound 断言HTTP状态码为404 Not Found

func AssertOK

func AssertOK(t *testing.T, response *Response)

AssertOK 断言HTTP状态码为200 OK

func AssertStatus

func AssertStatus(t *testing.T, response *Response, expectedCode int)

AssertStatus 断言HTTP状态码

func AssertUnauthorized

func AssertUnauthorized(t *testing.T, response *Response)

AssertUnauthorized 断言HTTP状态码为401 Unauthorized

Types

type HTTPClient

type HTTPClient struct {
	Engine  *flow.Engine
	T       *testing.T
	BaseURL string
}

HTTPClient 是测试HTTP请求的客户端

func NewHTTPClient

func NewHTTPClient(t *testing.T, engine *flow.Engine) *HTTPClient

NewHTTPClient 创建一个新的HTTP测试客户端

func (*HTTPClient) DELETE

func (c *HTTPClient) DELETE(path string, headers map[string]string) *Response

DELETE 发送DELETE请求

func (*HTTPClient) Do

func (c *HTTPClient) Do(req Request) *Response

Do 执行HTTP请求并返回响应

func (*HTTPClient) GET

func (c *HTTPClient) GET(path string, query url.Values, headers map[string]string) *Response

GET 发送GET请求

func (*HTTPClient) PATCH

func (c *HTTPClient) PATCH(path string, body interface{}, headers map[string]string) *Response

PATCH 发送PATCH请求

func (*HTTPClient) POST

func (c *HTTPClient) POST(path string, body interface{}, headers map[string]string) *Response

POST 发送POST请求

func (*HTTPClient) PUT

func (c *HTTPClient) PUT(path string, body interface{}, headers map[string]string) *Response

PUT 发送PUT请求

func (*HTTPClient) WithBaseURL

func (c *HTTPClient) WithBaseURL(baseURL string) *HTTPClient

WithBaseURL 设置基础URL

type Request

type Request struct {
	Method  string
	Path    string
	Headers map[string]string
	Body    interface{}
	Query   url.Values
}

Request 定义HTTP请求结构

type Response

type Response struct {
	StatusCode int
	Header     http.Header
	Body       []byte
	Result     interface{}
	Raw        *httptest.ResponseRecorder
}

Response 定义HTTP响应结构

func (*Response) BindJSON

func (r *Response) BindJSON(v interface{}) error

BindJSON 将响应体绑定到JSON结构

func (*Response) JSON

func (r *Response) JSON() (map[string]interface{}, error)

JSON 返回响应体解析为JSON的结果

func (*Response) String

func (r *Response) String() string

String 返回响应体的字符串表示

type TestCase

type TestCase struct {
	Name           string
	Method         string
	Path           string
	Headers        map[string]string
	Body           interface{}
	Query          url.Values
	ExpectedStatus int
	ExpectedBody   interface{}
	Setup          func(t *testing.T)
	Teardown       func(t *testing.T)
	Handler        flow.HandlerFunc
	Middleware     []flow.HandlerFunc
}

TestCase 定义测试用例结构

type TestSuite

type TestSuite struct {
	Engine      *flow.Engine
	BaseURL     string
	DefaultPath string
	T           *testing.T
}

TestSuite 定义测试套件结构

func NewTestSuite

func NewTestSuite(t *testing.T) *TestSuite

NewTestSuite 创建一个新的测试套件

func (*TestSuite) AssertHeader

func (ts *TestSuite) AssertHeader(w *httptest.ResponseRecorder, key, value string)

AssertHeader 断言Header等于指定值

func (*TestSuite) AssertHeaderContains

func (ts *TestSuite) AssertHeaderContains(w *httptest.ResponseRecorder, key, value string)

AssertHeaderContains 断言Header包含指定值

func (*TestSuite) AssertJSON

func (ts *TestSuite) AssertJSON(w *httptest.ResponseRecorder, expected interface{})

AssertJSON 断言JSON响应

func (*TestSuite) AssertJSONContains

func (ts *TestSuite) AssertJSONContains(w *httptest.ResponseRecorder, key string, value interface{})

AssertJSONContains 断言JSON响应中包含指定字段

func (*TestSuite) AssertStatus

func (ts *TestSuite) AssertStatus(w *httptest.ResponseRecorder, status int)

AssertStatus 断言HTTP状态码

func (*TestSuite) Request

func (ts *TestSuite) Request(method, path string, body interface{}, headers map[string]string) *httptest.ResponseRecorder

Request 执行HTTP请求

func (*TestSuite) RunTestCase

func (ts *TestSuite) RunTestCase(tc TestCase)

RunTestCase 运行单个测试用例

func (*TestSuite) RunTestCases

func (ts *TestSuite) RunTestCases(cases []TestCase)

RunTestCases 运行多个测试用例

func (*TestSuite) UploadFile

func (ts *TestSuite) UploadFile(path, fieldName, filePath string, additionalFields map[string]string) *httptest.ResponseRecorder

UploadFile 上传文件测试辅助函数

Jump to

Keyboard shortcuts

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