Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/gofr/http/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

gofrHttp "gofr.dev/pkg/gofr/http"
"gofr.dev/pkg/gofr/service"
)

// AuthMethod represents a custom type to define the different authentication methods supported.
Expand Down Expand Up @@ -39,7 +40,7 @@ type AuthProvider interface {
func AuthMiddleware(a AuthProvider) func(handler http.Handler) http.Handler {
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isWellKnown(r.URL.Path) {
if r.URL.Path == service.AlivePath {
handler.ServeHTTP(w, r)
return
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/gofr/http/middleware/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"strings"
"testing"

"gofr.dev/pkg/gofr/service"

Check failure on line 10 in pkg/gofr/http/middleware/auth_test.go

View workflow job for this annotation

GitHub Actions / Code Quality🎖️

File is not properly formatted (gci)
"github.com/stretchr/testify/assert"
)

Expand All @@ -20,8 +21,9 @@
expectedHeader any
expectedBody string
}{
{url: "/.well-known/health", success: true, statusCode: http.StatusOK, expectedBody: `OK`},
{url: "/.well-known/health", statusCode: http.StatusOK, expectedBody: `OK`},
{url: service.AlivePath, statusCode: http.StatusOK, expectedBody: `OK`},
{url: service.HealthPath, success: true, statusCode: http.StatusOK, expectedHeader: "user-header-string", expectedBody: `OK`},
{url: service.HealthPath, success: false, statusCode: http.StatusUnauthorized, expectedBody: errBody},
{url: "/", success: true, statusCode: http.StatusOK, expectedHeader: "user-header-string", expectedBody: `OK`},
{url: "/", success: false, statusCode: http.StatusUnauthorized, expectedBody: errBody},
}
Expand All @@ -44,12 +46,13 @@
assert.Equal(t, tc.statusCode == http.StatusOK, mockHandler.handlerCalled)
assert.Equal(t, tc.expectedBody, strings.TrimSuffix(rr.Body.String(), "\n"))

if strings.HasPrefix(tc.url, "/.well-known") {
if tc.url == service.AlivePath {
assert.False(t, authProvider.extractAuthHeaderCalled)
assert.False(t, authProvider.getAuthMethodCalled)
return
}

assert.True(t, authProvider.extractAuthHeaderCalled)

assert.Equal(t, tc.statusCode == http.StatusOK, authProvider.getAuthMethodCalled)
})
}
Expand Down
Loading