diff --git a/go.mod b/go.mod index ec2ef52..e089a06 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/issue9/assert/v3 +module github.com/issue9/assert/v4 go 1.17 diff --git a/rest/request.go b/rest/request.go index 82095b0..7df937b 100644 --- a/rest/request.go +++ b/rest/request.go @@ -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 请求的参数封装 @@ -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() @@ -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 { diff --git a/rest/request_test.go b/rest/request_test.go index 4349e64..110c9a8 100644 --- a/rest/request_test.go +++ b/rest/request_test.go @@ -8,7 +8,7 @@ import ( "net/http" "testing" - "github.com/issue9/assert/v3" + "github.com/issue9/assert/v4" ) func TestRequest_buildPath(t *testing.T) { diff --git a/rest/response.go b/rest/response.go index 03f9c3a..19f673e 100644 --- a/rest/response.go +++ b/rest/response.go @@ -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 测试请求的返回结构 @@ -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 { @@ -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 不相等 @@ -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 报文内容是否不为空 @@ -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 内容的断言方式 @@ -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 -} diff --git a/rest/response_test.go b/rest/response_test.go index fbd81a0..0ccd122 100644 --- a/rest/response_test.go +++ b/rest/response_test.go @@ -8,7 +8,7 @@ import ( "net/http" "testing" - "github.com/issue9/assert/v3" + "github.com/issue9/assert/v4" ) func TestRequest_Do(t *testing.T) { @@ -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() @@ -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}) } diff --git a/rest/rest.go b/rest/rest.go index a3b2bd7..069ab8e 100644 --- a/rest/rest.go +++ b/rest/rest.go @@ -12,7 +12,7 @@ import ( "net/http" "net/http/httptest" - "github.com/issue9/assert/v3" + "github.com/issue9/assert/v4" ) // BuildHandler 生成用于测试的 [http.Handler] 对象 diff --git a/rest/rest_test.go b/rest/rest_test.go index 0f3bd24..9e075f2 100644 --- a/rest/rest_test.go +++ b/rest/rest_test.go @@ -14,7 +14,7 @@ import ( "strings" "testing" - "github.com/issue9/assert/v3" + "github.com/issue9/assert/v4" ) type bodyTest struct { diff --git a/rest/server.go b/rest/server.go index 17b7239..99922af 100644 --- a/rest/server.go +++ b/rest/server.go @@ -8,7 +8,7 @@ import ( "net/http" "net/http/httptest" - "github.com/issue9/assert/v3" + "github.com/issue9/assert/v4" ) // Server 测试服务 diff --git a/rest/server_test.go b/rest/server_test.go index ad982bd..923bac2 100644 --- a/rest/server_test.go +++ b/rest/server_test.go @@ -8,7 +8,7 @@ import ( "net/http" "testing" - "github.com/issue9/assert/v3" + "github.com/issue9/assert/v4" ) func TestNew(t *testing.T) {