You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.1 KiB

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)
}
if c.Path != "/" {
t.Errorf("Wrong cookiepath %q != %q", c.Path, "/")
}
break
}
}
}
func TestGetSessionCookieCookieSet(t *testing.T) {
mySessionKey := "12341234"
r, _ := http.NewRequest("GET", "/", nil)
cookie := &http.Cookie{Name: "session", Value: mySessionKey, Path: r.URL.Path}
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")
}
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)
}
if c.Path != "" {
t.Errorf("Wrong cookiepath %q != %q", c.Path, "")
}
break
}
}
}
func TestGetSessionCookieCookieSetAuth(t *testing.T) {
mySessionKey := "12341234"
r, _ := http.NewRequest("GET", "/auth/", nil)
cookie := &http.Cookie{Name: "session", Value: mySessionKey, Path: r.URL.Path}
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")
}
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)
}
if c.Path != "" {
t.Errorf("Wrong cookiepath %q != %q", c.Path, "")
}
break
}
}
}