Skip to content

Commit

Permalink
refactor: 更新至 v4
Browse files Browse the repository at this point in the history
同时删除部分标记为 Deprecated 的函数
  • Loading branch information
caixw committed Feb 27, 2024
1 parent 9785f22 commit d6443e7
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 98 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/issue9/assert/v3
module github.com/issue9/assert/v4

go 1.17
26 changes: 3 additions & 23 deletions rest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ package rest

import (
"bytes"
"encoding/json"
"encoding/xml"
"io"
"net/http"
"net/url"
"strings"

"github.com/issue9/assert/v3"
"github.com/issue9/assert/v4"
)

// Request 请求的参数封装
Expand Down Expand Up @@ -153,7 +151,7 @@ func (req *Request) StringBody(body string) *Request {

// BodyFunc 指定一个未编码的对象
//
// marshal 对 obj 的编码函数,比如 json.Marshal 等。
// marshal 对 obj 的编码函数,比如 [json.Marshal] 等。
func (req *Request) BodyFunc(obj interface{}, marshal func(interface{}) ([]byte, error)) *Request {
req.a.TB().Helper()

Expand All @@ -162,30 +160,12 @@ func (req *Request) BodyFunc(obj interface{}, marshal func(interface{}) ([]byte,
return req.Body(data)
}

// JSONBody 指定一个 JSON 格式的 body
//
// NOTE: 此函并不会设置 content-type 报头。
//
// Deprecated: 下个版本将会被删除。
func (req *Request) JSONBody(obj interface{}) *Request {
return req.BodyFunc(obj, json.Marshal)
}

// XMLBody 指定一个 XML 格式的 body
//
// NOTE: 此函并不会设置 content-type 报头。
//
// Deprecated: 下个版本将会被删除。
func (req *Request) XMLBody(obj interface{}) *Request {
return req.BodyFunc(obj, xml.Marshal)
}

func (req *Request) buildPath() string {
path := req.path

for key, val := range req.params {
key = "{" + key + "}"
path = strings.Replace(path, key, val, -1)
path = strings.ReplaceAll(path, key, val)
}

if len(req.queries) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion rest/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"testing"

"github.com/issue9/assert/v3"
"github.com/issue9/assert/v4"
)

func TestRequest_buildPath(t *testing.T) {
Expand Down
62 changes: 6 additions & 56 deletions rest/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ package rest

import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"io"
"net/http"
"net/http/httptest"
"reflect"

"github.com/issue9/assert/v3"
"github.com/issue9/assert/v4"
)

// Response 测试请求的返回结构
Expand Down Expand Up @@ -64,7 +60,7 @@ func (req *Request) Do(h http.Handler) *Response {

// Resp 返回 [http.Response] 实例
//
// NOTE: http.Response.Body 内容已经被读取且关闭。
// NOTE: [http.Response.Body] 内容已经被读取且关闭。
func (resp *Response) Resp() *http.Response { return resp.resp }

func (resp *Response) assert(expr bool, f *assert.Failure) *Response {
Expand All @@ -91,7 +87,7 @@ func (resp *Response) Fail(msg ...interface{}) *Response {
func (resp *Response) Status(status int, msg ...interface{}) *Response {
resp.a.TB().Helper()
eq := resp.resp.StatusCode == status
return resp.assert(eq, assert.NewFailure("Status", msg, map[string]interface{}{"status1": resp.resp.StatusCode, "status2": status}))
return resp.assert(eq, assert.NewFailure("Status", msg, map[string]interface{}{"status": resp.resp.StatusCode, "val": status}))
}

// NotStatus 判断状态码是否与 status 不相等
Expand Down Expand Up @@ -120,14 +116,14 @@ func (resp *Response) NotHeader(key string, val string, msg ...interface{}) *Res
// Body 断言内容与 val 相同
func (resp *Response) Body(val []byte, msg ...interface{}) *Response {
resp.a.TB().Helper()
return resp.assert(bytes.Equal(resp.body, val), assert.NewFailure("Body", msg, map[string]interface{}{"v1": string(resp.body), "v2": string(val)}))
return resp.assert(bytes.Equal(resp.body, val), assert.NewFailure("Body", msg, map[string]interface{}{"body": string(resp.body), "val": string(val)}))
}

// StringBody 断言内容与 val 相同
func (resp *Response) StringBody(val string, msg ...interface{}) *Response {
resp.a.TB().Helper()
b := string(resp.body)
return resp.assert(b == val, assert.NewFailure("StringBody", msg, map[string]interface{}{"v1": b, "v2": val}))
return resp.assert(b == val, assert.NewFailure("StringBody", msg, map[string]interface{}{"body": b, "val": val}))
}

// BodyNotEmpty 报文内容是否不为空
Expand All @@ -139,37 +135,7 @@ func (resp *Response) BodyNotEmpty(msg ...interface{}) *Response {
// BodyEmpty 报文内容是否为空
func (resp *Response) BodyEmpty(msg ...interface{}) *Response {
resp.a.TB().Helper()
return resp.assert(len(resp.body) == 0, assert.NewFailure("BodyEmpty", msg, map[string]interface{}{"v": resp.body}))
}

// JSONBody body 转换成 JSON 对象之后是否等价于 val
//
// Deprecated: 下个版本将会被删除。
func (resp *Response) JSONBody(val interface{}) *Response {
resp.a.TB().Helper()
return resp.BodyFunc(func(a *assert.Assertion, body []byte) {
a.TB().Helper()

// NOTE: 应当始终将 body 转换 val 相同的类型,然后再比较对象,
// 因为 val 转换成字符串,可能因为空格缩进等原因,未必会与 body 是相同的。
b, err := UnmarshalObject(body, val, json.Unmarshal)
a.NotError(err).Equal(b, val)
})
}

// XMLBody body 转换成 XML 对象之后是否等价于 val
//
// Deprecated: 下个版本将会被删除。
func (resp *Response) XMLBody(val interface{}) *Response {
resp.a.TB().Helper()
return resp.BodyFunc(func(a *assert.Assertion, body []byte) {
a.TB().Helper()

// NOTE: 应当始终将 body 转换 val 相同的类型,然后再比较对象,
// 因为 val 转换成字符串,可能因为空格缩进等原因,未必会与 body 是相同的。
b, err := UnmarshalObject(body, val, xml.Unmarshal)
a.NotError(err).Equal(b, val)
})
return resp.assert(len(resp.body) == 0, assert.NewFailure("BodyEmpty", msg, map[string]interface{}{"body": resp.body}))
}

// BodyFunc 指定对 body 内容的断言方式
Expand All @@ -182,19 +148,3 @@ func (resp *Response) BodyFunc(f func(a *assert.Assertion, body []byte)) *Respon

return resp
}

// UnmarshalObject 将 data 以 u 作为转换方式转换成与 val 相同的类型
//
// 如果 val 是指针,则会转换成其指向的类型,返回的对象是指针类型。
func UnmarshalObject(data []byte, val interface{}, u func([]byte, interface{}) error) (interface{}, error) {
t := reflect.TypeOf(val)
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
bv := reflect.New(t)

if err := u(data, bv.Interface()); err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
return bv.Interface(), nil
}
15 changes: 2 additions & 13 deletions rest/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"testing"

"github.com/issue9/assert/v3"
"github.com/issue9/assert/v4"
)

func TestRequest_Do(t *testing.T) {
Expand Down Expand Up @@ -42,13 +42,12 @@ func TestResponse(t *testing.T) {
srv.NewRequest(http.MethodGet, "/body").
Header("content-type", "application/json").
Query("page", "5").
JSONBody(&bodyTest{ID: 5}).
StringBody(`{"id":5}`).
Do(nil).
Status(http.StatusCreated).
NotStatus(http.StatusNotFound).
Header("content-type", "application/json;charset=utf-8").
NotHeader("content-type", "invalid value").
JSONBody(&bodyTest{ID: 6}).
Body([]byte(`{"id":6}`)).
StringBody(`{"id":6}`).
BodyNotEmpty()
Expand All @@ -59,14 +58,4 @@ func TestResponse(t *testing.T) {
Status(http.StatusCreated).
NotHeader("content-type", "invalid value").
BodyEmpty()

// xml

srv.NewRequest(http.MethodGet, "/body").
Header("content-type", "application/xml").
XMLBody(&bodyTest{ID: 5}).
Do(nil).
Success(http.StatusCreated).
Header("content-type", "application/xml;charset=utf-8").
XMLBody(&bodyTest{ID: 6})
}
2 changes: 1 addition & 1 deletion rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"net/http"
"net/http/httptest"

"github.com/issue9/assert/v3"
"github.com/issue9/assert/v4"
)

// BuildHandler 生成用于测试的 [http.Handler] 对象
Expand Down
2 changes: 1 addition & 1 deletion rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strings"
"testing"

"github.com/issue9/assert/v3"
"github.com/issue9/assert/v4"
)

type bodyTest struct {
Expand Down
2 changes: 1 addition & 1 deletion rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"net/http/httptest"

"github.com/issue9/assert/v3"
"github.com/issue9/assert/v4"
)

// Server 测试服务
Expand Down
2 changes: 1 addition & 1 deletion rest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"testing"

"github.com/issue9/assert/v3"
"github.com/issue9/assert/v4"
)

func TestNew(t *testing.T) {
Expand Down

0 comments on commit d6443e7

Please sign in to comment.