-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Frank Jogeleit <[email protected]>
- Loading branch information
Showing
2 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package api_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/kyverno/policy-reporter/pkg/api" | ||
) | ||
|
||
func Test_HTTPBasicSkipped(t *testing.T) { | ||
handler := api.HTTPBasic(nil, api.HealthzHandler(func() bool { return true })) | ||
|
||
req, err := http.NewRequest("GET", "/healthz", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized) | ||
} | ||
} | ||
|
||
func Test_HTTPBasicUnauthorized(t *testing.T) { | ||
handler := api.HTTPBasic(&api.BasicAuth{Username: "user", Password: "password"}, api.HealthzHandler(func() bool { return true })) | ||
|
||
req, err := http.NewRequest("GET", "/healthz", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusUnauthorized { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized) | ||
} | ||
|
||
if rr.Header().Get("WWW-Authenticate") == "" { | ||
t.Errorf("Missing Header: WWW-Authenticate") | ||
} | ||
} | ||
|
||
func Test_HTTPBasicAuthorized(t *testing.T) { | ||
handler := api.HTTPBasic(&api.BasicAuth{Username: "user", Password: "password"}, api.HealthzHandler(func() bool { return true })) | ||
|
||
req, err := http.NewRequest("GET", "/healthz", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
req.SetBasicAuth("user", "password") | ||
|
||
rr := httptest.NewRecorder() | ||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters