0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

added header match and a new failing test

This commit is contained in:
jordi collell 2015-05-09 08:11:02 +02:00
parent 253c069b26
commit 4c11854927
2 changed files with 59 additions and 6 deletions

View file

@ -31,7 +31,6 @@ func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
// Check credentials // Check credentials
if !ok || username != rule.Username || password != rule.Password { if !ok || username != rule.Username || password != rule.Password {
w.Header().Set("WWW-Authenticate", "Basic") w.Header().Set("WWW-Authenticate", "Basic")
w.WriteHeader(http.StatusUnauthorized)
return http.StatusUnauthorized, nil return http.StatusUnauthorized, nil
} }

View file

@ -26,7 +26,6 @@ func TestBasicAuth(t *testing.T) {
}{ }{
{"/testing", http.StatusUnauthorized, "ttest:test"}, {"/testing", http.StatusUnauthorized, "ttest:test"},
{"/testing", http.StatusOK, "test:ttest"}, {"/testing", http.StatusOK, "test:ttest"},
{"/testing", http.StatusUnauthorized, ""}, {"/testing", http.StatusUnauthorized, ""},
} }
@ -51,16 +50,71 @@ func TestBasicAuth(t *testing.T) {
t.Errorf("Test %d: Expected Header '%d' but was '%d'", t.Errorf("Test %d: Expected Header '%d' but was '%d'",
i, test.result, result) i, test.result, result)
} }
if result == http.StatusUnauthorized {
if rec.Code != test.result { headers := rec.Header()
t.Errorf("Test %d: Expected Header '%d' but was '%d'", if val, ok := headers["Www-Authenticate"]; ok {
i, test.result, rec.Code) if val[0] != "Basic" {
t.Errorf("Test %d, Www-Authenticate should be %s provided %s", i, "Basic", val[0])
} }
} else {
t.Errorf("Test %d, should provide a header Www-Authenticate", i)
}
}
} }
} }
func TestMultipleOverlappingRules(t *testing.T) {
rw := BasicAuth{
Next: middleware.HandlerFunc(contentHandler),
Rules: []Rule{
{Username: "t", Password: "p1", Resources: []string{"/t"}},
{Username: "t1", Password: "p2", Resources: []string{"/t/t"}},
},
}
tests := []struct {
from string
result int
cred string
}{
{"/t", http.StatusOK, "t:p1"},
{"/t/t", http.StatusOK, "t:p1"},
{"/t/t", http.StatusOK, "t1:p2"},
}
for i, test := range tests {
req, err := http.NewRequest("GET", test.from, nil)
if err != nil {
t.Fatalf("Test %d: Could not create HTTP request %v", i, err)
}
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(test.cred))
req.Header.Set("Authorization", auth)
rec := httptest.NewRecorder()
result, err := rw.ServeHTTP(rec, req)
if err != nil {
t.Fatalf("Test %d: Could not ServeHTTP %v", i, err)
}
if result != test.result {
t.Errorf("Test %d: Expected Header '%d' but was '%d'",
i, test.result, result)
}
}
}
func contentHandler(w http.ResponseWriter, r *http.Request) (int, error) { func contentHandler(w http.ResponseWriter, r *http.Request) (int, error) {
fmt.Fprintf(w, r.URL.String()) fmt.Fprintf(w, r.URL.String())
return http.StatusOK, nil return http.StatusOK, nil