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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req := a.req
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
req = a.req
|
||||||
resp *Response
|
resp *Response
|
||||||
nilResp bool
|
nilResp bool
|
||||||
)
|
)
|
||||||
|
|
||||||
if a.resp == nil {
|
if a.resp == nil {
|
||||||
resp = AcquireResponse()
|
resp = AcquireResponse()
|
||||||
nilResp = true
|
nilResp = true
|
||||||
} else {
|
} else {
|
||||||
resp = a.resp
|
resp = a.resp
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if a.debugWriter != nil {
|
if a.debugWriter != nil {
|
||||||
printDebugInfo(req, resp, a.debugWriter)
|
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
|
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 {
|
if err := a.jsonDecoder(body, v); err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
|
|
|
@ -986,6 +986,21 @@ func Test_Client_Agent_Struct(t *testing.T) {
|
||||||
utils.AssertEqual(t, true, d.Success)
|
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) {
|
t.Run("error", func(t *testing.T) {
|
||||||
a := Get("http://example.com/error")
|
a := Get("http://example.com/error")
|
||||||
|
|
||||||
|
@ -993,8 +1008,7 @@ func Test_Client_Agent_Struct(t *testing.T) {
|
||||||
|
|
||||||
var d data
|
var d data
|
||||||
|
|
||||||
code, body, errs := a.JSONDecoder(json.Unmarshal).
|
code, body, errs := a.JSONDecoder(json.Unmarshal).Struct(&d)
|
||||||
Struct(&d)
|
|
||||||
|
|
||||||
utils.AssertEqual(t, StatusOK, code)
|
utils.AssertEqual(t, StatusOK, code)
|
||||||
utils.AssertEqual(t, `{"success"`, string(body))
|
utils.AssertEqual(t, `{"success"`, string(body))
|
||||||
|
|
Loading…
Reference in New Issue