| 99 | } |
| 100 | |
| 101 | func 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 | |
| 121 | func TestBasicAuth401WithCustomRealm(t *testing.T) { |
| 122 | called := false |