MCPcopy Index your code
hub / github.com/labstack/echo / TestBasicAuthRealm

Function TestBasicAuthRealm

middleware/basic_auth_test.go:183–240  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

181}
182
183func TestBasicAuthRealm(t *testing.T) {
184 e := echo.New()
185 mockValidator := func(c *echo.Context, u, p string) (bool, error) {
186 return false, nil // Always fail to trigger WWW-Authenticate header
187 }
188
189 tests := []struct {
190 name string
191 realm string
192 expectedAuth string
193 }{
194 {
195 name: "Default realm",
196 realm: "Restricted",
197 expectedAuth: `basic realm="Restricted"`,
198 },
199 {
200 name: "Custom realm",
201 realm: "My API",
202 expectedAuth: `basic realm="My API"`,
203 },
204 {
205 name: "Realm with special characters",
206 realm: `Realm with "quotes" and \backslashes`,
207 expectedAuth: `basic realm="Realm with \"quotes\" and \\backslashes"`,
208 },
209 {
210 name: "Empty realm (falls back to default)",
211 realm: "",
212 expectedAuth: `basic realm="Restricted"`,
213 },
214 {
215 name: "Realm with unicode",
216 realm: "测试领域",
217 expectedAuth: `basic realm="测试领域"`,
218 },
219 }
220
221 for _, tt := range tests {
222 t.Run(tt.name, func(t *testing.T) {
223 req := httptest.NewRequest(http.MethodGet, "/", nil)
224 res := httptest.NewRecorder()
225 c := e.NewContext(req, res)
226
227 h := BasicAuthWithConfig(BasicAuthConfig{
228 Validator: mockValidator,
229 Realm: tt.realm,
230 })(func(c *echo.Context) error {
231 return c.String(http.StatusOK, "test")
232 })
233
234 err := h(c)
235
236 assert.Equal(t, echo.ErrUnauthorized, err)
237 assert.Equal(t, tt.expectedAuth, res.Header().Get(echo.HeaderWWWAuthenticate))
238 })
239 }
240}

Callers

nothing calls this directly

Calls 5

StringMethod · 0.95
BasicAuthWithConfigFunction · 0.85
NewContextMethod · 0.80
GetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…