示例#1
0
func TestHttpAuth(t *testing.T) {
	is := is.New(t)
	n := noodle.New(mw.HTTPAuth("test", func(u, p string) bool {
		return p == "testPassword"
	})).Then(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		user := mw.GetUser(ctx)
		is.Equal(user, "testUser")
		return nil
	})

	r, _ := http.NewRequest("GET", "http://localhost", nil)
	w := httptest.NewRecorder()
	err := n(context.TODO(), w, r)
	is.Err(err)
	is.Equal(err, mw.UnauthorizedRequest)
	is.Equal(w.Code, http.StatusUnauthorized)
	is.Equal(w.Header().Get("WWW-Authenticate"), "Basic realm=test")

	r.SetBasicAuth("testUser", "wrongPassword")
	is.Err(n(context.TODO(), w, r))

	r.SetBasicAuth("testUser", "testPassword")
	is.NotErr(n(context.TODO(), w, r))
}
示例#2
0
文件: main.go 项目: andviro/noodle
func dashIndex(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
	res := map[string]interface{}{
		"User": mw.GetUser(ctx),
	}
	return render.Yield(ctx, 201, res)
}