-
Notifications
You must be signed in to change notification settings - Fork 0
/
params_test.go
74 lines (71 loc) · 1.66 KB
/
params_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
package yfgo
import "testing"
func TestBuildQuery(t *testing.T) {
tests := []struct {
Name string
params []QueryParam
expected string
}{
{"empty", nil, ""},
{"single param", []QueryParam{{Name: "key1", Value: "Value1"}}, "key1=Value1"},
{"multiple params", []QueryParam{
{Name: "key1", Value: "Value1"},
{Name: "key2", Value: "Value2"},
{Name: "key3", Value: "Value3"},
}, "key1=Value1&key2=Value2&key3=Value3"},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
query := BuildQuery(test.params)
if query != test.expected {
t.Errorf("buildQuery returned an incorrect query: %s", test.expected)
}
})
}
}
func TestMakeURL(t *testing.T) {
tests := []struct {
Name string
url string
symbol string
params []QueryParam
expected string
}{
{
"empty params",
"https://example.com",
"stocks",
nil,
"https://example.com/stocks?",
}, {
"single param",
"https://example.com",
"stocks",
[]QueryParam{{Name: "symbol", Value: "AAPL"}},
"https://example.com/stocks?symbol=AAPL",
}, {
"multiple params",
"https://example.com",
"stocks",
[]QueryParam{
{Name: "symbol", Value: "AAPL"},
{Name: "market", Value: "NASDAQ"},
},
"https://example.com/stocks?market=NASDAQ&symbol=AAPL",
}, {
"url and symbol only",
"https://api.example.com",
"dividends",
nil,
"https://api.example.com/dividends?",
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
url := MakeURL(test.url, test.symbol, test.params)
if url != test.expected {
t.Errorf("makeURL returned incorrect URL: %s - %s", url, test.expected)
}
})
}
}