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
args args
want int
reqAuth bool
}{
{
name: "no user or password setup",
args: args{h: dummyHandler},
want: http.StatusOK,
reqAuth: false,
},
{
name: "only user set",
@ -33,6 +35,7 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
b: BasicAuth{Username: "test"}, // no basic auth
},
want: http.StatusOK,
reqAuth: false,
},
{
name: "only pass set",
@ -41,6 +44,7 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
b: BasicAuth{Password: "test"}, // no basic auth
},
want: http.StatusOK,
reqAuth: false,
},
{
name: "credentials correct",
@ -49,6 +53,7 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
b: BasicAuth{Username: "test", Password: "test"}, // basic auth enabled
},
want: http.StatusOK,
reqAuth: true,
},
{
name: "credentials wrong",
@ -57,6 +62,16 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
b: BasicAuth{Username: "test1", Password: "test"}, // basic auth enabled
},
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 {
@ -64,7 +79,9 @@ func TestWrapHandlerInBasicAuth(t *testing.T) {
baHandler := WrapHandlerInBasicAuth(tt.args.h, tt.args.b)
req := httptest.NewRequest("GET", "http://localhost/metrics", nil)
if tt.reqAuth {
req.SetBasicAuth("test", "test")
}
w := httptest.NewRecorder()
baHandler(w, req)