forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.go
112 lines (95 loc) · 2.95 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
package revel
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type TestSuite struct {
Client *http.Client
Response *http.Response
ResponseBody []byte
}
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 {
return TestSuite{Client: &http.Client{}}
}
// Return the base URL of the server, e.g. "http://127.0.0.1:8557"
func (t *TestSuite) BaseUrl() string {
if Server.Addr[0] == ':' {
return "http://127.0.0.1" + Server.Addr
}
return "http://" + Server.Addr
}
// 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.MakeRequest(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.MakeRequest(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.
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)
}
}
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))
}
}