Browse files

Merge branch 'develop'

  • Loading branch information...
2 parents a45b3ce + 3852a01 commit 19a51bee2bc98140cc863db6d04bcad001c04558 @paulbellamy committed Jun 18, 2011
Showing with 65 additions and 15 deletions.
  1. +1 −1 mango.go
  2. +13 −6 sessions.go
  3. +51 −8 sessions_test.go
View
2 mango.go
@@ -101,7 +101,7 @@ type Stack struct {
}
func Version() []int {
- return []int{0, 3, 3}
+ return []int{0, 3, 4}
}
func VersionString() string {
View
19 sessions.go
@@ -8,6 +8,7 @@ import (
"fmt"
"io/ioutil"
"gob"
+ "http"
"strings"
)
@@ -64,7 +65,7 @@ func decodeCookie(value, secret string) (cookie map[string]interface{}) {
}
func encodeGob(value interface{}) (result string) {
- buffer := &bytes.Buffer{}
+ buffer := new(bytes.Buffer)
encoder := gob.NewEncoder(buffer)
encoder.Encode(value)
return buffer.String()
@@ -79,7 +80,7 @@ func dePad64(value string) (result string) {
}
func encode64(value string) (result string) {
- buffer := &bytes.Buffer{}
+ buffer := new(bytes.Buffer)
encoder := base64.NewEncoder(base64.URLEncoding, buffer)
encoder.Write([]byte(value))
encoder.Close()
@@ -93,17 +94,23 @@ func encodeCookie(value map[string]interface{}, secret string) (cookie string) {
}
func prepareSession(env Env, key, secret string) {
- if cookie, err := env.Request().Cookie(key); err == nil {
- env["mango.session"] = decodeCookie(cookie.Value, secret)
- return
+ for _, cookie := range env.Request().Cookies() {
+ if cookie.Name == key {
+ env["mango.session"] = decodeCookie(cookie.Value, secret)
+ return
+ }
}
// Didn't find a session to decode
env["mango.session"] = make(map[string]interface{})
}
func commitSession(headers Headers, env Env, key, secret, domain string) {
- headers.Add("Set-Cookie", fmt.Sprintf("%s=%s; Domain=%s;", key, encodeCookie(env["mango.session"].(map[string]interface{}), secret), domain))
+ cookie := new(http.Cookie)
+ cookie.Name = key
+ cookie.Value = encodeCookie(env["mango.session"].(map[string]interface{}), secret)
+ cookie.Domain = domain
+ headers.Add("Set-Cookie", cookie.String())
}
func Sessions(secret, key, domain string) Middleware {
View
59 sessions_test.go
@@ -3,26 +3,50 @@ package mango
import (
"http"
"runtime"
+ "strings"
"testing"
)
-func sessionsTestServer(env Env) (Status, Headers, Body) {
- env.Session()["test_attribute"] = "Never gonna give you up"
- return 200, Headers{}, Body("Hello World!")
-}
-
func init() {
runtime.GOMAXPROCS(4)
}
+func TestSessionEncodingDecoding(t *testing.T) {
+ cookie := map[string]interface{}{"value": "foo"}
+ secret := "secret"
+ result := decodeCookie(encodeCookie(cookie, secret), secret)
+
+ if len(result) != len(cookie) {
+ t.Error("Expected cookie to equal:", cookie, "got:", result)
+ }
+
+ if result["value"] != cookie["value"] {
+ t.Error("Expected cookie[\"value\"] to equal:", cookie["value"], "got:", result["value"])
+ }
+}
+
func TestSessions(t *testing.T) {
+ sessionsTestServer := func(env Env) (Status, Headers, Body) {
+ counter := env.Session()["counter"].(int)
+ if counter != 1 {
+ t.Error("Expected session[\"counter\"] to equal:", 1, "got:", counter)
+ }
+ env.Session()["counter"] = counter + 1
+ return 200, Headers{}, Body("Hello World!")
+ }
+
// Compile the stack
sessionsStack := new(Stack)
sessionsStack.Middleware(Sessions("my_secret", "my_key", ".my.domain.com"))
sessionsApp := sessionsStack.Compile(sessionsTestServer)
// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/", nil)
+ initial_cookie := new(http.Cookie)
+ initial_cookie.Name = "my_key"
+ initial_cookie.Value = encodeCookie(map[string]interface{}{"counter": 1}, "my_secret")
+ initial_cookie.Domain = ".my.domain.com"
+ request.AddCookie(initial_cookie)
status, headers, _ := sessionsApp(Env{"mango.request": &Request{request}})
if err != nil {
@@ -35,15 +59,34 @@ func TestSessions(t *testing.T) {
// base 64 encoded, hmac-hashed, and gob encoded stuff
cookie := headers.Get("Set-Cookie")
- expected_cookie := "my_key=Dv-BBAEC_4IAAQwBEAAANf-CAAEOdGVzdF9hdHRyaWJ1dGUGc3RyaW5nDBkAF05ldmVyIGdvbm5hIGdpdmUgeW91IHVw--bdHyJ5lvPpk6EoZiSSSiHKZtQHk; Domain=.my.domain.com;"
- if cookie != expected_cookie {
- t.Error("Expected Set-Cookie to equal: \"", expected_cookie, "\" got: \"", cookie, "\"")
+ if cookie == "" {
+ t.Error("Expected the Set-Cookie header to be set")
+ }
+
+ unparsed := strings.Split(strings.Split(cookie, "=", 2)[1], ";", 2)[0]
+ value := decodeCookie(unparsed, "my_secret")
+ expected_value := map[string]interface{}{"counter": int(2)}
+ if len(value) != len(expected_value) {
+ t.Error("Expected cookie to equal:", expected_value, "got:", value)
+ }
+
+ if value["counter"].(int) != expected_value["counter"].(int) {
+ t.Error("Expected cookie[\"counter\"] to equal:", expected_value["counter"], "got:", value["counter"])
+ }
+
+ if !strings.Contains(headers.Get("Set-Cookie"), "; Domain=.my.domain.com") {
+ t.Error("Expected cookie ", headers.Get("Set-Cookie"), " to contain: '; Domain=.my.domain.com'")
}
}
func BenchmarkSessions(b *testing.B) {
b.StopTimer()
+ sessionsTestServer := func(env Env) (Status, Headers, Body) {
+ env.Session()["counter"] = env.Session()["counter"].(int) + 1
+ return 200, Headers{}, Body("Hello World!")
+ }
+
sessionsStack := new(Stack)
sessionsStack.Middleware(Sessions("my_secret", "my_key", ".my.domain.com"))
sessionsApp := sessionsStack.Compile(sessionsTestServer)

0 comments on commit 19a51be

Please sign in to comment.