Skip to content

Commit

Permalink
Moving sessions test into a seperate file
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Jun 11, 2011
1 parent b010a27 commit ad0be06
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
* Better cookie handler
* Domain/Expiry
* Clean up/Flesh out tests more
* Use http/httptest for full-stack testing

POTENTIAL MIDDLEWARE
====================
Expand Down
44 changes: 0 additions & 44 deletions mango_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ func loggerTestServer(env Env) (Status, Headers, Body) {
return 200, Headers{}, Body("Hello World!")
}

func sessionsTestServer(env Env) (Status, Headers, Body) {
env.Session()["test_attribute"] = "Never gonna give you up"
return 200, Headers{}, Body("Hello World!")
}

func showErrorsTestServer(env Env) (Status, Headers, Body) {
panic("foo!")
return 200, Headers{}, Body("Hello World!")
Expand All @@ -47,10 +42,6 @@ func init() {
loggerStack.Middleware(Logger(custom_logger))
testRoutes["/logger"] = loggerStack.Compile(loggerTestServer)

sessionsStack := new(Stack)
sessionsStack.Middleware(Sessions("my_secret", "my_key", ".my.domain.com"))
testRoutes["/sessions"] = sessionsStack.Compile(sessionsTestServer)

showErrorsStack := new(Stack)
showErrorsStack.Middleware(ShowErrors("<html><body>{Error|html}</body></html>"))
testRoutes["/show_errors"] = showErrorsStack.Compile(showErrorsTestServer)
Expand Down Expand Up @@ -117,41 +108,6 @@ func BenchmarkLogger(b *testing.B) {
}
}

func TestSessions(t *testing.T) {
// Request against it
response, err := client.Get("http://localhost:3000/sessions")

if err != nil {
t.Error(err)
}

if response.StatusCode != 200 {
t.Error("Expected status to equal 200, got:", response.StatusCode)
}

expected_name := "my_key"
if response.SetCookie[0].Name != expected_name {
t.Error("Expected Set-Cookie name to equal: \"", expected_name, "\" got: \"", response.SetCookie[0].Name, "\"")
}

// base 64 encoded, hmac-hashed, and gob encoded stuff
expected_value := "Dv+BBAEC/4IAAQwBEAAANf+CAAEOdGVzdF9hdHRyaWJ1dGUGc3RyaW5nDBkAF05ldmVyIGdvbm5hIGdpdmUgeW91IHVw--bdHyJ5lvPpk6EoZiSSSiHKZtQHk="
if response.SetCookie[0].Value != expected_value {
t.Error("Expected Set-Cookie value to equal: \"", expected_value, "\" got: \"", response.SetCookie[0].Value, "\"")
}

expected_domain := ".my.domain.com"
if response.SetCookie[0].Domain != expected_domain {
t.Error("Expected Set-Cookie domain to equal: \"", expected_domain, "\" got: \"", response.SetCookie[0].Domain, "\"")
}
}

func BenchmarkSessions(b *testing.B) {
for i := 0; i < b.N; i++ {
client.Get("http://localhost:3000/sessions")
}
}

func TestShowErrors(t *testing.T) {
// Request against it
response, err := client.Get("http://localhost:3000/show_errors")
Expand Down
42 changes: 42 additions & 0 deletions sessions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package mango

import (
"http"
"runtime"
"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 TestSessions(t *testing.T) {
// 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)
status, headers, _ := sessionsApp(Env{"mango.request": &Request{request}})

if err != nil {
t.Error(err)
}

if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}

// 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, "\"")
}
}

0 comments on commit ad0be06

Please sign in to comment.