-
Notifications
You must be signed in to change notification settings - Fork 96
/
rest_client_test.go
188 lines (153 loc) · 4.39 KB
/
rest_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package cas
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestRequestGrantingTicket(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/cas/v1/tickets" || r.Method != "POST" {
w.WriteHeader(404)
return
}
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
w.WriteHeader(415)
return
}
if r.FormValue("username") != "tricia" && r.FormValue("password") != "hitchhiker" {
w.WriteHeader(400)
return
}
w.Header().Set("Location", "/cas/v1/tickets/TGT-abc")
w.WriteHeader(201)
}))
defer server.Close()
casURL, err := url.Parse(server.URL + "/cas/")
if err != nil {
t.Error("failed to create cas url from test server")
}
restClient := NewRestClient(&RestOptions{
CasURL: casURL,
Client: server.Client(),
})
tgt, err := restClient.RequestGrantingTicket("tricia", "hitchhiker")
if err != nil {
t.Errorf("requesting granting ticket failed: %v", err)
}
if tgt != "TGT-abc" {
t.Errorf("expected %s but received %v", "TGT-abc", tgt)
}
_, err = restClient.RequestGrantingTicket("arthur", "dent")
if err == nil {
t.Errorf("authentication should fail for arthur")
}
}
func TestRequestServiceTicket(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/cas/v1/tickets/TGT-abc" || r.Method != "POST" {
w.WriteHeader(404)
return
}
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
w.WriteHeader(415)
return
}
if r.FormValue("service") != "https://hitchhiker.com/heartOfGold" {
w.WriteHeader(400)
return
}
w.WriteHeader(200)
w.Write([]byte("ST-123"))
}))
defer server.Close()
casURL, err := url.Parse(server.URL + "/cas/")
if err != nil {
t.Error("failed to create cas url from test server")
}
serviceURL, err := url.Parse("https://hitchhiker.com/heartOfGold")
if err != nil {
t.Error("failed to create service url")
}
restClient := NewRestClient(&RestOptions{
CasURL: casURL,
ServiceURL: serviceURL,
Client: server.Client(),
})
st, err := restClient.RequestServiceTicket(TicketGrantingTicket("TGT-abc"))
if err != nil {
t.Errorf("requesting service ticket failed: %v", err)
}
if st != "ST-123" {
t.Errorf("expected %s but received %v", "ST-123", st)
}
_, err = restClient.RequestServiceTicket(TicketGrantingTicket("TGT-xyz"))
if err == nil {
t.Errorf("service ticket request should fail for TGT-xyz")
}
serviceURL, err = url.Parse("https://hitchhiker.com/restaurantAtTheEndOfTheUniverse")
if err != nil {
t.Error("failed to create service url")
}
restClient = NewRestClient(&RestOptions{
CasURL: casURL,
ServiceURL: serviceURL,
Client: server.Client(),
})
_, err = restClient.RequestServiceTicket(TicketGrantingTicket("TGT-abc"))
if err == nil {
t.Errorf("service ticket request should fail for this service")
}
}
func TestValidateService(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/cas/v1/tickets/TGT-abc" || r.Method != "DELETE" {
w.WriteHeader(404)
return
}
w.WriteHeader(200)
}))
defer server.Close()
casURL, err := url.Parse(server.URL + "/cas/")
if err != nil {
t.Error("failed to create cas url from test server")
}
restClient := NewRestClient(&RestOptions{
CasURL: casURL,
Client: server.Client(),
})
err = restClient.Logout(TicketGrantingTicket("TGT-abc"))
if err != nil {
t.Errorf("logout failed %v", err)
}
err = restClient.Logout(TicketGrantingTicket("TGT-xyz"))
if err == nil {
t.Errorf("logout should failed for this TGT")
}
}
func TestLogout(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/cas/v1/tickets/TGT-abc" || r.Method != "DELETE" {
w.WriteHeader(404)
return
}
w.WriteHeader(200)
}))
defer server.Close()
casURL, err := url.Parse(server.URL + "/cas/")
if err != nil {
t.Error("failed to create cas url from test server")
}
restClient := NewRestClient(&RestOptions{
CasURL: casURL,
Client: server.Client(),
})
err = restClient.Logout(TicketGrantingTicket("TGT-abc"))
if err != nil {
t.Errorf("logout failed %v", err)
}
err = restClient.Logout(TicketGrantingTicket("TGT-xyz"))
if err == nil {
t.Errorf("logout should failed for this TGT")
}
}