func Test_SessionCookieStore(t *testing.T) { if err := quick.Check(func(k, v string) bool { expected := make(kocha.Session) expected[k] = v store := kocha.NewTestSessionCookieStore() r, err := store.Save(expected) if err != nil { t.Fatal(err) } actual, err := store.Load(r) if err != nil { t.Fatal(err) } return reflect.DeepEqual(actual, expected) }, nil); err != nil { t.Error(err) } func() { store := kocha.NewTestSessionCookieStore() key := "invalid" _, err := store.Load(key) actual := err expect := fmt.Errorf("kocha: session cookie value too short") if !reflect.DeepEqual(actual, expect) { t.Errorf(`SessionCookieStore.Load(%#v) => _, %#v; want %#v`, key, actual, expect) } }() }
func TestSessionMiddleware_Before(t *testing.T) { newRequestResponse := func(cookie *http.Cookie) (*kocha.Request, *kocha.Response) { r, err := http.NewRequest("GET", "/", nil) if err != nil { t.Fatal(err) } req := &kocha.Request{Request: r} if cookie != nil { req.AddCookie(cookie) } res := &kocha.Response{ResponseWriter: httptest.NewRecorder()} return req, res } origNow := util.Now util.Now = func() time.Time { return time.Unix(1383820443, 0) } defer func() { util.Now = origNow }() // test new session func() { app := kocha.NewTestApp() req, res := newRequestResponse(nil) c := &kocha.Context{Request: req, Response: res} m := &kocha.SessionMiddleware{Store: &NullSessionStore{}} err := m.Process(app, c, func() error { actual := c.Session expected := make(kocha.Session) if !reflect.DeepEqual(actual, expected) { t.Errorf("Expect %v, but %v", expected, actual) } return fmt.Errorf("expected error") }) var actual interface{} = err var expect interface{} = fmt.Errorf("expected error") if !reflect.DeepEqual(actual, expect) { t.Errorf(`kocha.SessionMiddleware.Process(app, c, func) => %#v; want %#v`, actual, expect) } actual = c.Session expect = make(kocha.Session) if !reflect.DeepEqual(actual, expect) { t.Errorf("Expect %v, but %v", expect, actual) } }() // test expires not found func() { app := kocha.NewTestApp() store := kocha.NewTestSessionCookieStore() sess := make(kocha.Session) value, err := store.Save(sess) if err != nil { t.Fatal(err) } m := newTestSessionMiddleware(store) cookie := &http.Cookie{ Name: m.Name, Value: value, } req, res := newRequestResponse(cookie) c := &kocha.Context{Request: req, Response: res} err = m.Process(app, c, func() error { actual := c.Session expected := make(kocha.Session) if !reflect.DeepEqual(actual, expected) { t.Errorf("Expect %v, but %v", expected, actual) } return fmt.Errorf("expected error") }) var actual interface{} = err var expect interface{} = fmt.Errorf("expected error") if !reflect.DeepEqual(actual, expect) { t.Errorf(`kocha.SessionMiddleware.Process(app, c, func) => %#v; want %#v`, actual, expect) } actual = c.Session expect = make(kocha.Session) if !reflect.DeepEqual(actual, expect) { t.Errorf("Expect %v, but %v", expect, actual) } }() // test expires invalid time format func() { app := kocha.NewTestApp() store := kocha.NewTestSessionCookieStore() m := newTestSessionMiddleware(store) sess := make(kocha.Session) sess[m.ExpiresKey] = "invalid format" value, err := store.Save(sess) if err != nil { t.Fatal(err) } cookie := &http.Cookie{ Name: m.Name, Value: value, } req, res := newRequestResponse(cookie) c := &kocha.Context{Request: req, Response: res} err = m.Process(app, c, func() error { actual := c.Session expect := make(kocha.Session) if !reflect.DeepEqual(actual, expect) { t.Errorf("Expect %v, but %v", expect, actual) } return fmt.Errorf("expected error") }) var actual interface{} = err var expect interface{} = fmt.Errorf("expected error") if !reflect.DeepEqual(actual, expect) { t.Errorf(`kocha.SessionMiddleware.Process(app, c, func) => %#v; want %#v`, actual, expect) } actual = c.Session expect = make(kocha.Session) if !reflect.DeepEqual(actual, expect) { t.Errorf("Expect %v, but %v", expect, actual) } }() // test expired func() { app := kocha.NewTestApp() store := kocha.NewTestSessionCookieStore() m := newTestSessionMiddleware(store) sess := make(kocha.Session) sess[m.ExpiresKey] = "1383820442" value, err := store.Save(sess) if err != nil { t.Fatal(err) } cookie := &http.Cookie{ Name: m.Name, Value: value, } req, res := newRequestResponse(cookie) c := &kocha.Context{Request: req, Response: res} err = m.Process(app, c, func() error { actual := c.Session expected := make(kocha.Session) if !reflect.DeepEqual(actual, expected) { t.Errorf("Expect %v, but %v", expected, actual) } return fmt.Errorf("expected error") }) var actual interface{} = err var expect interface{} = fmt.Errorf("expected error") if !reflect.DeepEqual(actual, expect) { t.Errorf(`kocha.SessionMiddleware.Process(app, c, func) => %#v; want %#v`, actual, expect) } actual = c.Session expect = make(kocha.Session) if !reflect.DeepEqual(actual, expect) { t.Errorf("Expect %v, but %v", expect, actual) } }() func() { app := kocha.NewTestApp() store := kocha.NewTestSessionCookieStore() m := newTestSessionMiddleware(store) sess := make(kocha.Session) sess[m.ExpiresKey] = "1383820443" sess["brown fox"] = "lazy dog" value, err := store.Save(sess) if err != nil { t.Fatal(err) } cookie := &http.Cookie{ Name: m.Name, Value: value, } req, res := newRequestResponse(cookie) c := &kocha.Context{Request: req, Response: res} err = m.Process(app, c, func() error { return fmt.Errorf("expected error") }) var actual interface{} = err var expect interface{} = fmt.Errorf("expected error") if !reflect.DeepEqual(actual, expect) { t.Errorf(`kocha.SessionMiddlware.Process(app, c, func) => %#v; want %#v`, actual, expect) } actual = c.Session expect = kocha.Session{ m.ExpiresKey: "1383820443", "brown fox": "lazy dog", } if !reflect.DeepEqual(actual, expect) { t.Errorf("Expect %v, but %v", expect, actual) } }() }