diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bb3a4f7..87a34e0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # developing - Add http client and option func. [4455](https://github.com/beego/beego/issues/4455) +- Add: Convenient way to generate mock object [4620](https://github.com/beego/beego/issues/4620) - Infra: use dependabot to update dependencies. [4623](https://github.com/beego/beego/pull/4623) - Lint: use golangci-lint. [4619](https://github.com/beego/beego/pull/4619) - Chore: format code. [4615](https://github.com/beego/beego/pull/4615) diff --git a/client/httplib/client_option.go b/client/httplib/client_option.go index db81ec50..f970e67d 100644 --- a/client/httplib/client_option.go +++ b/client/httplib/client_option.go @@ -21,8 +21,10 @@ import ( "time" ) -type ClientOption func(client *Client) -type BeegoHTTPRequestOption func(request *BeegoHTTPRequest) +type ( + ClientOption func(client *Client) + BeegoHTTPRequestOption func(request *BeegoHTTPRequest) +) // WithEnableCookie will enable cookie in all subsequent request func WithEnableCookie(enable bool) ClientOption { diff --git a/client/httplib/httpclient.go b/client/httplib/httpclient.go index 45916a40..32ab4d6c 100644 --- a/client/httplib/httpclient.go +++ b/client/httplib/httpclient.go @@ -30,9 +30,9 @@ type Client struct { Setting BeegoHTTPSettings } -// HTTPResponseCarrier If value implement HTTPResponseCarrier. http.Response will pass to SetHttpResponse +// HTTPResponseCarrier If value implement HTTPResponseCarrier. http.Response will pass to SetHTTPResponse type HTTPResponseCarrier interface { - SetHttpResponse(resp *http.Response) + SetHTTPResponse(resp *http.Response) } // HTTPBodyCarrier If value implement HTTPBodyCarrier. http.Response.Body will pass to SetReader @@ -52,7 +52,7 @@ type HTTPStatusCarrier interface { } // HttpHeaderCarrier If value implement HttpHeaderCarrier. http.Response.Header will pass to SetHeader -type HttpHeadersCarrier interface { +type HTTPHeadersCarrier interface { SetHeader(header map[string][]string) } @@ -91,6 +91,7 @@ func (c *Client) handleResponse(value interface{}, req *BeegoHTTPRequest) error // handleCarrier set http data to value func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error { resp, err := req.Response() + defer resp.Body.Close() if err != nil { return err } @@ -104,7 +105,7 @@ func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error { return err } resp.Body = ioutil.NopCloser(bytes.NewReader(b)) - carrier.SetHttpResponse(resp) + carrier.SetHTTPResponse(resp) } if carrier, ok := value.(HTTPBodyCarrier); ok { b, err := req.Bytes() @@ -124,7 +125,7 @@ func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error { if carrier, ok := value.(HTTPStatusCarrier); ok { carrier.SetStatusCode(resp.StatusCode) } - if carrier, ok := value.(HttpHeadersCarrier); ok { + if carrier, ok := value.(HTTPHeadersCarrier); ok { carrier.SetHeader(resp.Header) } return nil diff --git a/client/httplib/httpclient_test.go b/client/httplib/httpclient_test.go index e13bdf74..6437e534 100644 --- a/client/httplib/httpclient_test.go +++ b/client/httplib/httpclient_test.go @@ -41,7 +41,7 @@ type slideShowResponse struct { Slideshow slideshow `json:"slideshow" yaml:"slideshow"` } -func (r *slideShowResponse) SetHttpResponse(resp *http.Response) { +func (r *slideShowResponse) SetHTTPResponse(resp *http.Response) { r.Resp = resp } diff --git a/client/httplib/httplib.go b/client/httplib/httplib.go index be4f388e..6a78e13b 100644 --- a/client/httplib/httplib.go +++ b/client/httplib/httplib.go @@ -49,7 +49,6 @@ import ( "strings" "time" - xrv "github.com/mattermost/xml-roundtrip-validator" "gopkg.in/yaml.v2" "github.com/beego/beego/v2/core/berror" @@ -639,10 +638,6 @@ func (b *BeegoHTTPRequest) ToXML(v interface{}) error { if err != nil { return err } - if err := xrv.Validate(bytes.NewReader(data)); err != nil { - return err - } - return berror.Wrap(xml.Unmarshal(data, v), UnmarshalXMLResponseToObjectFailed, "unmarshal xml body to object failed.") } @@ -668,6 +663,7 @@ func (b *BeegoHTTPRequest) ToValue(value interface{}) error { } resp, err := b.Response() + defer resp.Body.Close() if err != nil { return err } diff --git a/go.mod b/go.mod index abef3a2b..99b24947 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,6 @@ require ( github.com/hashicorp/golang-lru v0.5.4 github.com/ledisdb/ledisdb v0.0.0-20200510135210-d35789ec47e6 github.com/lib/pq v1.10.2 - github.com/mattermost/xml-roundtrip-validator v0.1.0 github.com/mattn/go-sqlite3 v1.14.7 github.com/mitchellh/mapstructure v1.4.1 github.com/opentracing/opentracing-go v1.2.0 diff --git a/go.sum b/go.sum index a60bd832..91616ec7 100644 --- a/go.sum +++ b/go.sum @@ -226,8 +226,6 @@ github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= -github.com/mattermost/xml-roundtrip-validator v0.1.0 h1:RXbVD2UAl7A7nOTR4u7E3ILa4IbtvKBHw64LDsmu9hU= -github.com/mattermost/xml-roundtrip-validator v0.1.0/go.mod h1:qccnGMcpgwcNaBnxqpJpWWUiPNr5H3O8eDgGV9gT5To= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=