Skip to content

Commit

Permalink
Add Skipper Unit Test In BasicBasicAuthConfig and Add More Detail Exp…
Browse files Browse the repository at this point in the history
…lanation regarding BasicAuthValidator (#2461)

* Add Skipper Unit Test In BasicBasicAuthConfig and Add More detail explanation regarding BasicAuthValidator

* Simplify Skipper Unit Test
  • Loading branch information
RyoKusnadi committed Feb 18, 2024
1 parent ea529bb commit fa70db8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions middleware/basic_auth.go
Expand Up @@ -25,6 +25,8 @@ type (
}

// BasicAuthValidator defines a function to validate BasicAuth credentials.
// The function should return a boolean indicating whether the credentials are valid,
// and an error if any error occurs during the validation process.
BasicAuthValidator func(string, string, echo.Context) (bool, error)
)

Expand Down
17 changes: 16 additions & 1 deletion middleware/basic_auth_test.go
Expand Up @@ -32,7 +32,6 @@ func TestBasicAuth(t *testing.T) {
assert.NoError(t, h(c))

h = BasicAuthWithConfig(BasicAuthConfig{
Skipper: nil,
Validator: f,
Realm: "someRealm",
})(func(c echo.Context) error {
Expand Down Expand Up @@ -72,4 +71,20 @@ func TestBasicAuth(t *testing.T) {
req.Header.Set(echo.HeaderAuthorization, auth)
he = h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusUnauthorized, he.Code)

h = BasicAuthWithConfig(BasicAuthConfig{
Validator: f,
Realm: "someRealm",
Skipper: func(c echo.Context) bool {
return true
},
})(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
})

// Skipped Request
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:skip"))
req.Header.Set(echo.HeaderAuthorization, auth)
assert.NoError(t, h(c))

}

0 comments on commit fa70db8

Please sign in to comment.