Documentation
¶
Overview ¶
webapitest 包提供用于测试 webapi 包的辅助方法。
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var NoOpHandler webapi.ApiHandler = &webapi.ApiHandlerWrapper{}
NoOpHandler 是一个空的 webapi.ApiHandler ,用于测试用例中不需要访问其方法只需要一个实例占位的场景。
Functions ¶
func CreateMultipartFileHeader ¶ added in v0.8.0
func CreateMultipartFileHeader(fieldName, fileName string, body []byte, header map[string]string) *multipart.FileHeader
CreateMultipartFileHeader 根据给定的内容创建一个 multipart.FileHeader 实例。
func NewStateForTest ¶
func NewStateForTest(apiHandler webapi.ApiHandler, uri string, setup NewStateSetup) (*webapi.ApiState, *httptest.ResponseRecorder)
NewStateForTest 基于 httptest 包创建用于测试 HTTP 请求的相关实例。
Types ¶
type LogRecorder ¶
type LogRecorder struct {
// contains filtered or unexported fields
}
LogRecorder 实现 logx.Logger ,将全部日志追加记录在一个字符串上,每个日志末尾追加一个换行。 每个日志的字符串拼接格式为,格式化使用 fmt.Sprintf() :
level={LEVEL} message={MESSAGE} KEY1=VALUE1 KEY2=VALUE2 ...
func (*LogRecorder) Log ¶
func (l *LogRecorder) Log(level logx.Level, message string, keyValues ...any) error
Log 实现 Logger.Log() 。
func (*LogRecorder) LogFn ¶
func (l *LogRecorder) LogFn(level logx.Level, messageFactory func() (string, []any)) error
Log 实现 Logger.LogFn() 。
func (*LogRecorder) Map ¶
func (l *LogRecorder) Map() []map[string]string
Map 返回结构化日志。每条日志使用一个 map 记录。
type NewStateSetup ¶
type NewStateSetup struct { HttpMethod string // HTTP 请求的方法, GET/POST/PUT/DELETE 。若未给定值,默认为 GET 。 ContentType string // 指定 HTTP Content-Type 头,若未给定值,则不会添加此字段。 Headers map[string]string // 其他 HTTP 头。 BodyString string // 指定请求的 body ,优先级高于 BodyReader 。给定值时 BodyReader 被忽略。 BodyReader io.Reader // 指定请求的 body ,仅在 BodyString 为空时生效。 RouteParams map[string]string // 指定路由参数。若为 nil 或为空集则不会初始化路由参数。 }
NewStateSetup 用于设置用于测试 HTTP 请求。
Click to show internal directories.
Click to hide internal directories.