Skip to content

Commit fa70db8

Browse files
authored
Add Skipper Unit Test In BasicBasicAuthConfig and Add More Detail Explanation regarding BasicAuthValidator (#2461)
* Add Skipper Unit Test In BasicBasicAuthConfig and Add More detail explanation regarding BasicAuthValidator * Simplify Skipper Unit Test
1 parent ea529bb commit fa70db8

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

middleware/basic_auth.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type (
2525
}
2626

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

middleware/basic_auth_test.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func TestBasicAuth(t *testing.T) {
3232
assert.NoError(t, h(c))
3333

3434
h = BasicAuthWithConfig(BasicAuthConfig{
35-
Skipper: nil,
3635
Validator: f,
3736
Realm: "someRealm",
3837
})(func(c echo.Context) error {
@@ -72,4 +71,20 @@ func TestBasicAuth(t *testing.T) {
7271
req.Header.Set(echo.HeaderAuthorization, auth)
7372
he = h(c).(*echo.HTTPError)
7473
assert.Equal(t, http.StatusUnauthorized, he.Code)
74+
75+
h = BasicAuthWithConfig(BasicAuthConfig{
76+
Validator: f,
77+
Realm: "someRealm",
78+
Skipper: func(c echo.Context) bool {
79+
return true
80+
},
81+
})(func(c echo.Context) error {
82+
return c.String(http.StatusOK, "test")
83+
})
84+
85+
// Skipped Request
86+
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:skip"))
87+
req.Header.Set(echo.HeaderAuthorization, auth)
88+
assert.NoError(t, h(c))
89+
7590
}

0 commit comments

Comments
 (0)