Skip to content

Commit 311cff9

Browse files
author
anonx
committed
Extract TestSuite out into its own package to close revel#737
2 parents 318e032 + 0c40fe4 commit 311cff9

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

session.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func (s Session) getExpiration() time.Time {
6666
return time.Now().Add(expireAfterDuration)
6767
}
6868

69-
// cookie returns an http.Cookie containing the signed session.
70-
func (s Session) cookie() *http.Cookie {
69+
// Cookie returns an http.Cookie containing the signed session.
70+
func (s Session) Cookie() *http.Cookie {
7171
var sessionValue string
7272
ts := s.getExpiration()
7373
s[TIMESTAMP_KEY] = getSessionExpirationCookie(ts)
@@ -106,9 +106,9 @@ func sessionTimeoutExpiredOrMissing(session Session) bool {
106106
return false
107107
}
108108

109-
// getSessionFromCookie returns a Session struct pulled from the signed
109+
// GetSessionFromCookie returns a Session struct pulled from the signed
110110
// session cookie.
111-
func getSessionFromCookie(cookie *http.Cookie) Session {
111+
func GetSessionFromCookie(cookie *http.Cookie) Session {
112112
session := make(Session)
113113

114114
// Separate the data from the signature.
@@ -149,7 +149,7 @@ func SessionFilter(c *Controller, fc []Filter) {
149149

150150
// Store the signed session if it could have changed.
151151
if len(c.Session) > 0 || !sessionWasEmpty {
152-
c.SetCookie(c.Session.cookie())
152+
c.SetCookie(c.Session.Cookie())
153153
}
154154
}
155155

@@ -160,7 +160,7 @@ func restoreSession(req *http.Request) Session {
160160
if err != nil {
161161
return make(Session)
162162
} else {
163-
return getSessionFromCookie(cookie)
163+
return GetSessionFromCookie(cookie)
164164
}
165165
}
166166

session_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ func TestSessionRestore(t *testing.T) {
1111
originSession := make(Session)
1212
originSession["foo"] = "foo"
1313
originSession["bar"] = "bar"
14-
cookie := originSession.cookie()
14+
cookie := originSession.Cookie()
1515
if !cookie.Expires.IsZero() {
1616
t.Error("incorrect cookie expire", cookie.Expires)
1717
}
1818

19-
restoredSession := getSessionFromCookie(cookie)
19+
restoredSession := GetSessionFromCookie(cookie)
2020
for k, v := range originSession {
2121
if restoredSession[k] != v {
2222
t.Errorf("session restore failed session[%s] != %s", k, v)
@@ -30,9 +30,9 @@ func TestSessionExpire(t *testing.T) {
3030
session["user"] = "Tom"
3131
var cookie *http.Cookie
3232
for i := 0; i < 3; i++ {
33-
cookie = session.cookie()
33+
cookie = session.Cookie()
3434
time.Sleep(time.Second)
35-
session = getSessionFromCookie(cookie)
35+
session = GetSessionFromCookie(cookie)
3636
}
3737
expectExpire := time.Now().Add(expireAfterDuration)
3838
if cookie.Expires.Unix() < expectExpire.Add(-time.Second).Unix() {
@@ -44,16 +44,16 @@ func TestSessionExpire(t *testing.T) {
4444

4545
session.SetNoExpiration()
4646
for i := 0; i < 3; i++ {
47-
cookie = session.cookie()
48-
session = getSessionFromCookie(cookie)
47+
cookie = session.Cookie()
48+
session = GetSessionFromCookie(cookie)
4949
}
50-
cookie = session.cookie()
50+
cookie = session.Cookie()
5151
if !cookie.Expires.IsZero() {
5252
t.Error("expect cookie expires is zero")
5353
}
5454

5555
session.SetDefaultExpiration()
56-
cookie = session.cookie()
56+
cookie = session.Cookie()
5757
expectExpire = time.Now().Add(expireAfterDuration)
5858
if cookie.Expires.Unix() < expectExpire.Add(-time.Second).Unix() {
5959
t.Error("expect expires", cookie.Expires, "after", expectExpire.Add(-time.Second))

skeleton/tests/apptest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package tests
22

3-
import "github.com/revel/revel"
3+
import "github.com/revel/revel/testing"
44

55
type AppTest struct {
6-
revel.TestSuite
6+
testing.TestSuite
77
}
88

99
func (t *AppTest) Before() {

tests.go renamed to testing/testsuite.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package revel
1+
package testing
22

33
import (
44
"bytes"
@@ -16,14 +16,16 @@ import (
1616
"regexp"
1717
"strings"
1818

19+
"github.com/revel/revel"
20+
1921
"golang.org/x/net/websocket"
2022
)
2123

2224
type TestSuite struct {
2325
Client *http.Client
2426
Response *http.Response
2527
ResponseBody []byte
26-
Session Session
28+
Session revel.Session
2729
}
2830

2931
type TestRequest struct {
@@ -39,22 +41,22 @@ func NewTestSuite() TestSuite {
3941
jar, _ := cookiejar.New(nil)
4042
return TestSuite{
4143
Client: &http.Client{Jar: jar},
42-
Session: make(Session),
44+
Session: make(revel.Session),
4345
}
4446
}
4547

4648
// Return the address and port of the server, e.g. "127.0.0.1:8557"
4749
func (t *TestSuite) Host() string {
48-
if Server.Addr[0] == ':' {
49-
return "127.0.0.1" + Server.Addr
50+
if revel.Server.Addr[0] == ':' {
51+
return "127.0.0.1" + revel.Server.Addr
5052
}
51-
return Server.Addr
53+
return revel.Server.Addr
5254
}
5355

5456
// Return the base http/https URL of the server, e.g. "http://127.0.0.1:8557".
5557
// The scheme is set to https if http.ssl is set to true in the configuration file.
5658
func (t *TestSuite) BaseUrl() string {
57-
if HttpSsl {
59+
if revel.HttpSsl {
5860
return "https://" + t.Host()
5961
} else {
6062
return "http://" + t.Host()
@@ -208,7 +210,7 @@ func (t *TestSuite) PostFileCustom(uri string, params url.Values, filePaths url.
208210
// examine the Response and ResponseBody properties. Session data will be
209211
// added to the request cookies for you.
210212
func (r *TestRequest) Send() {
211-
r.AddCookie(r.testSuite.Session.cookie())
213+
r.AddCookie(r.testSuite.Session.Cookie())
212214
r.MakeRequest()
213215
}
214216

@@ -225,10 +227,10 @@ func (r *TestRequest) MakeRequest() {
225227
}
226228

227229
// Look for a session cookie in the response and parse it.
228-
sessionCookieName := r.testSuite.Session.cookie().Name
230+
sessionCookieName := r.testSuite.Session.Cookie().Name
229231
for _, cookie := range r.testSuite.Client.Jar.Cookies(r.Request.URL) {
230232
if cookie.Name == sessionCookieName {
231-
r.testSuite.Session = getSessionFromCookie(cookie)
233+
r.testSuite.Session = revel.GetSessionFromCookie(cookie)
232234
break
233235
}
234236
}
@@ -271,13 +273,13 @@ func (t *TestSuite) AssertHeader(name, value string) {
271273
}
272274

273275
func (t *TestSuite) AssertEqual(expected, actual interface{}) {
274-
if !Equal(expected, actual) {
276+
if !revel.Equal(expected, actual) {
275277
panic(fmt.Errorf("(expected) %v != %v (actual)", expected, actual))
276278
}
277279
}
278280

279281
func (t *TestSuite) AssertNotEqual(expected, actual interface{}) {
280-
if Equal(expected, actual) {
282+
if revel.Equal(expected, actual) {
281283
panic(fmt.Errorf("(expected) %v == %v (actual)", expected, actual))
282284
}
283285
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy