mirror of https://github.com/gofiber/fiber.git
👷 handle pre errors for Struct
parent
1fddaed072
commit
2772af030e
|
@ -728,18 +728,19 @@ func (a *Agent) Bytes() (code int, body []byte, errs []error) {
|
|||
return
|
||||
}
|
||||
|
||||
req := a.req
|
||||
|
||||
var (
|
||||
req = a.req
|
||||
resp *Response
|
||||
nilResp bool
|
||||
)
|
||||
|
||||
if a.resp == nil {
|
||||
resp = AcquireResponse()
|
||||
nilResp = true
|
||||
} else {
|
||||
resp = a.resp
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if a.debugWriter != nil {
|
||||
printDebugInfo(req, resp, a.debugWriter)
|
||||
|
@ -798,7 +799,9 @@ func (a *Agent) Struct(v interface{}) (code int, body []byte, errs []error) {
|
|||
a.jsonDecoder = json.Unmarshal
|
||||
}
|
||||
|
||||
code, body, errs = a.Bytes()
|
||||
if code, body, errs = a.Bytes(); len(errs) > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if err := a.jsonDecoder(body, v); err != nil {
|
||||
errs = append(errs, err)
|
||||
|
|
|
@ -986,6 +986,21 @@ func Test_Client_Agent_Struct(t *testing.T) {
|
|||
utils.AssertEqual(t, true, d.Success)
|
||||
})
|
||||
|
||||
t.Run("pre error", func(t *testing.T) {
|
||||
a := Get("http://example.com")
|
||||
|
||||
a.HostClient.Dial = func(addr string) (net.Conn, error) { return ln.Dial() }
|
||||
|
||||
var d data
|
||||
|
||||
_, body, errs := a.Timeout(time.Nanosecond).Struct(&d)
|
||||
|
||||
utils.AssertEqual(t, "", string(body))
|
||||
utils.AssertEqual(t, 1, len(errs))
|
||||
utils.AssertEqual(t, "timeout", errs[0].Error())
|
||||
utils.AssertEqual(t, false, d.Success)
|
||||
})
|
||||
|
||||
t.Run("error", func(t *testing.T) {
|
||||
a := Get("http://example.com/error")
|
||||
|
||||
|
@ -993,8 +1008,7 @@ func Test_Client_Agent_Struct(t *testing.T) {
|
|||
|
||||
var d data
|
||||
|
||||
code, body, errs := a.JSONDecoder(json.Unmarshal).
|
||||
Struct(&d)
|
||||
code, body, errs := a.JSONDecoder(json.Unmarshal).Struct(&d)
|
||||
|
||||
utils.AssertEqual(t, StatusOK, code)
|
||||
utils.AssertEqual(t, `{"success"`, string(body))
|
||||
|
|
Loading…
Reference in New Issue