-
Notifications
You must be signed in to change notification settings - Fork 14
/
reauth_test.go
96 lines (81 loc) · 1.99 KB
/
reauth_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
package reauth
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)
func emptyHandler(w http.ResponseWriter, r *http.Request) (int, error) {
return http.StatusOK, nil
}
func TestSetup(t *testing.T) {
test := `reauth {
path /test
simple username=password
}`
c := caddy.NewTestController("http", test)
err := setup(c)
if err != nil {
t.Errorf("Unexpected error `%v`", err)
}
}
func TestInvalidSetup(t *testing.T) {
test := `reauth {
path /test
simple usernamepassword
}`
c := caddy.NewTestController("http", test)
err := setup(c)
if err == nil {
t.Errorf("Expected error, got none")
}
}
func TestMiddlewareProvider(t *testing.T) {
test := `reauth {
path /test
except /test/foo
except /test/bar
simple username=password
}`
c := caddy.NewTestController("http", test)
rules, _ := parseConfiguration(c)
auth := &Reauth{
rules: rules,
next: httpserver.HandlerFunc(emptyHandler),
}
req, _ := http.NewRequest("GET", "/", nil)
rec := httptest.NewRecorder()
result, err := auth.ServeHTTP(rec, req)
if err != nil {
t.Errorf("Unexpected error `%v`", err)
}
if result != http.StatusOK {
t.Errorf("Expected `%v` got `%v`", http.StatusOK, result)
}
req.URL.Path = "/test"
result, err = auth.ServeHTTP(rec, req)
if err != nil {
t.Errorf("Unexpected error `%v`", err)
}
if result != http.StatusUnauthorized {
t.Errorf("Expected `%v` got `%v`", http.StatusUnauthorized, result)
}
req.URL.Path = "/test/foo"
result, err = auth.ServeHTTP(rec, req)
if err != nil {
t.Errorf("Unexpected error `%v`", err)
}
if result != http.StatusOK {
t.Errorf("Expected `%v` got `%v`", http.StatusOK, result)
}
req.URL.Path = "/test"
req.SetBasicAuth("username", "password")
result, err = auth.ServeHTTP(rec, req)
if err != nil {
t.Errorf("Unexpected error `%v`", err)
}
if result != http.StatusOK {
t.Errorf("Expected `%v` got `%v`", http.StatusOK, result)
}
}