From ac30dee7bfcddc472593760d44037db47161a66a Mon Sep 17 00:00:00 2001 From: lixizan <lixizan@webull.com> Date: Mon, 23 May 2022 09:37:46 +0800 Subject: [PATCH] api test --- encoding.go | 5 ++-- go.mod | 1 + go.sum | 1 + request.go | 3 +- request_test.go | 67 +++++++++++++++++++++++++++++++++++-------- response.go | 5 ++-- server/test_server.go | 27 ++++++++++------- 7 files changed, 81 insertions(+), 28 deletions(-) diff --git a/encoding.go b/encoding.go index a46f584..f7388ff 100644 --- a/encoding.go +++ b/encoding.go @@ -1,10 +1,11 @@ package hasaki import ( - jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" "net/url" "reflect" + + jsoniter "github.com/json-iterator/go" + "github.com/pkg/errors" ) type Any map[string]interface{} diff --git a/go.mod b/go.mod index 6256ee4..2eae90c 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,5 @@ go 1.16 require ( github.com/json-iterator/go v1.1.10 github.com/pkg/errors v0.9.1 + github.com/stretchr/testify v1.3.0 ) diff --git a/go.sum b/go.sum index 9de2bea..c1df9db 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/request.go b/request.go index 55a3d20..38c941d 100644 --- a/request.go +++ b/request.go @@ -3,12 +3,13 @@ package hasaki import ( "bytes" "fmt" - "github.com/pkg/errors" "io" "net/http" neturl "net/url" "strings" "time" + + "github.com/pkg/errors" ) var DefaultClient = &http.Client{ diff --git a/request_test.go b/request_test.go index c236d6f..9cebf90 100644 --- a/request_test.go +++ b/request_test.go @@ -1,22 +1,65 @@ package hasaki -import "testing" +import ( + "bytes" + "io" + "net/http" + "testing" + + jsoniter "github.com/json-iterator/go" + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" +) + +type responseBody struct { + Code int `json:"code"` + Message string `json:"message"` +} + +func (this responseBody) Error() string { + return this.Message +} func TestNewRequest(t *testing.T) { const baseurl = "http://localhost:9000" - if _, err := Get(baseurl + "/p1").Send(nil).GetBody(); err != nil { - t.Error(err.Error()) - } - if _, err := Get(baseurl + "/p2").Send(nil).GetBody(); err == nil { - t.Error(err.Error()) - } + var checker ErrorChecker = func(resp *http.Response) error { + if resp.StatusCode != http.StatusOK { + return errors.New("StatusCodeError") + } - if _, err := Get(baseurl + "/p3").Send(nil).GetBody(); err == nil { - t.Error(err.Error()) - } + var buf = bytes.NewBuffer(nil) + if _, err := io.Copy(buf, resp.Body); err != nil { + return err + } + resp.Body.Close() + var body = &responseBody{} + jsoniter.Unmarshal(buf.Bytes(), body) + if body.Code != http.StatusOK { + return body + } - if _, err := Get(baseurl + "/p4").Send(nil).GetBody(); err == nil { - t.Error(err.Error()) + resp.Body = io.NopCloser(buf) + return nil } + + var as = assert.New(t) + + t.Run("StatusOK", func(t *testing.T) { + body, err := Post(baseurl + "/200"). + SetErrorChecker(checker). + Send(nil). + GetBody() + as.NoError(err) + t.Logf("body: %s", string(body)) + }) + + t.Run("StatusBadRequest", func(t *testing.T) { + _, err := Post(baseurl + "/400"). + SetErrorChecker(checker). + Send(nil). + GetBody() + as.Error(err) + t.Logf("err: %v", err) + }) } diff --git a/response.go b/response.go index 9fe6430..caf0fff 100644 --- a/response.go +++ b/response.go @@ -1,10 +1,11 @@ package hasaki import ( - "github.com/json-iterator/go" - "github.com/pkg/errors" "io/ioutil" "net/http" + + jsoniter "github.com/json-iterator/go" + "github.com/pkg/errors" ) type ErrorChecker func(resp *http.Response) error diff --git a/server/test_server.go b/server/test_server.go index 56b509a..a10ca7b 100644 --- a/server/test_server.go +++ b/server/test_server.go @@ -1,12 +1,15 @@ package main import ( - jsoniter "github.com/json-iterator/go" "net/http" + + jsoniter "github.com/json-iterator/go" ) -var response = map[string]interface{}{ - "success": true, +type ResponseBody struct { + Code int `json:"code"` + Message string `json:"message"` + Data interface{} `json:"data"` } func WriteJson(writer http.ResponseWriter, code int, v interface{}) error { @@ -21,16 +24,18 @@ func WriteJson(writer http.ResponseWriter, code int, v interface{}) error { } func main() { - http.HandleFunc("/p1", func(writer http.ResponseWriter, request *http.Request) { - WriteJson(writer, 200, response) - }) - - http.HandleFunc("/p2", func(writer http.ResponseWriter, request *http.Request) { - WriteJson(writer, 400, response) + http.HandleFunc("/200", func(writer http.ResponseWriter, request *http.Request) { + WriteJson(writer, http.StatusOK, ResponseBody{ + Code: http.StatusOK, + Message: "success", + }) }) - http.HandleFunc("/p4", func(writer http.ResponseWriter, request *http.Request) { - WriteJson(writer, 500, response) + http.HandleFunc("/400", func(writer http.ResponseWriter, request *http.Request) { + WriteJson(writer, http.StatusOK, ResponseBody{ + Code: http.StatusBadRequest, + Message: "StatusBadRequest", + }) }) http.ListenAndServe(":9000", nil)