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

Function TestBasicAuthSucceed

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

Source from the content-addressed store, hash-verified

82}
83
84func TestBasicAuthSucceed(t *testing.T) {
85 accounts := Accounts{"admin": "password"}
86 router := New()
87 router.Use(BasicAuth(accounts))
88 router.GET("/login", func(c *Context) {
89 c.String(http.StatusOK, c.MustGet(AuthUserKey).(string))
90 })
91
92 w := httptest.NewRecorder()
93 req, _ := http.NewRequest(http.MethodGet, "/login", nil)
94 req.Header.Set("Authorization", authorizationHeader("admin", "password"))
95 router.ServeHTTP(w, req)
96
97 assert.Equal(t, http.StatusOK, w.Code)
98 assert.Equal(t, "admin", w.Body.String())
99}
100
101func TestBasicAuth401(t *testing.T) {
102 called := false

Callers

nothing calls this directly

Calls 9

NewFunction · 0.85
BasicAuthFunction · 0.85
authorizationHeaderFunction · 0.85
MustGetMethod · 0.80
SetMethod · 0.80
UseMethod · 0.65
GETMethod · 0.65
StringMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected