@@ -2,7 +2,6 @@ package revel
2
2
3
3
import (
4
4
"bytes"
5
- "code.google.com/p/go.net/websocket"
6
5
"fmt"
7
6
"io"
8
7
"io/ioutil"
@@ -12,6 +11,8 @@ import (
12
11
"net/url"
13
12
"regexp"
14
13
"strings"
14
+
15
+ "code.google.com/p/go.net/websocket"
15
16
)
16
17
17
18
type TestSuite struct {
@@ -21,6 +22,11 @@ type TestSuite struct {
21
22
Session Session
22
23
}
23
24
25
+ type TestRequest struct {
26
+ * http.Request
27
+ testSuite * TestSuite
28
+ }
29
+
24
30
var TestSuites []interface {} // Array of structs that embed TestSuite
25
31
26
32
// NewTestSuite returns an initialized TestSuite ready for use. It is invoked
@@ -59,38 +65,69 @@ func (t *TestSuite) WebSocketUrl() string {
59
65
// Issue a GET request to the given path and store the result in Request and
60
66
// RequestBody.
61
67
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 {
62
73
req , err := http .NewRequest ("GET" , t .BaseUrl ()+ path , nil )
63
74
if err != nil {
64
75
panic (err )
65
76
}
66
- t .MakeRequestSession (req )
77
+ return & TestRequest {
78
+ Request : req ,
79
+ testSuite : t ,
80
+ }
67
81
}
68
82
69
83
// Issue a DELETE request to the given path and store the result in Request and
70
84
// RequestBody.
71
85
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 {
72
91
req , err := http .NewRequest ("DELETE" , t .BaseUrl ()+ path , nil )
73
92
if err != nil {
74
93
panic (err )
75
94
}
76
- t .MakeRequestSession (req )
95
+ return & TestRequest {
96
+ Request : req ,
97
+ testSuite : t ,
98
+ }
77
99
}
78
100
79
101
// Issue a POST request to the given path, sending the given Content-Type and
80
102
// data, and store the result in Request and RequestBody. "data" may be nil.
81
103
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 {
82
110
req , err := http .NewRequest ("POST" , t .BaseUrl ()+ path , reader )
83
111
if err != nil {
84
112
panic (err )
85
113
}
86
114
req .Header .Set ("Content-Type" , contentType )
87
- t .MakeRequestSession (req )
115
+ return & TestRequest {
116
+ Request : req ,
117
+ testSuite : t ,
118
+ }
88
119
}
89
120
90
121
// Issue a POST request to the given path as a form post of the given key and
91
122
// values, and store the result in Request and RequestBody.
92
123
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 ()))
94
131
}
95
132
96
133
// 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[
110
147
}
111
148
req .Header .Set ("Content-Type" , w .FormDataContentType ())
112
149
113
- t .MakeRequest (req )
114
150
}
115
151
116
152
// Issue any request and read the response. If successful, the caller may
117
153
// examine the Response and ResponseBody properties. Session data will be
118
154
// 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 ()
122
158
}
123
159
124
160
// Issue any request and read the response. If successful, the caller may
125
161
// examine the Response and ResponseBody properties. You will need to
126
162
// manage session / cookie data manually
127
- func (t * TestSuite ) MakeRequest (req * http. Request ) {
163
+ func (r * TestRequest ) MakeRequest () {
128
164
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 {
130
166
panic (err )
131
167
}
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 {
133
169
panic (err )
134
170
}
135
171
136
172
// 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 ) {
139
175
if cookie .Name == sessionCookieName {
140
- t .Session = getSessionFromCookie (cookie )
176
+ r . testSuite .Session = getSessionFromCookie (cookie )
141
177
break
142
178
}
143
179
}
0 commit comments