-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth_test.go
40 lines (33 loc) · 1.15 KB
/
auth_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
package main
import (
"github.com/sdvcrx/cuttlefish/utils"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func assertHTTPCode(t *testing.T, handler http.HandlerFunc, username, password string, expectedCode int) {
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil)
assert.Nil(t, err)
if username != "" && password != "" {
req.Header.Set("Proxy-Authorization", "Basic "+utils.Base64Encode(username+":"+password))
}
handler(w, req)
assert.Equal(t, expectedCode, w.Code)
}
func handlerFunc(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}
func TestProxyAuthenicateHandlerWithoutAuth(t *testing.T) {
px := ProxyAuthenticateHandler(handlerFunc, "", "")
assert.HTTPBodyContains(t, px.ServeHTTP, "GET", "/", nil, "ok")
}
func TestProxyAuthenicateHandler(t *testing.T) {
px := ProxyAuthenticateHandler(handlerFunc, "test", "test")
assertHTTPCode(t, px.ServeHTTP, "test", "test", http.StatusOK)
}
func TestProxyAuthenicateHandlerDeny(t *testing.T) {
px := ProxyAuthenticateHandler(handlerFunc, "fail", "fail")
assertHTTPCode(t, px.ServeHTTP, "joida", "asdwe", http.StatusProxyAuthRequired)
}