forked from griffin-stewie/go-backlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
90 lines (85 loc) · 2.02 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
package gobacklog
import (
"net/http"
"net/url"
"testing"
)
// func TestNewClientAdjustedBaseURL(t *testing.T) {
// tab := []struct {
// BaseURL string
// AdjustedURL string
// }{
// {
// BaseURL: "http://example.com/",
// AdjustedURL: "http://example.com",
// },
// {
// BaseURL: "http://example.com",
// AdjustedURL: "http://example.com",
// },
// {
// BaseURL: "",
// AdjustedURL: "",
// },
// }
// for _, v := range tab {
// url, _ := url.Parse(v.BaseURL)
// c := NewClient(url, "")
// if c.BaseURL.String() != v.AdjustedURL {
// t.Errorf(`NewClient(%q, ""): BaseURL = %q; want %q`, v.BaseURL, c.BaseURL, v.AdjustedURL)
// }
// }
// }
func values(kv map[string][]string) url.Values {
pairs := url.Values{}
for k, v := range kv {
for _, s := range v {
pairs.Add(k, s)
}
}
return pairs
}
func TestResolvingURL(t *testing.T) {
apiKey := "apikey"
tab := []struct {
Endpoint string
BaseURL string
Params url.Values
AdjustedURL string
}{
{
Endpoint: "/api/v2/space",
BaseURL: "http://example.com/",
Params: url.Values{},
AdjustedURL: "http://example.com/api/v2/space?apiKey=apikey",
},
{
Endpoint: "/api/v2/space",
BaseURL: "http://example.com",
Params: url.Values{},
AdjustedURL: "http://example.com/api/v2/space?apiKey=apikey",
},
{
Endpoint: "/api/v2/space",
BaseURL: "http://example.com",
Params: values(map[string][]string{"a": []string{"b"}}),
AdjustedURL: "http://example.com/api/v2/space?a=b&apiKey=apikey",
},
}
for _, v := range tab {
url, _ := url.Parse(v.BaseURL)
c := NewClient(url, apiKey)
// result := c.buildURLWithValues(v.BaseURL, v.Endpoint, v.Params)
result := c.composeURL(v.Endpoint, v.Params)
if result != v.AdjustedURL {
_, requestErr := http.NewRequest("GET",
result,
nil,
)
if requestErr != nil {
t.Error(requestErr)
}
t.Errorf(`Result = %q; BaseURL = %q; want %q`, result, c.BaseURL, v.AdjustedURL)
}
}
}