func (s *AuthSuite) TestRequestBadPassword(c *C) { a := &AuthMiddleware{authKeys: []authKey{{username: "******", password: "******"}}} h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "treasure") }) auth, err := a.NewHandler(h) c.Assert(err, IsNil) srv := httptest.NewServer(auth) defer srv.Close() // bad pass re, _, err := testutils.Get(srv.URL, testutils.BasicAuth(a.authKeys[0].username, "open please")) c.Assert(err, IsNil) c.Assert(re.StatusCode, Equals, http.StatusUnauthorized) // missing header re, _, err = testutils.Get(srv.URL) c.Assert(err, IsNil) c.Assert(re.StatusCode, Equals, http.StatusUnauthorized) // malformed header re, _, err = testutils.Get(srv.URL, testutils.Header("Authorization", "blablabla=")) c.Assert(err, IsNil) c.Assert(re.StatusCode, Equals, http.StatusUnauthorized) }
func (s *AuthSuite) TestRequestSuccess(c *C) { a := &AuthMiddleware{authKeys: []authKey{{username: "******", password: "******"}, {username: "******", password: "******"}}} h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "treasure") }) auth, err := a.NewHandler(h) c.Assert(err, IsNil) srv := httptest.NewServer(auth) defer srv.Close() _, body, err := testutils.Get(srv.URL, testutils.BasicAuth(a.authKeys[0].username, a.authKeys[0].password)) c.Assert(err, IsNil) c.Assert(string(body), Equals, "treasure") _, body, err = testutils.Get(srv.URL, testutils.BasicAuth(a.authKeys[1].username, a.authKeys[1].password)) c.Assert(err, IsNil) c.Assert(string(body), Equals, "treasure") }