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

Function TestBasicAuth401

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

Source from the content-addressed store, hash-verified

99}
100
101func TestBasicAuth401(t *testing.T) {
102 called := false
103 accounts := Accounts{"foo": "bar"}
104 router := New()
105 router.Use(BasicAuth(accounts))
106 router.GET("/login", func(c *Context) {
107 called = true
108 c.String(http.StatusOK, c.MustGet(AuthUserKey).(string))
109 })
110
111 w := httptest.NewRecorder()
112 req, _ := http.NewRequest(http.MethodGet, "/login", nil)
113 req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password")))
114 router.ServeHTTP(w, req)
115
116 assert.False(t, called)
117 assert.Equal(t, http.StatusUnauthorized, w.Code)
118 assert.Equal(t, "Basic realm=\"Authorization Required\"", w.Header().Get("WWW-Authenticate"))
119}
120
121func TestBasicAuth401WithCustomRealm(t *testing.T) {
122 called := false

Callers

nothing calls this directly

Calls 10

NewFunction · 0.85
BasicAuthFunction · 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