rest

package
v2.3.2 Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

Package rest 简单的 API 测试库

Index

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 BuildHandlerFunc(a *assert.Assertion, code int, body string, headers map[string]string) func(http.ResponseWriter, *http.Request)

func RawHTTP

func RawHTTP(a *assert.Assertion, client *http.Client, reqRaw, respRaw string)

RawHTTP 通过原始数据进行比较请求和返回数据是符合要求

reqRaw 表示原始的请求数据; respRaw 表示返回之后的原始数据;

NOTE: 仅判断状态码、报头和实际内容是否相同,而不是直接比较两个 http.Response 的值。

func RawHandler

func RawHandler(a *assert.Assertion, h http.Handler, reqRaw, respRaw string)

RawHandler 通过原始数据进行比较请求和返回数据是符合要求

功能上与 RawHTTP 相似,处理方式从 http.Client 变成了 http.Handler。

func UnmarshalObject

func UnmarshalObject(data []byte, val interface{}, u func([]byte, interface{}) error) (interface{}, error)

UnmarshalObject 将 data 以 u 作为转换方式转换成与 val 相同的类型

如果 val 是指针,则会转换成其指向的类型,返回的对象是指针类型。

Types

type Request

type Request struct {
	// contains filtered or unexported fields
}

Request 请求的参数封装

func Delete added in v2.0.1

func Delete(a *assert.Assertion, path string) *Request

func Get added in v2.0.1

func Get(a *assert.Assertion, path string) *Request

func NewRequest

func NewRequest(a *assert.Assertion, method, path string) *Request

NewRequest 以调用链的方式构建一个访问请求对象

func Patch added in v2.0.1

func Patch(a *assert.Assertion, path string, body []byte) *Request

func Post added in v2.0.1

func Post(a *assert.Assertion, path string, body []byte) *Request

func Put added in v2.0.1

func Put(a *assert.Assertion, path string, body []byte) *Request

func (*Request) Body

func (req *Request) Body(body []byte) *Request

Body 指定提交的内容

func (*Request) BodyFunc

func (req *Request) BodyFunc(obj interface{}, marshal func(interface{}) ([]byte, error)) *Request

BodyFunc 指定一个未编码的对象

marshal 对 obj 的编码函数,比如 json.Marshal 等。

func (*Request) Client added in v2.2.1

func (req *Request) Client(c *http.Client) *Request

Client 指定采用的客户端实例

可以为空,如果为空,那么在 Do 函数中的参数必不能为空。

func (*Request) Do

func (req *Request) Do(h http.Handler) *Response

Do 执行请求操作

h 默认为空,如果不为空,则表示当前请求忽略 http.Client,而是访问 h.ServeHTTP 的内容。

func (*Request) Header

func (req *Request) Header(key, val string) *Request

Header 指定请求时的报头

func (*Request) JSONBody

func (req *Request) JSONBody(obj interface{}) *Request

JSONBody 指定一个 JSON 格式的 body

NOTE: 此函并不会设置 content-type 报头。

func (*Request) Param

func (req *Request) Param(key, val string) *Request

Param 替换参数

func (*Request) Query

func (req *Request) Query(key, val string) *Request

Query 添加一个请求参数

func (*Request) Request added in v2.1.0

func (req *Request) Request() *http.Request

Request 返回标准库的 http.Request 实例

func (*Request) StringBody

func (req *Request) StringBody(body string) *Request

func (*Request) XMLBody

func (req *Request) XMLBody(obj interface{}) *Request

XMLBody 指定一个 XML 格式的 body

NOTE: 此函并不会设置 content-type 报头。

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response 测试请求的返回结构

func (*Response) Body

func (resp *Response) Body(val []byte, msg ...interface{}) *Response

Body 断言内容与 val 相同

func (*Response) BodyEmpty

func (resp *Response) BodyEmpty(msg ...interface{}) *Response

BodyEmpty 报文内容是否为空

func (*Response) BodyFunc

func (resp *Response) BodyFunc(f func(a *assert.Assertion, body []byte)) *Response

BodyFunc 指定对 body 内容的断言方式

func (*Response) BodyNotEmpty

func (resp *Response) BodyNotEmpty(msg ...interface{}) *Response

BodyNotEmpty 报文内容是否不为空

func (*Response) Fail

func (resp *Response) Fail(msg ...interface{}) *Response

Fail 状态码是否大于 399

func (*Response) Header

func (resp *Response) Header(key string, val string, msg ...interface{}) *Response

Header 判断指定的报头是否与 val 相同

msg 可以为空,会返回一个默认的错误提示信息

func (*Response) JSONBody

func (resp *Response) JSONBody(val interface{}) *Response

JSONBody body 转换成 JSON 对象之后是否等价于 val

func (*Response) NotHeader

func (resp *Response) NotHeader(key string, val string, msg ...interface{}) *Response

NotHeader 指定的报头必定不与 val 相同。

func (*Response) NotStatus

func (resp *Response) NotStatus(status int, msg ...interface{}) *Response

NotStatus 判断状态码是否与 status 不相等

func (*Response) Resp

func (resp *Response) Resp() *http.Response

Resp 返回 http.Response 实例

NOTE: http.Response.Body 内容已经被读取且关闭。

func (*Response) Status

func (resp *Response) Status(status int, msg ...interface{}) *Response

Status 判断状态码是否与 status 相等

func (*Response) StringBody

func (resp *Response) StringBody(val string, msg ...interface{}) *Response

StringBody 断言内容与 val 相同

func (*Response) Success

func (resp *Response) Success(msg ...interface{}) *Response

Success 状态码是否在 100-399 之间

func (*Response) XMLBody

func (resp *Response) XMLBody(val interface{}) *Response

XMLBody body 转换成 XML 对象之后是否等价于 val

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server 测试服务

func NewServer

func NewServer(a *assert.Assertion, h http.Handler, client *http.Client) *Server

NewServer 声明新的测试服务

如果 client 为 nil,则会采用 &http.Client{} 作为默认值

func NewTLSServer

func NewTLSServer(a *assert.Assertion, h http.Handler, client *http.Client) *Server

NewTLSServer 声明新的测试服务

如果 client 为 nil,则会采用 &http.Client{} 作为默认值

func (*Server) Assertion

func (srv *Server) Assertion() *assert.Assertion

func (*Server) Close added in v2.2.0

func (srv *Server) Close()

Close 关闭服务

如果未手动调用,则在 testing.TB.Cleanup 中自动调用。

func (*Server) Delete

func (srv *Server) Delete(path string) *Request

func (*Server) Get

func (srv *Server) Get(path string) *Request

func (*Server) NewRequest

func (srv *Server) NewRequest(method, path string) *Request

NewRequest 获取一条请求的结果

method 表示请求方法 path 表示请求的路径,域名部分无须填定。可以通过 {} 指定参数,比如:

r := NewRequest(http.MethodGet, "/users/{id}")

之后就可以使用 Params 指定 id 的具体值,达到复用的目的:

resp1 := r.Param("id", "1").Do()
resp2 := r.Param("id", "2").Do()

func (*Server) Patch

func (srv *Server) Patch(path string, body []byte) *Request

func (*Server) Post

func (srv *Server) Post(path string, body []byte) *Request

func (*Server) Put

func (srv *Server) Put(path string, body []byte) *Request

func (*Server) RawHTTP

func (srv *Server) RawHTTP(req, resp string) *Server

func (*Server) URL

func (srv *Server) URL() string

Jump to

Keyboard shortcuts

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