-
Notifications
You must be signed in to change notification settings - Fork 53
/
form_test.go
216 lines (209 loc) · 4.75 KB
/
form_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package api
import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"testing"
)
func TestFormResponse_Get(t *testing.T) {
tests := []struct {
name string
response FormResponse
key string
want string
}{
{
name: "blank",
response: FormResponse{},
key: "access_token",
want: "",
},
{
name: "with value",
response: FormResponse{
values: url.Values{
"access_token": []string{"ATOKEN"},
},
},
key: "access_token",
want: "ATOKEN",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.response.Get(tt.key); got != tt.want {
t.Errorf("FormResponse.Get() = %v, want %v", got, tt.want)
}
})
}
}
func TestFormResponse_Err(t *testing.T) {
tests := []struct {
name string
response FormResponse
wantErr Error
errorMsg string
}{
{
name: "blank",
response: FormResponse{},
wantErr: Error{},
errorMsg: "HTTP 0",
},
{
name: "with values",
response: FormResponse{
StatusCode: 422,
requestURI: "http://example.com/path",
values: url.Values{
"error": []string{"try_again"},
"error_description": []string{"maybe it works later"},
},
},
wantErr: Error{
Code: "try_again",
ResponseCode: 422,
RequestURI: "http://example.com/path",
},
errorMsg: "maybe it works later (try_again)",
},
{
name: "no values",
response: FormResponse{
StatusCode: 422,
requestURI: "http://example.com/path",
},
wantErr: Error{
Code: "",
ResponseCode: 422,
RequestURI: "http://example.com/path",
},
errorMsg: "HTTP 422",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.response.Err()
if err == nil {
t.Fatalf("FormResponse.Err() = %v, want %v", nil, tt.wantErr)
}
apiError := err.(*Error)
if apiError.Code != tt.wantErr.Code {
t.Errorf("Error.Code = %v, want %v", apiError.Code, tt.wantErr.Code)
}
if apiError.ResponseCode != tt.wantErr.ResponseCode {
t.Errorf("Error.ResponseCode = %v, want %v", apiError.ResponseCode, tt.wantErr.ResponseCode)
}
if apiError.RequestURI != tt.wantErr.RequestURI {
t.Errorf("Error.RequestURI = %v, want %v", apiError.RequestURI, tt.wantErr.RequestURI)
}
if apiError.Error() != tt.errorMsg {
t.Errorf("Error.Error() = %q, want %q", apiError.Error(), tt.errorMsg)
}
})
}
}
type apiClient struct {
status int
body string
contentType string
postCount int
}
func (c *apiClient) PostForm(u string, params url.Values) (*http.Response, error) {
c.postCount++
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString(c.body)),
Header: http.Header{
"Content-Type": {c.contentType},
},
StatusCode: c.status,
}, nil
}
func TestPostForm(t *testing.T) {
type args struct {
url string
params url.Values
}
tests := []struct {
name string
args args
http apiClient
want *FormResponse
wantErr bool
}{
{
name: "success urlencoded",
args: args{
url: "https://github.com/oauth",
},
http: apiClient{
body: "access_token=123abc&scopes=repo%20gist",
status: 200,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
},
want: &FormResponse{
StatusCode: 200,
requestURI: "https://github.com/oauth",
values: url.Values{
"access_token": {"123abc"},
"scopes": {"repo gist"},
},
},
wantErr: false,
},
{
name: "success JSON",
args: args{
url: "https://github.com/oauth",
},
http: apiClient{
body: `{"access_token":"123abc", "scopes":"repo gist"}`,
status: 200,
contentType: "application/json; charset=utf-8",
},
want: &FormResponse{
StatusCode: 200,
requestURI: "https://github.com/oauth",
values: url.Values{
"access_token": {"123abc"},
"scopes": {"repo gist"},
},
},
wantErr: false,
},
{
name: "HTML response",
args: args{
url: "https://github.com/oauth",
},
http: apiClient{
body: "<h1>Something went wrong</h1>",
status: 502,
contentType: "text/html",
},
want: &FormResponse{
StatusCode: 502,
requestURI: "https://github.com/oauth",
values: url.Values(nil),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := PostForm(&tt.http, tt.args.url, tt.args.params)
if (err != nil) != tt.wantErr {
t.Errorf("PostForm() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.http.postCount != 1 {
t.Errorf("expected PostForm to happen 1 time; happened %d times", tt.http.postCount)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("PostForm() = %v, want %v", got, tt.want)
}
})
}
}