示例#1
0
// Displays how to get the Subject in an HTTP context.
func Example() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		subject := kuro.Get(r, w)

		log.Printf("Is Subject authenticated? %s\n", subject.IsAuthenticated())
	})
	log.Fatal(http.ListenAndServe(":6999", nil))
}
示例#2
0
func TestSession(t *testing.T) {
	mgr := NewGorillaManager(sessions.NewCookieStore([]byte("siikrit")))
	kuro.Manager.SetSessionManager(mgr)

	handler := func(w http.ResponseWriter, r *http.Request) {
		subject := kuro.Get(r, w)

		subject.Session().Set("foo", "bar")
		subject.Session().Save()
	}

	req, _ := http.NewRequest("GET", "http://example.com/foo", nil)

	w := httptest.NewRecorder()
	handler(w, req)

	assert.Equal(t, 200, w.Code)

	cookie := w.Header().Get("Set-Cookie")
	assert.NotNil(t, cookie)
	assert.True(t, strings.HasPrefix(cookie, "SESSIONID="))

	logout := func(w http.ResponseWriter, r *http.Request) {
		subject := kuro.Get(r, w)

		subject.Logout()
	}

	req, _ = http.NewRequest("GET", "http://example.com/foo", nil)

	w = httptest.NewRecorder()
	logout(w, req)

	assert.Equal(t, 200, w.Code)

	cookie = w.Header().Get("Set-Cookie")
	assert.NotNil(t, cookie)
	assert.True(t, strings.Contains(cookie, "Max-Age=0"), "Got cookie '%s'", cookie)
}