MCPcopy
hub / github.com/gin-gonic/gin / TestBasicAuth401WithCustomRealm

Function TestBasicAuth401WithCustomRealm

auth_test.go:121–139  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

119}
120
121func TestBasicAuth401WithCustomRealm(t *testing.T) {
122 called := false
123 accounts := Accounts{"foo": "bar"}
124 router := New()
125 router.Use(BasicAuthForRealm(accounts, "My Custom \"Realm\""))
126 router.GET("/login", func(c *Context) {
127 called = true
128 c.String(http.StatusOK, c.MustGet(AuthUserKey).(string))
129 })
130
131 w := httptest.NewRecorder()
132 req, _ := http.NewRequest(http.MethodGet, "/login", nil)
133 req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password")))
134 router.ServeHTTP(w, req)
135
136 assert.False(t, called)
137 assert.Equal(t, http.StatusUnauthorized, w.Code)
138 assert.Equal(t, "Basic realm=\"My Custom \\\"Realm\\\"\"", w.Header().Get("WWW-Authenticate"))
139}
140
141func TestBasicAuthForProxySucceed(t *testing.T) {
142 accounts := Accounts{"admin": "password"}

Callers

nothing calls this directly

Calls 10

NewFunction · 0.85
BasicAuthForRealmFunction · 0.85
MustGetMethod · 0.80
SetMethod · 0.80
UseMethod · 0.65
GETMethod · 0.65
StringMethod · 0.65
ServeHTTPMethod · 0.45
GetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected