49 lines
997 B
Go
49 lines
997 B
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetSessionCookieMissingCookie(t *testing.T) {
|
|
r, _ := http.NewRequest("GET", "/", nil)
|
|
w := httptest.NewRecorder()
|
|
sessionKey, err := getSessionCookie(w, r)
|
|
|
|
if err != nil {
|
|
t.Fail()
|
|
}
|
|
if sessionKey == "" {
|
|
t.Errorf("Session key is empty")
|
|
}
|
|
|
|
cookies := w.Result().Cookies()
|
|
for _, c := range cookies {
|
|
if c.Name == "session" {
|
|
if c.Value != sessionKey {
|
|
t.Errorf("Wrong sessionKey %q != %q", c.Value, sessionKey)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGetSessionCookieCookieSet(t *testing.T) {
|
|
mySessionKey := "12341234"
|
|
|
|
r, _ := http.NewRequest("GET", "/", nil)
|
|
cookie := &http.Cookie{Name: "session", Value: mySessionKey}
|
|
r.AddCookie(cookie)
|
|
|
|
w := httptest.NewRecorder()
|
|
sessionKey, err := getSessionCookie(w, r)
|
|
if err != nil {
|
|
t.Errorf("err != nil in getSessionCookie")
|
|
}
|
|
|
|
if mySessionKey != sessionKey {
|
|
t.Errorf("getSessionKey didn't fetch sessionKey from \"session\" cookie")
|
|
}
|
|
}
|