Debian Codegraph @

memcache/memcache_test.go

raw symbols SCIP
1 // Copyright 2014 The Macaron Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"): you may
4 // not use this file except in compliance with the License. You may obtain
5 // a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations
13 // under the License.
14
15 package session
16
17 import (
18 "net/http"
19 "net/http/httptest"
20 "testing"
21
22 . "github.com/smartystreets/goconvey/convey"
23 "gopkg.in/macaron.v1"
24
25 "github.com/go-macaron/session"
26 )
27
28 func Test_MemcacheProvider(t *testing.T) {
29 Convey("Test memcache session provider", t, func() {
30 opt := session.Options{
31 Provider: "memcache",
32 ProviderConfig: "127.0.0.1:9090",
33 }
34
35 Convey("Basic operation", func() {
36 m := macaron.New()
37 m.Use(session.Sessioner(opt))
38
39 m.Get("/", func(ctx *macaron.Context, sess session.Store) {
40 sess.Set("uname", "unknwon")
41 })
42 m.Get("/reg", func(ctx *macaron.Context, sess session.Store) {
43 raw, err := sess.RegenerateId(ctx)
44 So(err, ShouldBeNil)
45 So(raw, ShouldNotBeNil)
46
47 uname := raw.Get("uname")
48 So(uname, ShouldNotBeNil)
49 So(uname, ShouldEqual, "unknwon")
50 })
51 m.Get("/get", func(ctx *macaron.Context, sess session.Store) {
52 sid := sess.ID()
53 So(sid, ShouldNotBeEmpty)
54
55 raw, err := sess.Read(sid)
56 So(err, ShouldBeNil)
57 So(raw, ShouldNotBeNil)
58
59 uname := sess.Get("uname")
60 So(uname, ShouldNotBeNil)
61 So(uname, ShouldEqual, "unknwon")
62
63 So(sess.Delete("uname"), ShouldBeNil)
64 So(sess.Get("uname"), ShouldBeNil)
65
66 So(sess.Destory(ctx), ShouldBeNil)
67 })
68
69 resp := httptest.NewRecorder()
70 req, err := http.NewRequest("GET", "/", nil)
71 So(err, ShouldBeNil)
72 m.ServeHTTP(resp, req)
73
74 cookie := resp.Header().Get("Set-Cookie")
75
76 resp = httptest.NewRecorder()
77 req, err = http.NewRequest("GET", "/reg", nil)
78 So(err, ShouldBeNil)
79 req.Header.Set("Cookie", cookie)
80 m.ServeHTTP(resp, req)
81
82 cookie = resp.Header().Get("Set-Cookie")
83
84 resp = httptest.NewRecorder()
85 req, err = http.NewRequest("GET", "/get", nil)
86 So(err, ShouldBeNil)
87 req.Header.Set("Cookie", cookie)
88 m.ServeHTTP(resp, req)
89 })
90
91 Convey("Regenrate empty session", func() {
92 m := macaron.New()
93 m.Use(session.Sessioner(opt))
94 m.Get("/", func(ctx *macaron.Context, sess session.Store) {
95 raw, err := sess.RegenerateId(ctx)
96 So(err, ShouldBeNil)
97 So(raw, ShouldNotBeNil)
98 })
99
100 resp := httptest.NewRecorder()
101 req, err := http.NewRequest("GET", "/", nil)
102 So(err, ShouldBeNil)
103 req.Header.Set("Cookie", "MacaronSession=ad2c7e3cbecfcf486; Path=/;")
104 m.ServeHTTP(resp, req)
105 })
106 })
107 }