improve auth tests and rename auth struct
This commit is contained in:
parent
aa7dad3a03
commit
3c9e260926
6
auth.go
6
auth.go
|
@ -24,7 +24,7 @@ type AuthOptions struct {
|
|||
UnauthMethods []string
|
||||
}
|
||||
|
||||
type uploadBasicAuth struct {
|
||||
type auth struct {
|
||||
successHandler http.Handler
|
||||
failureHandler http.Handler
|
||||
o AuthOptions
|
||||
|
@ -56,7 +56,7 @@ func checkAuth(authFile string, decodedAuth []byte) (result bool, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (a uploadBasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func (a auth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if sliceContains(a.o.UnauthMethods, r.Method) {
|
||||
// allow unauthenticated methods
|
||||
a.successHandler.ServeHTTP(w, r)
|
||||
|
@ -86,7 +86,7 @@ func (a uploadBasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func UploadAuth(o AuthOptions) func(http.Handler) http.Handler {
|
||||
fn := func(h http.Handler) http.Handler {
|
||||
return uploadBasicAuth{
|
||||
return auth{
|
||||
successHandler: h,
|
||||
failureHandler: http.HandlerFunc(badAuthorizationHandler),
|
||||
o: o,
|
||||
|
|
|
@ -52,21 +52,29 @@ func TestIndex(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIndexAuthKeys(t *testing.T) {
|
||||
func TestAuthKeysRedirects(t *testing.T) {
|
||||
Config.authFile = "/dev/null"
|
||||
|
||||
mux := setup()
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
redirects := []string{
|
||||
"/",
|
||||
"/paste/",
|
||||
}
|
||||
|
||||
mux.ServeHTTP(w, req)
|
||||
mux := setup()
|
||||
|
||||
if w.Code != 303 {
|
||||
t.Fatalf("Status code is not 301, but %d", w.Code)
|
||||
for _, v := range redirects {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
req, err := http.NewRequest("GET", v, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mux.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != 303 {
|
||||
t.Fatalf("Status code is not 303, but %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
Config.authFile = ""
|
||||
|
|
Loading…
Reference in New Issue