forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
113 lines (85 loc) · 2.52 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
111
112
113
package linodego
import (
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestClient_SetAPIVersion(t *testing.T) {
defaultURL := "https://api.linode.com/v4"
baseURL := "api.very.cool.com"
apiVersion := "v4beta"
expectedHost := fmt.Sprintf("https://%s/%s", baseURL, apiVersion)
updatedBaseURL := "api.more.cool.com"
updatedAPIVersion := "v4beta_changed"
updatedExpectedHost := fmt.Sprintf("https://%s/%s", updatedBaseURL, updatedAPIVersion)
protocolBaseURL := "http://api.more.cool.com"
protocolAPIVersion := "v4_http"
protocolExpectedHost := fmt.Sprintf("%s/%s", protocolBaseURL, protocolAPIVersion)
client := NewClient(nil)
if client.resty.BaseURL != defaultURL {
t.Fatal(cmp.Diff(client.resty.BaseURL, defaultURL))
}
client.SetBaseURL(baseURL)
client.SetAPIVersion(apiVersion)
if client.resty.BaseURL != expectedHost {
t.Fatal(cmp.Diff(client.resty.BaseURL, expectedHost))
}
// Ensure setting twice does not cause conflicts
client.SetBaseURL(updatedBaseURL)
client.SetAPIVersion(updatedAPIVersion)
if client.resty.BaseURL != updatedExpectedHost {
t.Fatal(cmp.Diff(client.resty.BaseURL, updatedExpectedHost))
}
// Revert
client.SetBaseURL(baseURL)
client.SetAPIVersion(apiVersion)
if client.resty.BaseURL != expectedHost {
t.Fatal(cmp.Diff(client.resty.BaseURL, expectedHost))
}
// Custom protocol
client.SetBaseURL(protocolBaseURL)
client.SetAPIVersion(protocolAPIVersion)
if client.resty.BaseURL != protocolExpectedHost {
t.Fatal(cmp.Diff(client.resty.BaseURL, expectedHost))
}
}
func TestClient_NewFromEnv(t *testing.T) {
file := createTestConfig(t, configNewFromEnv)
// This is cool
t.Setenv(APIEnvVar, "")
t.Setenv(APIConfigEnvVar, file.Name())
t.Setenv(APIConfigProfileEnvVar, "cool")
client, err := NewClientFromEnv(nil)
if err != nil {
t.Fatal(err)
}
if client.selectedProfile != "cool" {
t.Fatalf("mismatched profile: %s != %s", client.selectedProfile, "cool")
}
if client.loadedProfile != "" {
t.Fatal("expected empty loaded profile")
}
if err := client.UseProfile("cool"); err != nil {
t.Fatal(err)
}
if client.loadedProfile != "cool" {
t.Fatal("expected cool as loaded profile")
}
}
func TestClient_NewFromEnvToken(t *testing.T) {
t.Setenv(APIEnvVar, "blah")
client, err := NewClientFromEnv(nil)
if err != nil {
t.Fatal(err)
}
if client.resty.Header.Get("Authorization") != "Bearer blah" {
t.Fatal("token not found in auth header: blah")
}
}
const configNewFromEnv = `
[default]
api_url = api.cool.linode.com
api_version = v4beta
[cool]
token = blah
`