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 c64f4e2

Browse filesBrowse files
author
anonx
committed
Methods for custom requests have been added
1 parent bd582ad commit c64f4e2
Copy full SHA for c64f4e2

File tree

Expand file treeCollapse file tree

1 file changed

+51
-15
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+51
-15
lines changed

‎tests.go

Copy file name to clipboardExpand all lines: tests.go
+51-15Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package revel
22

33
import (
44
"bytes"
5-
"code.google.com/p/go.net/websocket"
65
"fmt"
76
"io"
87
"io/ioutil"
@@ -12,6 +11,8 @@ import (
1211
"net/url"
1312
"regexp"
1413
"strings"
14+
15+
"code.google.com/p/go.net/websocket"
1516
)
1617

1718
type TestSuite struct {
@@ -21,6 +22,11 @@ type TestSuite struct {
2122
Session Session
2223
}
2324

25+
type TestRequest struct {
26+
*http.Request
27+
testSuite *TestSuite
28+
}
29+
2430
var TestSuites []interface{} // Array of structs that embed TestSuite
2531

2632
// NewTestSuite returns an initialized TestSuite ready for use. It is invoked
@@ -59,38 +65,69 @@ func (t *TestSuite) WebSocketUrl() string {
5965
// Issue a GET request to the given path and store the result in Request and
6066
// RequestBody.
6167
func (t *TestSuite) Get(path string) {
68+
t.GetCustom(path).Send()
69+
}
70+
71+
// Return a GET request to the given path in a form of its wrapper.
72+
func (t *TestSuite) GetCustom(path string) *TestRequest {
6273
req, err := http.NewRequest("GET", t.BaseUrl()+path, nil)
6374
if err != nil {
6475
panic(err)
6576
}
66-
t.MakeRequestSession(req)
77+
return &TestRequest{
78+
Request: req,
79+
testSuite: t,
80+
}
6781
}
6882

6983
// Issue a DELETE request to the given path and store the result in Request and
7084
// RequestBody.
7185
func (t *TestSuite) Delete(path string) {
86+
t.DeleteCustom(path).Send()
87+
}
88+
89+
// Return a DELETE request to the given path in a form of its wrapper.
90+
func (t *TestSuite) DeleteCustom(path string) *TestRequest {
7291
req, err := http.NewRequest("DELETE", t.BaseUrl()+path, nil)
7392
if err != nil {
7493
panic(err)
7594
}
76-
t.MakeRequestSession(req)
95+
return &TestRequest{
96+
Request: req,
97+
testSuite: t,
98+
}
7799
}
78100

79101
// Issue a POST request to the given path, sending the given Content-Type and
80102
// data, and store the result in Request and RequestBody. "data" may be nil.
81103
func (t *TestSuite) Post(path string, contentType string, reader io.Reader) {
104+
t.PostCustom(path, contentType, reader).Send()
105+
}
106+
107+
// Return a POST request to the given path with specified Content-Type and data
108+
// in a form of wrapper. "data" may be nil.
109+
func (t *TestSuite) PostCustom(path string, contentType string, reader io.Reader) *TestRequest {
82110
req, err := http.NewRequest("POST", t.BaseUrl()+path, reader)
83111
if err != nil {
84112
panic(err)
85113
}
86114
req.Header.Set("Content-Type", contentType)
87-
t.MakeRequestSession(req)
115+
return &TestRequest{
116+
Request: req,
117+
testSuite: t,
118+
}
88119
}
89120

90121
// Issue a POST request to the given path as a form post of the given key and
91122
// values, and store the result in Request and RequestBody.
92123
func (t *TestSuite) PostForm(path string, data url.Values) {
93-
t.Post(path, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
124+
t.PostFormCustom(path, data).Send()
125+
}
126+
127+
// Return a POST request to the given path as a form post of the given key and values.
128+
// The request is a wrapper of type TestRequest.
129+
func (t *TestSuite) PostFormCustom(path string, data url.Values) *TestRequest {
130+
return t.PostCustom(path, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
94131
}
95132

96133
// Issue a multipart request for the method & fields given and read the response.
@@ -110,34 +147,33 @@ func (t *TestSuite) MakeMultipartRequest(method string, path string, fields map[
110147
}
111148
req.Header.Set("Content-Type", w.FormDataContentType())
112149

113-
t.MakeRequest(req)
114150
}
115151

116152
// Issue any request and read the response. If successful, the caller may
117153
// examine the Response and ResponseBody properties. Session data will be
118154
// added to the request cookies for you.
119-
func (t *TestSuite) MakeRequestSession(req *http.Request) {
120-
req.AddCookie(t.Session.cookie())
121-
t.MakeRequest(req)
155+
func (r *TestRequest) Send() {
156+
r.AddCookie(r.testSuite.Session.cookie())
157+
r.MakeRequest()
122158
}
123159

124160
// Issue any request and read the response. If successful, the caller may
125161
// examine the Response and ResponseBody properties. You will need to
126162
// manage session / cookie data manually
127-
func (t *TestSuite) MakeRequest(req *http.Request) {
163+
func (r *TestRequest) MakeRequest() {
128164
var err error
129-
if t.Response, err = t.Client.Do(req); err != nil {
165+
if r.testSuite.Response, err = r.testSuite.Client.Do(r.Request); err != nil {
130166
panic(err)
131167
}
132-
if t.ResponseBody, err = ioutil.ReadAll(t.Response.Body); err != nil {
168+
if r.testSuite.ResponseBody, err = ioutil.ReadAll(r.testSuite.Response.Body); err != nil {
133169
panic(err)
134170
}
135171

136172
// Look for a session cookie in the response and parse it.
137-
sessionCookieName := t.Session.cookie().Name
138-
for _, cookie := range t.Client.Jar.Cookies(req.URL) {
173+
sessionCookieName := r.testSuite.Session.cookie().Name
174+
for _, cookie := range r.testSuite.Client.Jar.Cookies(r.Request.URL) {
139175
if cookie.Name == sessionCookieName {
140-
t.Session = getSessionFromCookie(cookie)
176+
r.testSuite.Session = getSessionFromCookie(cookie)
141177
break
142178
}
143179
}

0 commit comments

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