| 82 | } |
| 83 | |
| 84 | func 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 | |
| 101 | func TestBasicAuth401(t *testing.T) { |
| 102 | called := false |