-
Notifications
You must be signed in to change notification settings - Fork 11
/
params_test.go
118 lines (100 loc) · 2.81 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
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
package golangsdk
import (
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuildURL(t *testing.T) {
type testCase struct {
Name string
isFail bool
Endpoints []string
QueryParams interface{}
}
type network struct {
Name string `q:"display_name"`
Status string `q:"status"`
NetworkID string `q:"network_id"`
}
type empty struct{}
type withoutTags struct {
Name string
}
cases := []testCase{
{
Name: "positive case without query parameters",
Endpoints: []string{"servers"},
QueryParams: nil,
},
{
Name: "positive case with query parameters",
Endpoints: []string{"servers", "network"},
QueryParams: &network{
Name: "network_name",
Status: "active",
NetworkID: "7bf9e4a9-fab1-4415-a6f7-ff7284ee1870",
},
},
{
Name: "positive case with empty struct, without query parameters",
Endpoints: []string{"servers", "network"},
QueryParams: &empty{},
},
{
Name: "positive case with struct without query tags",
Endpoints: []string{"servers", "network"},
QueryParams: &withoutTags{Name: "anyname"},
},
{
Name: "negative case with slash '/' in the endpoint name",
isFail: true,
Endpoints: []string{"servers/"},
},
{
Name: "negative case with slash '/' in the second endpoint name",
isFail: true,
Endpoints: []string{"servers", "/asdf/"},
QueryParams: nil,
},
{
Name: "negative case with characters ' /!?$#=&+_\"' ' in the second endpoint name",
isFail: true,
Endpoints: []string{"servers", "/!?$#=&+_\"'"},
QueryParams: nil,
},
{
Name: "negative case then query params is not a struct",
isFail: true,
Endpoints: []string{"servers", "network"},
QueryParams: "anystring",
},
}
for _, c := range cases {
t.Log("starting test case:", c.Name)
u, err := NewURLBuilder().WithEndpoints(c.Endpoints...).WithQueryParams(c.QueryParams).Build()
if c.isFail {
assert.Error(t, err)
continue
}
assert.NoError(t, err)
uObj, err := url.Parse(u.String())
assert.NoError(t, err)
assert.Equal(t, strings.Join(c.Endpoints, "/"), uObj.Path)
if c.QueryParams == nil {
continue
}
if c.Name == "positive case with struct without query tags" ||
c.Name == "positive case with empty struct, without query parameters" {
q := uObj.Query()
assert.Equal(t, 0, len(q))
continue
}
assert.Equal(t, true, uObj.Query().Has("display_name"))
assert.Equal(t, "network_name", uObj.Query().Get("display_name"))
assert.Equal(t, true, uObj.Query().Has("status"))
assert.Equal(t, "active", uObj.Query().Get("status"))
assert.Equal(t, true, uObj.Query().Has("network_id"))
assert.Equal(t, "7bf9e4a9-fab1-4415-a6f7-ff7284ee1870", uObj.Query().Get("network_id"))
}
}