From b9a4caade0578c530a8f5c51391bf2e8e0d16012 Mon Sep 17 00:00:00 2001 From: Hector Date: Wed, 21 Jun 2023 11:39:49 +0100 Subject: [PATCH] rename auth middleware --- server/{middleware.go => auth.go} | 2 +- server/{middleware_test.go => auth_test.go} | 8 ++++---- server/server.go | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) rename server/{middleware.go => auth.go} (72%) rename server/{middleware_test.go => auth_test.go} (74%) diff --git a/server/middleware.go b/server/auth.go similarity index 72% rename from server/middleware.go rename to server/auth.go index 7593bbc..6ed42ea 100644 --- a/server/middleware.go +++ b/server/auth.go @@ -6,7 +6,7 @@ import ( "gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/auth" ) -func BasicAuthMiddleware(handlerFunc http.HandlerFunc, authProvider auth.AuthProvider) http.HandlerFunc { +func AuthMiddleware(handlerFunc http.HandlerFunc, authProvider auth.AuthProvider) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if authProvider.IsAllowed(r) { handlerFunc.ServeHTTP(w, r) diff --git a/server/middleware_test.go b/server/auth_test.go similarity index 74% rename from server/middleware_test.go rename to server/auth_test.go index 3ac564b..5a460c9 100644 --- a/server/middleware_test.go +++ b/server/auth_test.go @@ -18,13 +18,13 @@ func newTestRequest() *http.Request { return httptest.NewRequest(http.MethodGet, "http://example.com", nil) } -func executeBasicAuthMiddlewareTest(t *testing.T, authMatches bool, expectedCode int, expectedCallCount int) { +func executeAuthMiddlewareTest(t *testing.T, authMatches bool, expectedCode int, expectedCallCount int) { callCount := 0 testHandler := func(w http.ResponseWriter, r *http.Request) { callCount++ } - handler := BasicAuthMiddleware(testHandler, testAuthProvider{match: authMatches}) + handler := AuthMiddleware(testHandler, testAuthProvider{match: authMatches}) recorder := httptest.NewRecorder() request := newTestRequest() handler.ServeHTTP(recorder, request) @@ -38,9 +38,9 @@ func executeBasicAuthMiddlewareTest(t *testing.T, authMatches bool, expectedCode } func Test_GIVEN_MatchingBasicAuth_WHEN_MethodCalled_THEN_RequestProcessed(t *testing.T) { - executeBasicAuthMiddlewareTest(t, true, http.StatusOK, 1) + executeAuthMiddlewareTest(t, true, http.StatusOK, 1) } func Test_GIVEN_NonMatchingBasicAuth_WHEN_MethodCalled_THEN_RequestRejected(t *testing.T) { - executeBasicAuthMiddlewareTest(t, false, http.StatusUnauthorized, 0) + executeAuthMiddlewareTest(t, false, http.StatusUnauthorized, 0) } diff --git a/server/server.go b/server/server.go index 70599a4..18f2b9f 100644 --- a/server/server.go +++ b/server/server.go @@ -12,11 +12,11 @@ func StartServer( appSettings *cfg.AppSettings, textFileCollector *textfile.Collector, ) chan error { - http.HandleFunc("/", BasicAuthMiddleware( + http.HandleFunc("/", AuthMiddleware( rootHtmlHandler, appSettings.AuthProvider, )) - http.HandleFunc(metricsPath, BasicAuthMiddleware( + http.HandleFunc(metricsPath, AuthMiddleware( func(w http.ResponseWriter, r *http.Request) { metricHandler(w, r, textFileCollector) },