-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_test.go
110 lines (102 loc) · 3.59 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package cloudability
import (
"net/url"
"testing"
"time"
)
func TestNewClient(t *testing.T) {
duration := 30 * time.Second
userAgent := "cloudability-sdk-go"
apikey := "testapikey"
testClient := NewClient(apikey)
if testClient.Client.Timeout != duration {
t.Errorf("HTTP client timeout mismatch. Got %s. Expected %s", testClient.Client.Timeout, duration)
}
if testClient.UserAgent != userAgent {
t.Errorf("Cloudability client useragent mismatch. Got %s. Expected %s", testClient.UserAgent, userAgent)
}
if testClient.apikey != apikey {
t.Errorf("Cloudability client apikey mismatch. Got %s. Expected %s", testClient.apikey, apikey)
}
}
func TestSetTimeout(t *testing.T) {
duration := 60 * time.Second
userAgent := "cloudability-sdk-go"
apikey := "testapikey"
testClient := NewClient(apikey)
testClient.SetTimeout(duration)
if testClient.Client.Timeout != duration {
t.Errorf("HTTP client timeout mismatch. Got %s. Expected %s", testClient.Client.Timeout, duration)
}
if testClient.UserAgent != userAgent {
t.Errorf("Cloudability client useragent mismatch. Got %s. Expected %s", testClient.UserAgent, userAgent)
}
if testClient.apikey != apikey {
t.Errorf("Cloudability client apikey mismatch. Got %s. Expected %s", testClient.apikey, apikey)
}
}
func TestNewEndpoint(t *testing.T) {
baseURL, _ := url.Parse("api.test.com")
endpointPath := "test-endpoint"
apikey := "testapikey"
testClient := NewClient(apikey)
e := newEndpoint(testClient, baseURL, endpointPath)
if e.BaseURL.String() != baseURL.String() {
t.Errorf("Endpoint BaseURL mismatch. Got %s. Expected %s", e.BaseURL.String(), baseURL)
}
if e.EndpointPath != endpointPath {
t.Errorf("Endpoint BaseURL mismatch. Got %s. Expected %s", e.EndpointPath, endpointPath)
}
}
func TestNewV1Endpoint(t *testing.T) {
endpointPath := "test-endpoint"
apikey := "testapikey"
testClient := NewClient(apikey)
e := newV1Endpoint(testClient, endpointPath)
if e.BaseURL.String() != apiV1URL {
t.Errorf("V3Endpoint BaseURL mismatch. Got %s. Expected %s", e.BaseURL.String(), apiV1URL)
}
if e.EndpointPath != endpointPath {
t.Errorf("V3Endpoint BaseURL mismatch. Got %s. Expected %s", e.EndpointPath, endpointPath)
}
}
func TestNewV3Endpoint(t *testing.T) {
endpointPath := "test-endpoint"
apikey := "testapikey"
testClient := NewClient(apikey)
e := newV3Endpoint(testClient, endpointPath)
if e.BaseURL.String() != apiV3URL {
t.Errorf("V3Endpoint BaseURL mismatch. Got %s. Expected %s", e.BaseURL.String(), apiV3URL)
}
if e.EndpointPath != endpointPath {
t.Errorf("V3Endpoint BaseURL mismatch. Got %s. Expected %s", e.EndpointPath, endpointPath)
}
}
func TestV3NewRequest(t *testing.T) {
endpointPath := "test-endpoint"
u := &url.URL{Path: "api.test.com/test-endpoint"}
apikey := "testapikey"
testClient := NewClient(apikey)
v3e := newV3Endpoint(testClient, endpointPath)
v3req, _ := v3e.newRequest("GET", u, nil)
if u, p, ok := v3req.BasicAuth(); ok {
if u != apikey {
t.Errorf("v3Endpoint.NewRequest BasicAuth username mismatch. Got %s. Expected %s", u, apikey)
}
if p != "" {
t.Errorf("v3Endpoint.NewRequest BasicAuth password mismatch. Got %s. Expected %s", p, "")
}
}
}
func TestV1NewRequest(t *testing.T) {
endpointPath := "test-endpoint"
u := &url.URL{Path: "api.test.com/test-endpoint"}
apikey := "testapikey"
testClient := NewClient(apikey)
v1e := newV1Endpoint(testClient, endpointPath)
v1req, _ := v1e.newRequest("GET", u, nil)
q := v1req.URL.Query()
if q.Get("auth_token") != apikey {
t.Errorf("v1Endpoint.NewRequest Query parameter 'auth_token' mismatch. Got %s. Expected %s", q.Get("auth_token"), apikey)
}
}