-
Notifications
You must be signed in to change notification settings - Fork 0
/
forest-test_test.go
197 lines (180 loc) · 5.34 KB
/
forest-test_test.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2015 Afshin Darian. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
/*
Package forest_test contains tests and examples for package forest. The goal is
100% code coverage.
*/
package forest_test
import (
"encoding/json"
"github.com/ursiform/bear"
"github.com/ursiform/forest"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
)
const root = "/test"
type requested struct {
method string
path string
}
type wanted struct {
code int
success bool
data interface{}
}
func makeRequest(t *testing.T, app *forest.App,
params *requested, want *wanted) *http.Response {
var request *http.Request
method := params.method
path := params.path
request, _ = http.NewRequest(method, path, nil)
response := httptest.NewRecorder()
app.ServeHTTP(response, request)
responseData := new(forest.Response)
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Error(err)
return nil
}
if err := json.Unmarshal(responseBody, responseData); err != nil {
t.Error(err)
return nil
}
if response.Code != want.code {
t.Errorf("%s %s want: %d (%s) got: %d %s, body: %s",
method, path, want.code, http.StatusText(want.code), response.Code,
http.StatusText(response.Code), string(responseBody))
return nil
}
if responseData.Success != want.success {
t.Errorf("%s %s should return success: %t", method, path, want.success)
return nil
}
return &http.Response{Header: response.Header()}
}
func TestBasicOperation(t *testing.T) {
path := root
app := forest.New(forest.ConfigFile)
app.Config.Debug = true
app.RegisterRoute(path, newRouter(app))
params := &requested{method: "GET", path: path}
want := &wanted{code: http.StatusOK, success: true}
makeRequest(t, app, params, want)
}
func TestCookiesAndHeaders(t *testing.T) {
cookieName := "foo" // also in setCookie function of router
cookieValue := "bar" // also in setCookie function of router
path := root
app := forest.New(forest.ConfigFile)
app.Config.Debug = true
app.RegisterRoute(path, newRouter(app))
app.Config.PoweredBy = "Testing-FTW"
params := &requested{method: "GET", path: path}
want := &wanted{code: http.StatusOK, success: true}
response := makeRequest(t, app, params, want)
if response == nil {
return
}
if response.Header.Get("X-Powered-By") != app.Config.PoweredBy {
t.Errorf(
"app.Config.PoweredBy header did not match response header: %s",
response.Header.Get("X-Powered-By"))
}
for _, cookie := range response.Cookies() {
if cookie.Name == cookieName && cookie.Value == cookieValue {
return
}
}
t.Errorf("cookie was not found")
}
func TestInstallWare(t *testing.T) {
app := forest.New(forest.ConfigFile)
handlerName := "TestHandler"
message := "test handler installed"
var handlerNil func(ctx *bear.Context)
if err := app.InstallWare(handlerName, handlerNil, message); err == nil {
t.Errorf("app.InstallWare should reject nil handlers")
}
handler := func(*bear.Context) {}
if err := app.InstallWare(handlerName, handler, message); err != nil {
t.Errorf("app.InstallWare failed: %s", err.Error())
}
// test duplicate ware installation
if err := app.InstallWare(handlerName, handler, message); err != nil {
t.Errorf("app.InstallWare failed: %s", err.Error())
}
}
func TestNonExistentWare(t *testing.T) {
path := root + "/nonexistent"
app := forest.New(forest.ConfigFile)
app.RegisterRoute(root, newRouter(app))
params := &requested{method: "GET", path: path}
want := &wanted{code: http.StatusInternalServerError, success: false}
makeRequest(t, app, params, want)
}
func TestRetrievalDuration(t *testing.T) {
durFoo := time.Hour * 1
app := forest.New(forest.ConfigFile)
app.SetDuration("Foo", durFoo)
if app.Duration("Foo") != durFoo {
t.Errorf("SetDuration failed, want: %s got: %s",
durFoo, app.Duration("Foo"))
}
}
func TestRetrievalError(t *testing.T) {
errFoo := "FOO_ERROR"
app := forest.New(forest.ConfigFile)
app.SetError("Foo", errFoo)
if app.Error("Foo") != errFoo {
t.Errorf("SetError failed, want: %v got: %v", errFoo, app.Error("Foo"))
}
}
func TestRetrievalMessage(t *testing.T) {
msgFoo := "FOO_Message"
app := forest.New(forest.ConfigFile)
app.SetMessage("Foo", msgFoo)
if app.Message("Foo") != msgFoo {
t.Errorf("SetMessage failed, want: %s got: %s",
msgFoo, app.Message("Foo"))
}
}
func TestServeSuccess(t *testing.T) {
path := root
app := forest.New(forest.ConfigFile)
app.RegisterRoute(path, newRouter(app))
go func() {
app.Config.Address = ":0"
if err := app.ListenAndServe(); err != nil {
t.Errorf("app.ListenAndServe failed, %s", err.Error())
}
}()
}
func TestTLSServeSuccess(t *testing.T) {
path := root
app := forest.New(forest.ConfigFile)
app.RegisterRoute(path, newRouter(app))
go func() {
app.Config.Address = ":0"
certFile := "test-data/cert.pem"
keyFile := "test-data/key.pem"
if err := app.ListenAndServeTLS(certFile, keyFile); err != nil {
t.Errorf("app.ListenAndServeTLS failed, %s", err.Error())
}
}()
}
func TestEmptyConfigFile(t *testing.T) {
app := forest.New("")
if app.Config.LogLevelName != "listen" {
t.Errorf("app.Config.LogLevelName should be reset to listen if nonexistent")
}
}
func TestBadLogLevel(t *testing.T) {
app := forest.New("test-data/forest.json")
if app.Config.LogLevelName != "debug" {
t.Errorf("app.Config.LogLevelName should be reset to debug if bogus")
}
}