forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.go
181 lines (156 loc) · 4.94 KB
/
tests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package revel
import (
"bytes"
"code.google.com/p/go.net/websocket"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strings"
)
type TestSuite struct {
Client *http.Client
Response *http.Response
ResponseBody []byte
Session Session
}
var TestSuites []interface{} // Array of structs that embed TestSuite
// NewTestSuite returns an initialized TestSuite ready for use. It is invoked
// by the test harness to initialize the embedded field in application tests.
func NewTestSuite() TestSuite {
jar, _ := cookiejar.New(nil)
return TestSuite{
Client: &http.Client{Jar: jar},
Session: make(Session),
}
}
// Return the address and port of the server, e.g. "127.0.0.1:8557"
func (t *TestSuite) Host() string {
if Server.Addr[0] == ':' {
return "127.0.0.1" + Server.Addr
}
return Server.Addr
}
// Return the base http/https URL of the server, e.g. "http://127.0.0.1:8557".
// The scheme is set to https if http.ssl is set to true in the configuration file.
func (t *TestSuite) BaseUrl() string {
if HttpSsl {
return "https://" + t.Host()
} else {
return "http://" + t.Host()
}
}
// Return the base websocket URL of the server, e.g. "ws://127.0.0.1:8557"
func (t *TestSuite) WebSocketUrl() string {
return "ws://" + t.Host()
}
// Issue a GET request to the given path and store the result in Request and
// RequestBody.
func (t *TestSuite) Get(path string) {
req, err := http.NewRequest("GET", t.BaseUrl()+path, nil)
if err != nil {
panic(err)
}
t.MakeRequestSession(req)
}
// Issue a POST request to the given path, sending the given Content-Type and
// data, and store the result in Request and RequestBody. "data" may be nil.
func (t *TestSuite) Post(path string, contentType string, reader io.Reader) {
req, err := http.NewRequest("POST", t.BaseUrl()+path, reader)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", contentType)
t.MakeRequestSession(req)
}
// Issue a POST request to the given path as a form post of the given key and
// values, and store the result in Request and RequestBody.
func (t *TestSuite) PostForm(path string, data url.Values) {
t.Post(path, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
}
// Issue any request and read the response. If successful, the caller may
// examine the Response and ResponseBody properties. Session data will be
// added to the request cookies for you.
func (t *TestSuite) MakeRequestSession(req *http.Request) {
req.AddCookie(t.Session.cookie())
t.MakeRequest(req)
}
// Issue any request and read the response. If successful, the caller may
// examine the Response and ResponseBody properties. You will need to
// manage session / cookie data manually
func (t *TestSuite) MakeRequest(req *http.Request) {
var err error
if t.Response, err = t.Client.Do(req); err != nil {
panic(err)
}
if t.ResponseBody, err = ioutil.ReadAll(t.Response.Body); err != nil {
panic(err)
}
// Look for a session cookie in the response and parse it.
sessionCookieName := t.Session.cookie().Name
for _, cookie := range t.Client.Jar.Cookies(req.URL) {
if cookie.Name == sessionCookieName {
t.Session = getSessionFromCookie(cookie)
break
}
}
}
// Create a websocket connection to the given path and return the connection
func (t *TestSuite) WebSocket(path string) *websocket.Conn {
origin := t.BaseUrl() + "/"
url := t.WebSocketUrl() + path
ws, err := websocket.Dial(url, "", origin)
if err != nil {
panic(err)
}
return ws
}
func (t *TestSuite) AssertOk() {
t.AssertStatus(http.StatusOK)
}
func (t *TestSuite) AssertNotFound() {
t.AssertStatus(http.StatusNotFound)
}
func (t *TestSuite) AssertStatus(status int) {
if t.Response.StatusCode != status {
panic(fmt.Errorf("Status: (expected) %d != %d (actual)", status, t.Response.StatusCode))
}
}
func (t *TestSuite) AssertContentType(contentType string) {
t.AssertHeader("Content-Type", contentType)
}
func (t *TestSuite) AssertHeader(name, value string) {
actual := t.Response.Header.Get(name)
if actual != value {
panic(fmt.Errorf("Header %s: (expected) %s != %s (actual)", name, value, actual))
}
}
func (t *TestSuite) AssertEqual(expected, actual interface{}) {
if !Equal(expected, actual) {
panic(fmt.Errorf("(expected) %v != %v (actual)", expected, actual))
}
}
func (t *TestSuite) Assert(exp bool) {
t.Assertf(exp, "Assertion failed")
}
func (t *TestSuite) Assertf(exp bool, formatStr string, args ...interface{}) {
if !exp {
panic(fmt.Errorf(formatStr, args...))
}
}
// Assert that the response contains the given string.
func (t *TestSuite) AssertContains(s string) {
if !bytes.Contains(t.ResponseBody, []byte(s)) {
panic(fmt.Errorf("Assertion failed. Expected response to contain %s", s))
}
}
// Assert that the response matches the given regular expression.BUG
func (t *TestSuite) AssertContainsRegex(regex string) {
r := regexp.MustCompile(regex)
if !r.Match(t.ResponseBody) {
panic(fmt.Errorf("Assertion failed. Expected response to match regexp %s", regex))
}
}