| 119 | } |
| 120 | |
| 121 | func 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 | |
| 141 | func TestBasicAuthForProxySucceed(t *testing.T) { |
| 142 | accounts := Accounts{"admin": "password"} |