forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
161 lines (145 loc) · 4.39 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package cfclient
import (
"net/http"
"testing"
"time"
"github.com/onsi/gomega"
. "github.com/smartystreets/goconvey/convey"
)
func TestDefaultConfig(t *testing.T) {
Convey("Default config", t, func() {
c := DefaultConfig()
So(c.ApiAddress, ShouldEqual, "http://api.bosh-lite.com")
So(c.Username, ShouldEqual, "admin")
So(c.Password, ShouldEqual, "admin")
So(c.SkipSslValidation, ShouldEqual, false)
So(c.Token, ShouldEqual, "")
So(c.UserAgent, ShouldEqual, "Go-CF-client/1.1")
})
}
func TestRemovalofTrailingSlashOnAPIAddress(t *testing.T) {
Convey("Test removal of trailing slash of the API Address", t, func() {
setup(MockRoute{"GET", "/v2/organizations", listOrgsPayload, "", 200, "", nil}, t)
defer teardown()
c := &Config{
ApiAddress: server.URL + "/",
}
client, err := NewClient(c)
So(err, ShouldBeNil)
So(client.Config.ApiAddress, ShouldNotEndWith, "/")
})
}
func TestMakeRequest(t *testing.T) {
Convey("Test making request b", t, func() {
setup(MockRoute{"GET", "/v2/organizations", listOrgsPayload, "", 200, "", nil}, t)
defer teardown()
c := &Config{
ApiAddress: server.URL,
Username: "foo",
Password: "bar",
SkipSslValidation: true,
}
client, err := NewClient(c)
So(err, ShouldBeNil)
req := client.NewRequest("GET", "/v2/organizations")
resp, err := client.DoRequest(req)
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
})
}
func TestMakeRequestFailure(t *testing.T) {
Convey("Test making request b", t, func() {
setup(MockRoute{"GET", "/v2/organizations", listOrgsPayload, "", 200, "", nil}, t)
defer teardown()
c := &Config{
ApiAddress: server.URL,
Username: "foo",
Password: "bar",
SkipSslValidation: true,
}
client, err := NewClient(c)
So(err, ShouldBeNil)
req := client.NewRequest("GET", "/v2/organizations")
req.url = "%gh&%ij"
resp, err := client.DoRequest(req)
So(resp, ShouldBeNil)
So(err, ShouldNotBeNil)
})
}
func TestMakeRequestWithTimeout(t *testing.T) {
Convey("Test making request b", t, func() {
setup(MockRoute{"GET", "/v2/organizations", listOrgsPayload, "", 200, "", nil}, t)
defer teardown()
c := &Config{
ApiAddress: server.URL,
Username: "foo",
Password: "bar",
SkipSslValidation: true,
HttpClient: &http.Client{Timeout: 10 * time.Nanosecond},
}
client, err := NewClient(c)
So(err, ShouldNotBeNil)
So(client, ShouldBeNil)
})
}
func TestHTTPErrorHandling(t *testing.T) {
Convey("Test making request b", t, func() {
setup(MockRoute{"GET", "/v2/organizations", "502 Bad Gateway", "", 502, "", nil}, t)
defer teardown()
c := &Config{
ApiAddress: server.URL,
Username: "foo",
Password: "bar",
SkipSslValidation: true,
}
client, err := NewClient(c)
So(err, ShouldBeNil)
req := client.NewRequest("GET", "/v2/organizations")
resp, err := client.DoRequest(req)
So(err, ShouldNotBeNil)
So(resp, ShouldNotBeNil)
httpErr := err.(CloudFoundryHTTPError)
So(httpErr.StatusCode, ShouldEqual, 502)
So(httpErr.Status, ShouldEqual, "502 Bad Gateway")
So(string(httpErr.Body), ShouldEqual, "502 Bad Gateway")
})
}
func TestTokenRefresh(t *testing.T) {
gomega.RegisterTestingT(t)
Convey("Test making request", t, func() {
setup(MockRoute{"GET", "/v2/organizations", listOrgsPayload, "", 200, "", nil}, t)
fakeUAAServer = FakeUAAServer(1)
c := &Config{
ApiAddress: server.URL,
Username: "foo",
Password: "bar",
}
client, err := NewClient(c)
So(err, ShouldBeNil)
token, err := client.GetToken()
So(err, ShouldBeNil)
gomega.Consistently(token).Should(gomega.Equal("bearer foobar2"))
gomega.Eventually(func() string { token, _ := client.GetToken(); return token }, "2s").Should(gomega.Equal("bearer foobar3"))
})
}
func TestEndpointRefresh(t *testing.T) {
gomega.RegisterTestingT(t)
Convey("Test expiring endpoint", t, func() {
setup(MockRoute{"GET", "/v2/organizations", listOrgsPayload, "", 200, "", nil}, t)
fakeUAAServer = FakeUAAServer(0)
c := &Config{
ApiAddress: server.URL,
Username: "foo",
Password: "bar",
}
client, err := NewClient(c)
So(err, ShouldBeNil)
lastTokenSource := client.Config.TokenSource
for i := 1; i < 5; i++ {
_, err := client.GetToken()
So(err, ShouldBeNil)
So(client.Config.TokenSource, ShouldNotEqual, lastTokenSource)
lastTokenSource = client.Config.TokenSource
}
})
}