-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
44 lines (36 loc) · 926 Bytes
/
errors_test.go
File metadata and controls
44 lines (36 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package httpx_test
import (
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/tflyons/httpx"
)
func badClose(c httpx.Client) httpx.ClientFunc {
return func(req *http.Request) (*http.Response, error) {
resp, err := c.Do(req)
resp.Body = badCloser{resp.Body}
return resp, err
}
}
type badCloser struct {
io.Reader
}
func (badCloser) Close() error { return fmt.Errorf("close body error") }
func TestClient_CloseBodyError(t *testing.T) {
srv := httptest.NewServer(echoHandler)
defer srv.Close()
var c httpx.Client = srv.Client()
// add a function that will cause the call to resp.Body.Close to error
c = httpx.SetRequestBodyJSON(c, map[string]string{})
var out map[string]string
c = badClose(c)
c = httpx.SetResponseBodyHandlerJSON(c, &out)
c = httpx.SetRequest(c, http.MethodPost, srv.URL)
_, err := c.Do(nil)
if !errors.Is(err, httpx.ErrBodyClose) {
t.Fatal(err)
}
}