From 53ec9b33103fea7d2a9a0ace5a6fb45cfc292142 Mon Sep 17 00:00:00 2001 From: Hans Lawrenz Date: Mon, 19 May 2014 10:50:30 -0400 Subject: [PATCH] Fix save function to actually update database record. Add a test. --- pgstore.go | 2 +- pgstore_test.go | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 pgstore_test.go diff --git a/pgstore.go b/pgstore.go index 3d0c4f0..74d9fe6 100644 --- a/pgstore.go +++ b/pgstore.go @@ -180,7 +180,7 @@ func (db *PGStore) save(session *sessions.Session) error { if session.IsNew { err = db.DbMap.Insert(&s) } else { - _, err = db.DbMap.Update(&s) + _, err = db.DbMap.Exec("update http_sessions set data=$1, modifiedon=$2, expireson=$3 where key=$4", s.Data, s.ModifiedOn, s.ExpiresOn, s.Key) } return err diff --git a/pgstore_test.go b/pgstore_test.go new file mode 100644 index 0000000..d32377d --- /dev/null +++ b/pgstore_test.go @@ -0,0 +1,101 @@ +package pgstore + +import ( + "github.com/gorilla/securecookie" + "github.com/gorilla/sessions" + "net/http" + "os" + "testing" +) +type headerOnlyResponseWriter http.Header + +func (ho headerOnlyResponseWriter) Header() http.Header { + return http.Header(ho) +} + +func (ho headerOnlyResponseWriter) Write([]byte) (int, error) { + panic("NOIMPL") +} + +func (ho headerOnlyResponseWriter) WriteHeader(int) { + panic("NOIMPL") +} + +var secret = "EyaC2BPcJtNqU3tjEHy+c+Wmqc1yihYIbUWEl/jk0Ga73kWBclmuSFd9HuJKwJw/Wdsh1XnjY2Bw1HBVph6WOw==" + +func TestPGStore(t *testing.T) { + ss := NewPGStore(os.Getenv("PGSTORE_TEST_CONN"), []byte(secret)) + + defer ss.Close() + + // ROUND 1 - Check that the cookie is being saved + req, err := http.NewRequest("GET", "http://www.example.com", nil) + if err != nil { + t.Fatal("failed to create request", err) + } + + session, err := ss.Get(req, "mysess") + if err != nil { + t.Fatal("failed to get session", err.Error()) + } + + session.Values["counter"] = 1 + + m := make(http.Header) + if err = ss.Save(req, headerOnlyResponseWriter(m), session); err != nil { + t.Fatal("Failed to save session:", err.Error()) + } + + if m["Set-Cookie"][0][0:6] != "mysess" { + t.Fatal("Cookie wasn't set!") + } + + // ROUND 2 - check that the cookie can be retrieved + req, err = http.NewRequest("GET", "http://www.example.com", nil) + if err != nil { + t.Fatal("failed to create round 2 request", err) + } + + encoded, err := securecookie.EncodeMulti(session.Name(), session.ID, ss.Codecs...) + if err != nil { + t.Fatal("Failed to make cookie value", err) + } + + req.AddCookie(sessions.NewCookie(session.Name(), encoded, session.Options)) + + + session, err = ss.Get(req, "mysess") + if err != nil { + t.Fatal("failed to get round 2 session", err.Error()) + } + + if session.Values["counter"] != 1 { + t.Fatal("Retrieved session had wrong value:", session.Values["counter"]) + } + + session.Values["counter"] = 9 // set new value for round 3 + if err = ss.Save(req, headerOnlyResponseWriter(m), session); err != nil { + t.Fatal("Failed to save session:", err.Error()) + } + + + // ROUND 2 - check that the cookie has been updated + req, err = http.NewRequest("GET", "http://www.example.com", nil) + if err != nil { + t.Fatal("failed to create round 3 request", err) + } + req.AddCookie(sessions.NewCookie(session.Name(), encoded, session.Options)) + + session, err = ss.Get(req, "mysess") + if err != nil { + t.Fatal("failed to get session round 3", err.Error()) + } + + if session.Values["counter"] != 9 { + t.Fatal("Retrieved session had wrong value in round 3:", session.Values["counter"]) + } + +} + + +