-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_test.go
More file actions
198 lines (156 loc) · 4.04 KB
/
request_test.go
File metadata and controls
198 lines (156 loc) · 4.04 KB
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
package darksky
import (
"fmt"
"net/http"
"net/url"
"strings"
"testing"
"time"
)
func TestForecastRequest(t *testing.T) {
err := testForecastRequest()
if err != nil {
t.Error(err)
}
}
func TestForecastRequestWithSingleOptions(t *testing.T) {
opts := []Option{
LanguageOption("fr"),
ExcludeOption("minutely", "hourly"),
ExtendOption(),
UnitOption("ca"),
}
for _, opt := range opts {
err := testForecastRequest(opt)
if err != nil {
t.Error(err)
}
}
}
func TestForecastRequestWithMultipleOptions(t *testing.T) {
opts := []Option{
LanguageOption("fr"),
ExcludeOption("minutely", "hourly"),
ExtendOption(),
UnitOption("ca"),
}
err := testForecastRequest(opts...)
if err != nil {
t.Error(err)
}
}
func TestTimeMachineRequest(t *testing.T) {
err := testTimeMachineRequest()
if err != nil {
t.Error(err)
}
}
func TestTimeMachineRequestWithSingleOption(t *testing.T) {
opts := []Option{
LanguageOption("fr"),
ExcludeOption("minutely", "hourly"),
ExtendOption(),
UnitOption("ca"),
}
for _, opt := range opts {
err := testTimeMachineRequest(opt)
if err != nil {
t.Error(err)
}
}
}
func TestTimeMachineRequestWithMultipleOptions(t *testing.T) {
opts := []Option{
LanguageOption("fr"),
ExcludeOption("minutely", "hourly"),
ExtendOption(),
UnitOption("ca"),
}
err := testTimeMachineRequest(opts...)
if err != nil {
t.Error(err)
}
}
func TestUnsupportedLanguageOption(t *testing.T) {
testOptionError(t, ErrLanguageNotSupported, LanguageOption("zzz"))
}
func TestUnSupportedExcludeOption(t *testing.T) {
ex := []string{"zzz"}
testOptionError(t, newOptionError("zzz"), ExcludeOption(ex...))
}
func TestNotUniqueExcludeOption(t *testing.T) {
for _, se := range supportedExclude {
values := []string{se, strings.ToUpper(se)}
testOptionError(t, ErrExcludeOptionNotUnique, ExcludeOption(values...))
}
}
func TestUnsupportedUnit(t *testing.T) {
testOptionError(t, ErrUnitNotSupported, UnitOption("zzz"))
}
func TestLanguageUpperCaseOption(t *testing.T) {
for _, sl := range supportedLanguages {
values := make(url.Values)
option := LanguageOption(strings.ToUpper(sl))
err := option(&values)
if err != nil {
t.Error("Should not return an error, only case do not match.")
}
if values.Get("lang") != sl {
t.Error("Language option should have been converted to lower case.")
}
}
}
func TestUnitCaseOption(t *testing.T) {
for _, su := range supportedUnits {
values := make(url.Values)
option := UnitOption(strings.ToUpper(su))
err := option(&values)
if err != nil {
t.Error("Should not return an error, only case do not match.")
}
if values.Get("units") != su {
t.Error("Unit option should have been converted to lower case.")
}
}
}
func testForecastRequest(opts ...Option) error {
r, err := newForecastRequest(defaultSecret, defaultLat, defaultLng, opts)
if err != nil {
return err
}
return validateURL(r, defaultForecastURL, opts)
}
func testTimeMachineRequest(opts ...Option) error {
t := time.Now()
ts := int32(t.Unix())
r, err := newTimeMachineRequest(defaultSecret, defaultLat, defaultLng, t, opts)
if err != nil {
return err
}
return validateURL(r, fmt.Sprintf(defaultTimeMachineURL, ts), opts)
}
func testOptionError(t *testing.T, expectedError error, opts ...Option) {
_, err := newForecastRequest(defaultSecret, defaultLat, defaultLng, opts)
if err == nil || err.Error() != expectedError.Error() {
t.Errorf("Should have error : %s", expectedError)
}
_, err = newTimeMachineRequest(defaultSecret, defaultLat, defaultLng, time.Now(), opts)
if err == nil || err.Error() != expectedError.Error() {
t.Errorf("Should have error : %s", expectedError)
}
}
func validateURL(r *http.Request, expected string, opts []Option) error {
q := make(url.Values)
for _, opt := range opts {
if err := opt(&q); err != nil {
return err
}
}
if queryString := q.Encode(); queryString != "" {
expected += "?" + q.Encode()
}
if r.URL.String() != expected {
return fmt.Errorf("Request URL should be %s, %s given", expected, r.URL.String())
}
return nil
}