forked from yookoala/gofast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_test.go
291 lines (257 loc) · 7.87 KB
/
php_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package php_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"strings"
"testing"
"github.com/go-restit/lzjson"
"github.com/yookoala/gofast/example/php"
"github.com/yookoala/gofast/tools/phpfpm"
)
var username, phpfpmPath string
func init() {
var err error
// defined in environment
if phpfpmPath = os.Getenv("TEST_PHPFPM_PATH"); phpfpmPath != "" {
// do nothing
} else if phpfpmPath, err = phpfpm.FindBinary(phpfpm.ReadPaths(os.Getenv("PATH"))...); err != nil {
panic(err)
}
username = os.Getenv("USER")
}
func checkError(t *testing.T, cb func() error) {
if err := cb(); err != nil {
t.Errorf("Unexpected error: %s", err)
}
}
func isExamplePath(testPath string) bool {
if _, err := os.Stat(testPath); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(path.Join(testPath, "var")); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(path.Join(testPath, "etc")); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(path.Join(testPath, "htdocs")); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(path.Join(testPath, "htdocs", "index.php")); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(path.Join(testPath, "htdocs", "form.php")); os.IsNotExist(err) {
return false
}
return true
}
func examplePath() string {
basePath, err := os.Getwd()
if err != nil {
panic(err)
}
if isExamplePath(basePath) {
return basePath
}
basePath = path.Join(basePath, "example", "phpfpm")
if isExamplePath(basePath) {
return basePath
}
panic("example path not found")
}
func get(h http.Handler, path string) (w *httptest.ResponseRecorder, err error) {
r, err := http.NewRequest("GET", path, nil)
if err != nil {
return
}
w = httptest.NewRecorder()
h.ServeHTTP(w, r)
return
}
func post(h http.Handler, path string, payload string) (w *httptest.ResponseRecorder, err error) {
var reader io.Reader = strings.NewReader(payload)
r, err := http.NewRequest("POST", path, reader)
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", fmt.Sprintf("%d", len(payload)))
if err != nil {
return
}
w = httptest.NewRecorder()
h.ServeHTTP(w, r)
return
}
func initEnv(t *testing.T, name string, worker int) (exmpPath string, process *phpfpm.Process) {
if phpfpmPath == "" {
t.Skip("empty TEST_PHPFPM_PATH, skip test")
}
if stat, err := os.Stat(phpfpmPath); os.IsNotExist(err) {
t.Errorf("TEST_PHPFPM_PATH (%#v) not found", phpfpmPath)
return
} else if fmode := stat.Mode(); !fmode.IsRegular() {
t.Errorf("TEST_PHPFPM_PATH (%#v) is not a regular file", phpfpmPath)
return
}
exmpPath = examplePath()
process = phpfpm.NewProcess(phpfpmPath)
process.SetName(name)
process.SetWorker(worker)
process.SetDatadir(path.Join(exmpPath, "var"))
process.User = username
process.SaveConfig(path.Join(exmpPath, "etc", "test.handler.conf"))
if err := process.Start(); err != nil {
t.Errorf("unexpected error: %s", err.Error())
return
}
return
}
func TestNewSimpleHandler(t *testing.T) {
exmpPath, process := initEnv(t, "phpfpm1", 10)
defer checkError(t, process.Stop)
// start the proxy handler
network, address := process.Address()
h := php.NewSimpleHandler(
path.Join(exmpPath, "htdocs"),
network, address)
// check results
w, err := get(h, "/")
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
if want, have := "hello index", w.Body.String(); want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
if want, have := "World", w.Header().Get("X-Hello"); want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
if want, have := "Bar", w.Header().Get("X-Foo"); want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
w, err = get(h, "/index.php")
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
if want, have := "hello index", w.Body.String(); want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
w, err = get(h, "/form.php")
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
formPrefix := "<!DOCTYPE html>\n<html>\n<head>\n <title>Simple Form"
if have := w.Body.String(); !strings.HasPrefix(have, formPrefix) {
t.Errorf("expected to start with %#v, got %#v", formPrefix, have)
}
w, err = get(h, "/form.php?hello=world")
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
if want, have := "$_GET = array (\n 'hello' => 'world',\n)", w.Body.String(); want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
form := url.Values{}
form.Add("text_input", "hello world")
w, err = post(h, "/form.php", form.Encode())
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
if want, have := "$_POST = array (\n 'text_input' => 'hello world',\n)", w.Body.String(); want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
}
func TestNewSimpleHandler__ErrorStream(t *testing.T) {
exmpPath, process := initEnv(t, "phpfpm2", 1) // 1 worker to test regression
defer checkError(t, process.Stop)
// start the proxy handler
network, address := process.Address()
h := php.NewSimpleHandler(
path.Join(exmpPath, "htdocs"),
network, address)
// check results
w, err := get(h, "/error.php")
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
if want, have := "1. some standard output.\n3. some more standard output.\n5. unparsed.\n", w.Body.String(); want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
// check results
w, err = get(h, "/error.php?error_only=1")
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
if want, have := "", w.Body.String(); want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
}
func TestNewFileEndpointHandler(t *testing.T) {
exmpPath, process := initEnv(t, "phpfpm3", 10)
defer checkError(t, process.Stop)
// start the proxy handler
var w *httptest.ResponseRecorder
var err error
var resp lzjson.Node
network, address := process.Address()
h := php.NewFileEndpointHandler(
path.Join(exmpPath, "htdocs", "vars.php"),
network, address)
// check results for a proper path
w, err = get(h, "/")
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
resp = lzjson.Decode(w.Body)
if resp.Get("$_SERVER").Type() == lzjson.TypeUndefined {
t.Errorf("$_SERVER not set in response json")
t.Logf("Response JSON: %s", resp.Raw())
} else if resp.Get("$_SERVER").Get("REQUEST_URI").Type() == lzjson.TypeUndefined {
t.Errorf("$_SERVER.REQUEST_URI not set in response json")
t.Logf("Response JSON: %s", resp.Raw())
} else if want, have := "/", resp.Get("$_SERVER").Get("REQUEST_URI").String(); want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
// check results for a proper path
w, err = get(h, "/hello/world")
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
resp = lzjson.Decode(w.Body)
if resp.Get("$_SERVER").Type() == lzjson.TypeUndefined {
t.Errorf("$_SERVER not set in response json")
t.Logf("Response JSON: %s", resp.Raw())
} else if resp.Get("$_SERVER").Get("REQUEST_URI").Type() == lzjson.TypeUndefined {
t.Errorf("$_SERVER.REQUEST_URI not set in response json")
t.Logf("Response JSON: %s", resp.Raw())
} else if want, have := "/hello/world", resp.Get("$_SERVER").Get("REQUEST_URI").String(); want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
// check results for a proper path
w, err = get(h, "/index.php")
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
resp = lzjson.Decode(w.Body)
if resp.Get("$_SERVER").Type() == lzjson.TypeUndefined {
t.Errorf("$_SERVER not set in response json")
t.Logf("Response JSON: %s", resp.Raw())
} else if resp.Get("$_SERVER").Get("REQUEST_URI").Type() == lzjson.TypeUndefined {
t.Errorf("$_SERVER.REQUEST_URI not set in response json")
t.Logf("Response JSON: %s", resp.Raw())
} else if want, have := "/index.php", resp.Get("$_SERVER").Get("REQUEST_URI").String(); want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
}