Documentation ¶
Overview ¶
Package rest 简单的 API 测试库
Index ¶
- func BuildHandler(a *assert.Assertion, code int, body string, headers map[string]string) http.Handler
- func BuildHandlerFunc(a *assert.Assertion, code int, body string, headers map[string]string) func(http.ResponseWriter, *http.Request)
- func RawHTTP(a *assert.Assertion, client *http.Client, reqRaw, respRaw string)
- func RawHandler(a *assert.Assertion, h http.Handler, reqRaw, respRaw string)
- func UnmarshalObject(data []byte, val interface{}, u func([]byte, interface{}) error) (interface{}, error)
- type Request
- func Delete(a *assert.Assertion, path string) *Request
- func Get(a *assert.Assertion, path string) *Request
- func NewRequest(a *assert.Assertion, method, path string) *Request
- func Patch(a *assert.Assertion, path string, body []byte) *Request
- func Post(a *assert.Assertion, path string, body []byte) *Request
- func Put(a *assert.Assertion, path string, body []byte) *Request
- func (req *Request) Body(body []byte) *Request
- func (req *Request) BodyFunc(obj interface{}, marshal func(interface{}) ([]byte, error)) *Request
- func (req *Request) Do(h http.Handler) *Response
- func (req *Request) Header(key, val string) *Request
- func (req *Request) JSONBody(obj interface{}) *Request
- func (req *Request) Param(key, val string) *Request
- func (req *Request) Query(key, val string) *Request
- func (req *Request) Request() *http.Request
- func (req *Request) StringBody(body string) *Request
- func (req *Request) XMLBody(obj interface{}) *Request
- type Response
- func (resp *Response) Body(val []byte, msg ...interface{}) *Response
- func (resp *Response) BodyEmpty(msg ...interface{}) *Response
- func (resp *Response) BodyFunc(f func(a *assert.Assertion, body []byte)) *Response
- func (resp *Response) BodyNotEmpty(msg ...interface{}) *Response
- func (resp *Response) Fail(msg ...interface{}) *Response
- func (resp *Response) Header(key string, val string, msg ...interface{}) *Response
- func (resp *Response) JSONBody(val interface{}) *Response
- func (resp *Response) NotHeader(key string, val string, msg ...interface{}) *Response
- func (resp *Response) NotStatus(status int, msg ...interface{}) *Response
- func (resp *Response) Resp() *http.Response
- func (resp *Response) Status(status int, msg ...interface{}) *Response
- func (resp *Response) StringBody(val string, msg ...interface{}) *Response
- func (resp *Response) Success(msg ...interface{}) *Response
- func (resp *Response) XMLBody(val interface{}) *Response
- type Server
- func (srv *Server) Assertion() *assert.Assertion
- func (srv *Server) Delete(path string) *Request
- func (srv *Server) Get(path string) *Request
- func (srv *Server) NewRequest(method, path string) *Request
- func (srv *Server) Patch(path string, body []byte) *Request
- func (srv *Server) Post(path string, body []byte) *Request
- func (srv *Server) Put(path string, body []byte) *Request
- func (srv *Server) RawHTTP(req, resp string) *Server
- func (srv *Server) URL() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildHandler ¶
func BuildHandler(a *assert.Assertion, code int, body string, headers map[string]string) http.Handler
BuildHandler 生成用于测试的 http.Handler 对象
仅是简单地按以下步骤输出内容:
- 输出状态码 code;
- 输出报头 headers,以 Add 方式,而不是 set,不会覆盖原来的数据;
- 输出 body,如果为空字符串,则不会输出;
func BuildHandlerFunc ¶
func RawHTTP ¶
RawHTTP 通过原始数据进行比较请求和返回数据是符合要求
reqRaw 表示原始的请求数据; respRaw 表示返回之后的原始数据;
NOTE: 仅判断状态码、报头和实际内容是否相同,而不是直接比较两个 http.Response 的值。
func RawHandler ¶
RawHandler 通过原始数据进行比较请求和返回数据是符合要求
功能上与 RawHTTP 相似,处理方式从 http.Client 变成了 http.Handler。
Types ¶
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request 请求的参数封装
func NewRequest ¶
NewRequest 以调用链的方式构建一个访问请求对象
NOTE: 通过此函数构建的对象只能访问 http.Handler 的内容, 如果需要访问网络内容,可以采用 Server.NewRequest。
func (*Request) StringBody ¶
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response 测试请求的返回结构
func (*Response) BodyNotEmpty ¶
BodyNotEmpty 报文内容是否不为空
func (*Response) StringBody ¶
StringBody 断言内容与 val 相同
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 测试服务
func (*Server) NewRequest ¶
NewRequest 获取一条请求的结果
method 表示请求方法 path 表示请求的路径,域名部分无须填定。可以通过 {} 指定参数,比如:
r := NewRequest(http.MethodGet, "/users/{id}")
之后就可以使用 Params 指定 id 的具体值,达到复用的目的:
resp1 := r.Param("id", "1").Do() resp2 := r.Param("id", "2").Do()
Click to show internal directories.
Click to hide internal directories.