This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
200 lines (179 loc) · 4.55 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package yolp
import (
"encoding/xml"
"errors"
"net/url"
"os"
"reflect"
"testing"
gock "gopkg.in/h2non/gock.v1"
)
type Test struct {
expected interface{}
actual interface{}
}
func (test Test) Compare(t *testing.T) {
if test.expected != test.actual {
t.Errorf(`Expected "%v" but got "%v"`, test.expected, test.actual)
}
}
func (test Test) DeepEqual(t *testing.T) {
if !reflect.DeepEqual(test.expected, test.actual) {
t.Errorf(`Expected "%v" but got "%v"`, test.expected, test.actual)
}
}
func TestNew(t *testing.T) {
client, _ := New("APPID", "SECRET")
for _, test := range []Test{
{"APPID", client.AppID},
{"SECRET", client.Secret},
} {
test.Compare(t)
}
}
func TestNewEmptyAppID(t *testing.T) {
client, err := New("", "SECRET")
Test{"AppID is not specified", err.Error()}.Compare(t)
if client != nil {
t.Errorf(`Expected nil but got "%v"`, client)
}
}
func TestNewEmptySecret(t *testing.T) {
client, err := New("APPID", "")
Test{"Secret is not specified", err.Error()}.Compare(t)
if client != nil {
t.Errorf(`Expected nil but got "%v"`, client)
}
}
func TestNewFromEnvionment(t *testing.T) {
os.Setenv("YDN_APP_ID", "APPID")
os.Setenv("YDN_SECRET", "SECRET")
client, _ := NewFromEnvionment()
for _, test := range []Test{
{"APPID", client.AppID},
{"SECRET", client.Secret},
} {
test.Compare(t)
}
}
type mockRequest struct {
method string
}
func (mop mockRequest) Query() url.Values {
return url.Values{
"foo": []string{"bar"},
}
}
func (mop mockRequest) HTTPMethod() string {
if mop.method == "" {
return "GET"
}
return mop.method
}
func (mop mockRequest) Endpoint() string {
return "https://foo.com/bar/baz"
}
type mockResponse struct {
XMLName xml.Name `xml:"mock"`
Result string `xml:"result"`
}
func TestClientURL(t *testing.T) {
client, _ := New("APP", "SEC")
op := mockRequest{}
Test{
"https://foo.com/bar/baz?appid=APP&foo=bar&output=xml",
client.URL(op).String(),
}.Compare(t)
}
func TestDoGetRequest(t *testing.T) {
defer gock.Off()
gock.DisableNetworking()
gock.New("https://foo.com/bar/baz?appid=APP&foo=bar&output=xml").
Reply(200).
BodyString("<mock><result>OK</result></mock>")
client, _ := New("APP", "SEC")
mockOp := &mockRequest{}
mockResp := mockResponse{}
res, err := client.DoRequest(mockOp, &mockResp)
if err != nil {
t.Errorf("Expected nil but got %v", err)
}
Test{200, res.StatusCode}.Compare(t)
Test{"OK", mockResp.Result}.Compare(t)
}
func TestDoPostRequest(t *testing.T) {
defer gock.Off()
gock.DisableNetworking()
gock.New("https://foo.com").
Post("/bar/baz").
BodyString("appid=APP&foo=bar&output=xml").
Reply(200).
BodyString("<mock><result>OK</result></mock>")
client, _ := New("APP", "SEC")
mockOp := &mockRequest{
method: "POST",
}
mockResp := mockResponse{}
res, err := client.DoRequest(mockOp, &mockResp)
if err != nil {
t.Errorf("Expected nil but got %v", err)
}
Test{200, res.StatusCode}.Compare(t)
Test{"OK", mockResp.Result}.Compare(t)
}
func TestDoInvalidMethodRequest(t *testing.T) {
client, _ := New("APP", "SEC")
mockOp := &mockRequest{
method: "DELETE",
}
mockResp := mockResponse{}
res, err := client.DoRequest(mockOp, &mockResp)
Test{"Unsupported HTTP method: DELETE", err.Error()}.Compare(t)
if res != nil {
t.Errorf("Expected nil but got %v", res)
}
}
func TestDoHTTPError(t *testing.T) {
gock.New("https://foo.com/bar/baz?appid=APP&foo=bar&output=xml").
ReplyError(errors.New("oops"))
client, _ := New("APP", "SEC")
mockOp := &mockRequest{}
mockResp := mockResponse{}
res, err := client.DoRequest(mockOp, &mockResp)
Test{
"Get https://foo.com/bar/baz?appid=APP&foo=bar&output=xml: oops",
err.Error()}.Compare(t)
if res != nil {
t.Errorf("Expected nil but got %v", res)
}
}
func TestDoInvalidXML(t *testing.T) {
gock.New("https://foo.com/bar/baz?foo=bar&output=xml").
Reply(200).
BodyString("<invalidmock><result>OK</result></invalidmock>")
client, _ := New("APP", "SEC")
mockOp := &mockRequest{}
mockResp := mockResponse{}
res, err := client.DoRequest(mockOp, &mockResp)
Test{
"expected element type <mock> but have <invalidmock>",
err.Error()}.Compare(t)
if res != nil {
t.Errorf("Expected nil but got %v", res)
}
}
func TestDoErrorResponse(t *testing.T) {
gock.New("https://foo.com/bar/baz?foo=bar&output=xml").
Reply(200).
File("_fixtures/error.xml")
client, _ := New("APP", "SEC")
mockOp := &mockRequest{}
mockResp := mockResponse{}
res, err := client.DoRequest(mockOp, &mockResp)
Test{
"Your Request was Forbidden",
err.Error()}.Compare(t)
if res != nil {
t.Errorf("Expected nil but got %v", res)
}
}