-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathproxy_auth_basic_test.go
More file actions
298 lines (268 loc) · 8.45 KB
/
proxy_auth_basic_test.go
File metadata and controls
298 lines (268 loc) · 8.45 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package pivnet_test
import (
"encoding/base64"
"net/http"
"net/http/httptest"
"strings"
"testing"
pivnet "github.com/pivotal-cf/go-pivnet/v9"
)
func TestNewBasicProxyAuth(t *testing.T) {
tests := []struct {
name string
username string
password string
}{
{
name: "with valid credentials",
username: "testuser",
password: "testpass",
},
{
name: "with empty username",
username: "",
password: "testpass",
},
{
name: "with empty password",
username: "testuser",
password: "",
},
{
name: "with both empty",
username: "",
password: "",
},
{
name: "with special characters",
username: "user@domain.com",
password: "p@ssw0rd!#$%",
},
{
name: "with spaces",
username: "user name",
password: "pass word",
},
{
name: "with colon in password",
username: "user",
password: "pass:word",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
auth := pivnet.NewBasicProxyAuth(tt.username, tt.password)
if auth == nil {
t.Error("expected non-nil BasicProxyAuth")
}
})
}
}
func TestBasicProxyAuth_Authenticate(t *testing.T) {
tests := []struct {
name string
username string
password string
expectHeader bool
expectedHeaderValue string
}{
{
name: "valid credentials",
username: "testuser",
password: "testpass",
expectHeader: true,
expectedHeaderValue: "Basic " + base64.StdEncoding.EncodeToString([]byte("testuser:testpass")),
},
{
name: "empty username and password - no header",
username: "",
password: "",
expectHeader: false,
expectedHeaderValue: "",
},
{
name: "empty username with password",
username: "",
password: "testpass",
expectHeader: true,
expectedHeaderValue: "Basic " + base64.StdEncoding.EncodeToString([]byte(":testpass")),
},
{
name: "username with empty password",
username: "testuser",
password: "",
expectHeader: true,
expectedHeaderValue: "Basic " + base64.StdEncoding.EncodeToString([]byte("testuser:")),
},
{
name: "credentials with special characters",
username: "user@domain.com",
password: "p@ssw0rd!#$%^&*()",
expectHeader: true,
expectedHeaderValue: "Basic " + base64.StdEncoding.EncodeToString([]byte("user@domain.com:p@ssw0rd!#$%^&*()")),
},
{
name: "credentials with colon",
username: "user:name",
password: "pass:word",
expectHeader: true,
expectedHeaderValue: "Basic " + base64.StdEncoding.EncodeToString([]byte("user:name:pass:word")),
},
{
name: "credentials with spaces",
username: "user name",
password: "pass word",
expectHeader: true,
expectedHeaderValue: "Basic " + base64.StdEncoding.EncodeToString([]byte("user name:pass word")),
},
{
name: "credentials with newlines",
username: "user\nname",
password: "pass\nword",
expectHeader: true,
expectedHeaderValue: "Basic " + base64.StdEncoding.EncodeToString([]byte("user\nname:pass\nword")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
auth := pivnet.NewBasicProxyAuth(tt.username, tt.password)
req := httptest.NewRequest("GET", "http://example.com", nil)
err := auth.Authenticate(req)
if err != nil {
t.Errorf("expected no error, but got: %v", err)
return
}
headerValue := req.Header.Get("Proxy-Authorization")
if tt.expectHeader {
if headerValue == "" {
t.Error("expected Proxy-Authorization header to be set, but it was empty")
return
}
if headerValue != tt.expectedHeaderValue {
t.Errorf("expected header value '%s', got '%s'", tt.expectedHeaderValue, headerValue)
}
// Verify the header can be decoded
if strings.HasPrefix(headerValue, "Basic ") {
encodedCreds := strings.TrimPrefix(headerValue, "Basic ")
decodedBytes, err := base64.StdEncoding.DecodeString(encodedCreds)
if err != nil {
t.Errorf("failed to decode base64: %v", err)
}
expectedCreds := tt.username + ":" + tt.password
if string(decodedBytes) != expectedCreds {
t.Errorf("decoded credentials '%s' don't match expected '%s'", string(decodedBytes), expectedCreds)
}
}
} else {
if headerValue != "" {
t.Errorf("expected no Proxy-Authorization header, but got: %s", headerValue)
}
}
})
}
}
func TestBasicProxyAuth_WithMockProxyServer(t *testing.T) {
t.Run("successful authentication with mock proxy", func(t *testing.T) {
authAttempts := 0
proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authAttempts++
// Check for Proxy-Authorization header
authHeader := r.Header.Get("Proxy-Authorization")
if authHeader == "" {
w.Header().Set("Proxy-Authenticate", "Basic realm=\"Mock Proxy\"")
w.WriteHeader(http.StatusProxyAuthRequired)
w.Write([]byte("Proxy authentication required"))
return
}
// Decode and verify credentials
expectedAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte("mockuser:mockpass"))
if authHeader != expectedAuth {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("Invalid proxy credentials"))
return
}
// Success
w.WriteHeader(http.StatusOK)
w.Write([]byte("Authenticated through proxy"))
}))
defer proxyServer.Close()
// Create auth and transport
auth := pivnet.NewBasicProxyAuth("mockuser", "mockpass")
transport, err := pivnet.NewProxyAuthTransport(http.DefaultTransport, auth)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
}
// Make request
client := &http.Client{Transport: transport}
resp, err := client.Get(proxyServer.URL)
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status 200, got %d", resp.StatusCode)
}
if authAttempts != 1 {
t.Errorf("expected 1 auth attempt, got %d", authAttempts)
}
})
t.Run("proxy rejects invalid credentials", func(t *testing.T) {
proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Proxy-Authorization")
// Only accept specific credentials
validAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte("validuser:validpass"))
if authHeader != validAuth {
w.Header().Set("Proxy-Authenticate", "Basic realm=\"Mock Proxy\"")
w.WriteHeader(http.StatusProxyAuthRequired)
w.Write([]byte("Invalid credentials"))
return
}
w.WriteHeader(http.StatusOK)
}))
defer proxyServer.Close()
// Use invalid credentials
auth := pivnet.NewBasicProxyAuth("invaliduser", "invalidpass")
transport, err := pivnet.NewProxyAuthTransport(http.DefaultTransport, auth)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
}
client := &http.Client{Transport: transport}
resp, err := client.Get(proxyServer.URL)
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusProxyAuthRequired {
t.Errorf("expected status 407, got %d", resp.StatusCode)
}
})
t.Run("proxy with special characters in credentials", func(t *testing.T) {
specialUser := "user@domain.com"
specialPass := "p@ss:w0rd!#$%"
proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Proxy-Authorization")
expectedAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(specialUser+":"+specialPass))
if authHeader != expectedAuth {
w.WriteHeader(http.StatusProxyAuthRequired)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Special chars handled"))
}))
defer proxyServer.Close()
auth := pivnet.NewBasicProxyAuth(specialUser, specialPass)
transport, err := pivnet.NewProxyAuthTransport(http.DefaultTransport, auth)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
}
client := &http.Client{Transport: transport}
resp, err := client.Get(proxyServer.URL)
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status 200, got %d", resp.StatusCode)
}
})
}