SetBasicAuth per test case

This commit is contained in:
Till Faelligen 2019-12-22 10:41:19 +01:00
parent 655695cd1c
commit a9c51f1299

View file

@ -17,14 +17,16 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
}) })
tests := []struct { tests := []struct {
name string name string
args args args args
want int want int
reqAuth bool
}{ }{
{ {
name: "no user or password setup", name: "no user or password setup",
args: args{h: dummyHandler}, args: args{h: dummyHandler},
want: http.StatusOK, want: http.StatusOK,
reqAuth: false,
}, },
{ {
name: "only user set", name: "only user set",
@ -32,7 +34,8 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
h: dummyHandler, h: dummyHandler,
b: BasicAuth{Username: "test"}, // no basic auth b: BasicAuth{Username: "test"}, // no basic auth
}, },
want: http.StatusOK, want: http.StatusOK,
reqAuth: false,
}, },
{ {
name: "only pass set", name: "only pass set",
@ -40,7 +43,8 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
h: dummyHandler, h: dummyHandler,
b: BasicAuth{Password: "test"}, // no basic auth b: BasicAuth{Password: "test"}, // no basic auth
}, },
want: http.StatusOK, want: http.StatusOK,
reqAuth: false,
}, },
{ {
name: "credentials correct", name: "credentials correct",
@ -48,7 +52,8 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
h: dummyHandler, h: dummyHandler,
b: BasicAuth{Username: "test", Password: "test"}, // basic auth enabled b: BasicAuth{Username: "test", Password: "test"}, // basic auth enabled
}, },
want: http.StatusOK, want: http.StatusOK,
reqAuth: true,
}, },
{ {
name: "credentials wrong", name: "credentials wrong",
@ -56,7 +61,17 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
h: dummyHandler, h: dummyHandler,
b: BasicAuth{Username: "test1", Password: "test"}, // basic auth enabled b: BasicAuth{Username: "test1", Password: "test"}, // basic auth enabled
}, },
want: http.StatusForbidden, want: http.StatusForbidden,
reqAuth: true,
},
{
name: "no basic auth in request",
args: args{
h: dummyHandler,
b: BasicAuth{Username: "test", Password: "test"}, // basic auth enabled
},
want: http.StatusForbidden,
reqAuth: false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -64,7 +79,9 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
baHandler := WrapHandlerInBasicAuth(tt.args.h, tt.args.b) baHandler := WrapHandlerInBasicAuth(tt.args.h, tt.args.b)
req := httptest.NewRequest("GET", "http://localhost/metrics", nil) req := httptest.NewRequest("GET", "http://localhost/metrics", nil)
req.SetBasicAuth("test", "test") if tt.reqAuth {
req.SetBasicAuth("test", "test")
}
w := httptest.NewRecorder() w := httptest.NewRecorder()
baHandler(w, req) baHandler(w, req)