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

@ -20,11 +20,13 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
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",
@ -33,6 +35,7 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
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",
@ -41,6 +44,7 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
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",
@ -49,6 +53,7 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
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",
@ -57,6 +62,16 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
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)
if tt.reqAuth {
req.SetBasicAuth("test", "test") req.SetBasicAuth("test", "test")
}
w := httptest.NewRecorder() w := httptest.NewRecorder()
baHandler(w, req) baHandler(w, req)