rest

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package rest 简单的 API 测试库

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Request

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

Request 请求的参数封装

func NewRequest added in v1.1.0

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

NewRequest 创建新的请求实例

client 如果为空,则会采用 http.DefaultClient 作为其值。

func (*Request) Body

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

Body 指定提交的内容

func (*Request) Do

func (req *Request) Do() *Response

Do 执行请求操作

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

func (*Request) Param

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

Param 替换参数

func (*Request) Query

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

Query 替换一个请求参数

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 added in v1.3.2

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

BodyEmpty 报文内容是否为空

func (*Response) BodyNil

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

BodyNil 报文内容是否为 nil

func (*Response) BodyNotEmpty added in v1.3.2

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

BodyNotEmpty 报文内容是否不为空

func (*Response) BodyNotNil

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

BodyNotNil 报文内容是否不为 nil

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 将 val 转换成 JSON 对象,并与 body 作对比

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 不相等,若相等,则返回 msg 指定的消息

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

func (*Response) ReadBody added in v1.2.0

func (resp *Response) ReadBody(w io.Writer) *Response

ReadBody 读取 Body 的内容到 w

func (*Response) Status

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

Status 判断状态码是否与 status 相等,若不相等,则返回 msg 指定的消息

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

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 之间均算成功

type Server

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

Server 测试服务

func NewServer

func NewServer(t testing.TB, h http.Handler, client *http.Client) *Server

NewServer 声明新的测试服务

如果 client 为 nil,则会采用 http.DefaultClient 作为默认值

func NewTLSServer

func NewTLSServer(t testing.TB, h http.Handler, client *http.Client) *Server

NewTLSServer 声明新的测试服务

如果 client 为 nil,则会采用 http.DefaultClient 作为默认值

func (*Server) Close

func (srv *Server) Close()

Close 停止服务

func (*Server) Delete added in v1.3.0

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

Delete 相当于 NewRequest(http.MethodDelete, path).Body()

func (*Server) Get added in v1.3.0

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

Get 相当于 NewRequest(http.MethodGet, path)

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 added in v1.3.0

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

Patch 相当于 NewRequest(http.MethodPatch, path).Body()

func (*Server) Post added in v1.3.0

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

Post 相当于 NewRequest(http.MethodPost, path).Body()

func (*Server) Put added in v1.3.0

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

Put 相当于 NewRequest(http.MethodPut, path).Body()

Jump to

Keyboard shortcuts

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