-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpauth_test.go
190 lines (152 loc) · 4.04 KB
/
httpauth_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
189
190
// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package httpauth_test
import (
"net/http"
"net/http/httptest"
"testing"
. "github.com/dhowden/httpauth"
)
func TestCreds(t *testing.T) {
creds := map[string]string{
"alice": "shhhh",
"bob": "",
}
tests := []struct {
username, password string
valid bool
}{
// Empty, invalid
{
"",
"",
false,
},
// Unknown user
{
"cecil",
"",
false,
},
// Alice, wrong password
{
"alice",
"bob",
false,
},
// Alice, correct password
{
"alice",
"shhhh",
true,
},
// Bob, correct (empty) password
{
"bob",
"",
true,
},
}
c := Creds(creds)
for ii, tt := range tests {
got := c.Check(tt.username, tt.password)
if got != tt.valid {
t.Errorf("[%d] c.Check(%#v, %#v) = %#v, expected %#v", ii, tt.username, tt.password, got, tt.valid)
}
}
var nilCreds map[string]string
c = Creds(nilCreds)
if c.Check("alice", "") {
t.Errorf("nil Creds should return false")
}
}
func TestNone(t *testing.T) {
if !Skip.Check("", "") {
t.Errorf("n.Check(\"\", \"\") = false, expected: true")
}
}
func handlerFuncOK(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(http.StatusText(http.StatusOK)))
return
}
func testHandlerUnauthorised(t *testing.T, url string, h http.Handler) {
r, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatalf("unexpected error creating request: %v", err)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != http.StatusUnauthorized {
t.Errorf("w.Code = %d, expected: %d", w.Code, http.StatusUnauthorized)
}
if w.Header().Get("WWW-Authenticate") != "Basic" {
t.Errorf("w.Header().Get(\"WWW-Authenticate\") = %s, expected: %s", w.Header().Get("WWW-Authenticate"), "Basic")
}
body := w.Body
if body.String() != http.StatusText(http.StatusUnauthorized) {
t.Errorf("w.Body = %q, expected: %q", body.String(), http.StatusText(http.StatusUnauthorized))
}
}
func testHandlerOK(t *testing.T, url string, h http.Handler) {
r, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatalf("unexpected error creating request: %v", err)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("w.Code = %d, expected: %d", w.Code, http.StatusOK)
}
body := w.Body
if body.String() != http.StatusText(http.StatusOK) {
t.Errorf("w.Body = %q, expected: %q", body.String(), http.StatusText(http.StatusOK))
}
}
type fixedChecker bool
func (c fixedChecker) Check(username, password string) bool { return bool(c) }
func TestHandler(t *testing.T) {
c := fixedChecker(false)
h := NewHandler(c, http.HandlerFunc(handlerFuncOK))
testHandlerUnauthorised(t, "/", h)
c = fixedChecker(true)
h = NewHandler(c, http.HandlerFunc(handlerFuncOK))
testHandlerOK(t, "/", h)
}
func TestHandlerFunc(t *testing.T) {
c := fixedChecker(false)
h := HandlerFunc(c, handlerFuncOK)
testHandlerUnauthorised(t, "/", h)
c = fixedChecker(true)
h = HandlerFunc(c, handlerFuncOK)
testHandlerOK(t, "/", h)
}
func TestDefaultHandle(t *testing.T) {
c := fixedChecker(false)
HandleFunc(c, "/hfu", handlerFuncOK)
testHandlerUnauthorised(t, "/hfu", http.DefaultServeMux)
Handle(c, "/hu", http.HandlerFunc(handlerFuncOK))
testHandlerUnauthorised(t, "/hu", http.DefaultServeMux)
c = fixedChecker(true)
HandleFunc(c, "/hfok", handlerFuncOK)
testHandlerOK(t, "/hfok", http.DefaultServeMux)
Handle(c, "/hok", http.HandlerFunc(handlerFuncOK))
testHandlerOK(t, "/hok", http.DefaultServeMux)
}
func TestServeMux(t *testing.T) {
c := fixedChecker(false)
m := http.NewServeMux()
w := NewServeMux(c, m)
w.HandleFunc("/f", handlerFuncOK)
testHandlerUnauthorised(t, "/f", m)
w.Handle("/h", http.HandlerFunc(handlerFuncOK))
testHandlerUnauthorised(t, "/h", m)
c = fixedChecker(true)
m = http.NewServeMux()
w = NewServeMux(c, m)
w.HandleFunc("/f", handlerFuncOK)
testHandlerOK(t, "/f", m)
w.Handle("/h", http.HandlerFunc(handlerFuncOK))
testHandlerOK(t, "/h", m)
}