-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
106 lines (79 loc) · 2.27 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
// +build !integration
package linode
import (
"bytes"
"errors"
"io"
"net/http"
"net/url"
"testing"
"github.com/alexsacr/linode/_third_party/testify/assert"
"github.com/alexsacr/linode/_third_party/testify/require"
)
func TestClientSerialization(t *testing.T) {
// Just check the paths that aren't exercised during normal usage.
// Non-nil bool
capturePost := func(_ string, v url.Values) (*http.Response, error) {
assert.Equal(t, "true", v.Get("foo"))
assert.Equal(t, "false", v.Get("bar"))
return nil, errors.New("bail")
}
c := NewClient("")
c.post = capturePost
args := make(map[string]interface{})
args["foo"] = true
args["bar"] = false
_, _ = c.apiCall("testing", args)
// Unsupported
args = make(map[string]interface{})
args["foo"] = []struct{}{}
_, err := c.apiCall("testing", args)
assert.Error(t, err)
}
type errReadCloser struct{}
func (e errReadCloser) Read(_ []byte) (int, error) {
return 0, errors.New("foo")
}
func (e errReadCloser) Close() error {
return nil
}
func TestClientReadError(t *testing.T) {
bodyErrPost := func(_ string, _ url.Values) (*http.Response, error) {
return &http.Response{Body: errReadCloser{}}, nil
}
c := NewClient("")
c.post = bodyErrPost
_, err := c.apiCall("foo", map[string]interface{}{})
assert.Error(t, err)
}
type nopCloser struct {
io.Reader
}
func (nopCloser) Close() error {
return nil
}
func TestClientJSONUnmarshalError(t *testing.T) {
badJSONPost := func(_ string, _ url.Values) (*http.Response, error) {
return &http.Response{Body: nopCloser{bytes.NewBufferString("<")}}, nil
}
c := NewClient("")
c.post = badJSONPost
_, err := c.apiCall("foo", map[string]interface{}{})
assert.Error(t, err)
}
func mockClientAPIError() []mockAPIResponse {
var output string
var params map[string]string
var responses []mockAPIResponse
output = `{"ERRORARRAY":[{"ERRORCODE":4,"ERRORMESSAGE":"Authentication failed"}],"DATA":{},"ACTION":"test.echo"}`
params = map[string]string{}
responses = append(responses, newMockAPIResponse("test.echo", params, output))
return responses
}
func TestClientAPIError(t *testing.T) {
c, ts := clientFor(newMockAPIServer(t, mockClientAPIError()))
defer ts.Close()
err := c.TestEcho()
require.Error(t, err)
assert.Equal(t, "api: 4: Authentication failed", err.Error())
}