-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_test.go
108 lines (89 loc) · 2.79 KB
/
client_test.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package httperr
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type roundTripperFunc func(*http.Request) (*http.Response, error)
func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
type testError struct {
Message string `json:"message"`
Code int `json:"code"`
}
func (te testError) Error() string {
return fmt.Sprintf("%s (%d)", te.Message, te.Code)
}
func TestClient(t *testing.T) {
transport := Transport{
Next: roundTripperFunc(func(*http.Request) (*http.Response, error) {
resp := http.Response{}
resp.StatusCode = 400
resp.Body = ioutil.NopCloser(strings.NewReader(`{"message": "cannot frob the grob", "code": 1}`))
return &resp, nil
}),
}
t.Run("nostruct", func(t *testing.T) {
client := http.Client{Transport: transport}
resp, err := client.Get("/foo")
assert.Nil(t, resp)
httpErr := err.(*url.Error).Unwrap().(Response)
assert.Equal(t, "Bad Request", httpErr.Error())
assert.Equal(t, 400, httpErr.StatusCode)
body, err := ioutil.ReadAll(httpErr.Body)
assert.NoError(t, err)
assert.Equal(t, `{"message": "cannot frob the grob", "code": 1}`, string(body))
})
t.Run("unmarshal", func(t *testing.T) {
arg := JSON(testError{})
arg(&transport)
client := http.Client{Transport: transport}
resp, err := client.Get("/foo")
assert.Nil(t, resp)
httpErr := err.(*url.Error).Unwrap().(testError)
assert.Equal(t, "cannot frob the grob (1)", httpErr.Error())
assert.Equal(t, testError{Message: "cannot frob the grob", Code: 1}, httpErr)
})
t.Run("unmarshal bad", func(t *testing.T) {
transport := Transport{
Next: roundTripperFunc(func(*http.Request) (*http.Response, error) {
resp := http.Response{}
resp.StatusCode = 400
resp.Body = ioutil.NopCloser(strings.NewReader(`{invalid json`))
return &resp, nil
}),
}
arg := JSON(testError{})
arg(&transport)
client := http.Client{Transport: transport}
resp, err := client.Get("/foo")
assert.Nil(t, resp)
httpErr := err.(*url.Error).Unwrap().(Response)
assert.Equal(t, "Bad Request", httpErr.Error())
assert.Equal(t, 400, httpErr.StatusCode)
body, err := ioutil.ReadAll(httpErr.Body)
assert.NoError(t, err)
assert.Equal(t, `{invalid json`, string(body))
})
t.Run("success", func(t *testing.T) {
transport := Transport{
Next: roundTripperFunc(func(*http.Request) (*http.Response, error) {
resp := http.Response{}
resp.StatusCode = 200
resp.Body = ioutil.NopCloser(strings.NewReader(`{"ok": true}`))
return &resp, nil
}),
}
client := http.Client{Transport: transport}
resp, err := client.Get("/foo")
assert.NoError(t, err)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, `{"ok": true}`, string(body))
})
}