Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6f4ee24

Browse filesBrowse files
author
anonx
committed
Reducing the number of http requests in persona sample tests
1 parent 7f2d187 commit 6f4ee24
Copy full SHA for 6f4ee24

File tree

Expand file treeCollapse file tree

1 file changed

+32
-7
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+32
-7
lines changed

‎samples/persona/tests/apptest.go

Copy file name to clipboardExpand all lines: samples/persona/tests/apptest.go
+32-7Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package tests
33
import (
44
"encoding/json"
55
"net/url"
6+
"time"
67

78
"github.com/revel/revel"
89
)
910

11+
var personaTestUsers map[string]PersonaTestUser
12+
1013
type AppTest struct {
1114
revel.TestSuite
1215
}
@@ -16,27 +19,28 @@ type PersonaTestUser struct {
1619
Audience string `json:"audience"`
1720
Email string `json:"email"`
1821
Pass string `json:"pass"`
22+
Received time.Time
1923
}
2024

2125
func (t *AppTest) TestThatLoginPageWorks() {
2226
// Make sure empty assertion will cause an error.
2327
t.PostForm("/login", url.Values{
24-
"assertion": []string{""},
28+
"assertion": {""},
2529
})
2630
t.AssertStatus(400)
2731

2832
// Ensure that incorrect audience parameter will lead to an error.
2933
user := t.EmailWithAssertion("https://example.com")
3034
t.PostForm("/login", url.Values{
31-
"assertion": []string{user.Assertion},
35+
"assertion": {user.Assertion},
3236
})
3337
t.AssertEqual(user.Audience, "https://example.com")
3438
t.AssertStatus(400)
3539

3640
// Check whether authentication works.
3741
user = t.EmailWithAssertion("http://" + revel.Config.StringDefault("http.host", "localhost"))
3842
t.PostForm("/login", url.Values{
39-
"assertion": []string{user.Assertion},
43+
"assertion": {user.Assertion},
4044
})
4145
t.AssertOk()
4246
t.AssertContains("Login successful")
@@ -50,7 +54,7 @@ func (t *AppTest) TestThatLogoutPageWorks() {
5054
// Authenticating a user.
5155
user := t.EmailWithAssertion("http://" + revel.Config.StringDefault("http.host", "localhost"))
5256
t.PostForm("/login", url.Values{
53-
"assertion": []string{user.Assertion},
57+
"assertion": {user.Assertion},
5458
})
5559
t.AssertOk()
5660
t.AssertContains("Login successful")
@@ -68,8 +72,18 @@ func (t *AppTest) TestThatLogoutPageWorks() {
6872
}
6973

7074
// EmailWithAssertion uses personatestuser.org service for getting testing parameters.
71-
// The testing service expects audience to begin with protocol, for example: "http://".
72-
func (t *AppTest) EmailWithAssertion(audience string) *PersonaTestUser {
75+
// The test persona service expects audience to begin with protocol, for example: "http://".
76+
func (t *AppTest) EmailWithAssertion(audience string) PersonaTestUser {
77+
// The process of getting new test users takes a lot of time. To reduce the number
78+
// of http requests using the same user data till they are up-to-date.
79+
if user, ok := personaTestUsers[audience]; ok {
80+
// Make sure user data are still valid.
81+
// Data expire after 2 minutes. We are updating them after 1 just in case.
82+
if !time.Now().After(user.Received.Add(time.Minute)) {
83+
return user
84+
}
85+
}
86+
7387
// Trying to get data from testing server.
7488
u := "http://personatestuser.org"
7589
urn := "/email_with_assertion/" + url.QueryEscape(audience)
@@ -87,5 +101,16 @@ func (t *AppTest) EmailWithAssertion(audience string) *PersonaTestUser {
87101
err := json.Unmarshal(t.ResponseBody, &user)
88102
t.Assert(err == nil)
89103

90-
return &user
104+
// Register the time when new user data are received. We are not using "Expire"
105+
// parameter from persona test server because then we'll have to synchronise our clock.
106+
user.Received = time.Now()
107+
108+
// Cache the user data.
109+
personaTestUsers[audience] = user
110+
111+
return user
112+
}
113+
114+
func init() {
115+
personaTestUsers = map[string]PersonaTestUser{}
91116
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.