Repository navigation
Expand file tree
/
Copy pathserver_test.go
More file actions
243 lines (192 loc) · 8.78 KB
/
Copy pathserver_test.go
File metadata and controls
243 lines (192 loc) · 8.78 KB
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
package frankenphp_test
import (
"io"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/dunglas/frankenphp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func initServers(t *testing.T, opts ...frankenphp.Option) {
t.Helper()
t.Cleanup(frankenphp.Shutdown)
require.NoError(t, frankenphp.Init(opts...))
}
func serverRequest(t *testing.T, server *frankenphp.Server, req *http.Request) (string, *http.Response) {
t.Helper()
w := httptest.NewRecorder()
require.NoError(t, server.ServeHTTP(w, req))
resp := w.Result()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
return string(body), resp
}
func serverGet(t *testing.T, server *frankenphp.Server, url string) string {
t.Helper()
body, _ := serverRequest(t, server, httptest.NewRequest(http.MethodGet, url, nil))
return body
}
func TestServer(t *testing.T) {
t.Run("idx", func(t *testing.T) {
server1, _ := frankenphp.NewServer(testDataDir, frankenphp.WithServerEnv(map[string]string{"PHP_SERVER_IDX_1": "1"}))
server2, _ := frankenphp.NewServer(testDataDir, frankenphp.WithServerEnv(map[string]string{"PHP_SERVER_IDX_2": "2"}))
initServers(t, frankenphp.WithServer(server1), frankenphp.WithServer(server2))
body1 := serverGet(t, server1, "http://example.com/server-variable.php")
body2 := serverGet(t, server2, "http://example.com/server-variable.php")
assert.Contains(t, body1, "[PHP_SERVER_IDX_1] => 1")
assert.Contains(t, body2, "[PHP_SERVER_IDX_2] => 2")
assert.NotContains(t, body1, "[PHP_SERVER_IDX_2]")
assert.NotContains(t, body2, "[PHP_SERVER_IDX_1]")
})
t.Run("name", func(t *testing.T) {
named, _ := frankenphp.NewServer(testDataDir, frankenphp.WithServerName("api"))
unnamed, _ := frankenphp.NewServer(testDataDir)
initServers(t, frankenphp.WithServer(named), frankenphp.WithServer(unnamed))
assert.Equal(t, "api", named.Name())
// an empty name defaults to the server index at registration
assert.Equal(t, "server_1", unnamed.Name())
})
t.Run("root", func(t *testing.T) {
server, _ := frankenphp.NewServer(testDataDir)
initServers(t, frankenphp.WithServer(server))
body := serverGet(t, server, "http://example.com/server-globals.php")
expectedRoot := filepath.Clean(strings.TrimSuffix(testDataDir, string(filepath.Separator)))
assert.Contains(t, body, "DOCUMENT_ROOT: "+expectedRoot+"\n")
})
t.Run("env", func(t *testing.T) {
server, _ := frankenphp.NewServer(testDataDir, frankenphp.WithServerEnv(map[string]string{"TEST_123": "123"}))
initServers(t, frankenphp.WithServer(server))
body := serverGet(t, server, "http://example.com/server-variable.php")
assert.Contains(t, body, "[TEST_123] => 123")
})
t.Run("split_path", func(t *testing.T) {
server, _ := frankenphp.NewServer(testDataDir, frankenphp.WithServerSplitPath([]string{".custom"}))
initServers(t, frankenphp.WithServer(server))
body := serverGet(t, server, "http://example.com/split-path.custom/pathinfo")
assert.Contains(t, body, "PATH_INFO: /pathinfo\n")
assert.Contains(t, body, "SCRIPT_NAME: /split-path.custom\n")
assert.Contains(t, body, "PHP_SELF: /split-path.custom/pathinfo\n")
})
t.Run("split_path_no_match", func(t *testing.T) {
server, _ := frankenphp.NewServer(testDataDir, frankenphp.WithServerSplitPath([]string{".inc"}))
initServers(t, frankenphp.WithServer(server))
w := httptest.NewRecorder()
require.ErrorIs(t, server.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "http://example.com/hello.%C4%B0nc", nil)), frankenphp.ErrInvalidScriptPath)
assert.Equal(t, http.StatusNotFound, w.Code)
assert.Equal(t, "invalid script path", w.Body.String())
w = httptest.NewRecorder()
require.ErrorIs(t, server.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "http://example.com/hello.txt", nil)), frankenphp.ErrInvalidScriptPath)
assert.Equal(t, http.StatusNotFound, w.Code)
})
t.Run("workers_by_path_and_request_matcher", func(t *testing.T) {
server1, _ := frankenphp.NewServer(testDataDir)
server2, _ := frankenphp.NewServer(testDataDir)
initServers(
t,
frankenphp.WithPhpIni(map[string]string{"display_errors": "1"}),
frankenphp.WithServer(server1),
frankenphp.WithServer(server2),
frankenphp.WithWorkers("counter", testDataDir+"worker-with-counter.php", 1, frankenphp.WithWorkerServerScope(server1)),
frankenphp.WithWorkers("match", testDataDir+"worker-with-counter.php", 1,
frankenphp.WithWorkerServerScope(server2),
frankenphp.WithWorkerMatcher(func(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, "/match/")
}),
),
)
body1 := serverGet(t, server1, "http://example.com/worker-with-counter.php")
body2 := serverGet(t, server1, "http://example.com/worker-with-counter.php")
body3 := serverGet(t, server2, "http://example.com/match/anything")
body4 := serverGet(t, server2, "http://example.com/match/anything")
body5 := serverGet(t, server2, "http://example.com/index.php")
w := httptest.NewRecorder()
err6 := server1.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "http://example.com/match/anything", nil))
assert.Equal(t, "requests:1", body1, "could not access the worker by path on server 1")
assert.Equal(t, "requests:2", body2, "could not access the worker by path on server 1")
assert.Equal(t, "requests:1", body3, "could not access the worker by matcher on server 2")
assert.Equal(t, "requests:2", body4, "could not access the worker by matcher on server 2")
assert.Contains(t, body5, "I am by birth a Genevese (i not set)", "could not access the worker by path on server 2")
assert.ErrorIs(t, err6, frankenphp.ErrInvalidScriptPath, "the matcher worker is scoped to server 2, so server 1 has no script for this path")
})
t.Run("worker_env_inheritance", func(t *testing.T) {
server, _ := frankenphp.NewServer(testDataDir, frankenphp.WithServerEnv(map[string]string{
"FROM_SERVER_ENV": "original",
"FROM_WORKER_ENV": "overridden",
}))
initServers(
t,
frankenphp.WithPhpIni(map[string]string{"variables_order": "EGPCS"}),
frankenphp.WithServer(server),
frankenphp.WithWorkers(
"env",
testDataDir+"env/env.php",
1,
frankenphp.WithWorkerServerScope(server),
frankenphp.WithWorkerEnv(map[string]string{
"FROM_WORKER_ENV": "original",
}),
),
)
body := serverGet(t, server, "http://example.com/env/env.php?keys[]=FROM_SERVER_ENV&keys[]=FROM_WORKER_ENV")
assert.Equal(
t,
"FROM_SERVER_ENV=original,FROM_WORKER_ENV=original",
body,
"should contain the server env and not override the worker env",
)
})
t.Run("error_on_duplicate_worker_filenames", func(t *testing.T) {
t.Cleanup(frankenphp.Shutdown)
server, _ := frankenphp.NewServer(testDataDir)
err := frankenphp.Init(
frankenphp.WithServer(server),
frankenphp.WithWorkers("worker1", testDataDir+"worker-with-counter.php", 1, frankenphp.WithWorkerServerScope(server)),
frankenphp.WithWorkers("worker2", testDataDir+"worker-with-counter.php", 1, frankenphp.WithWorkerServerScope(server)),
)
assert.Error(t, err)
assert.Contains(t, err.Error(), "two workers in a server cannot have the same filename")
})
t.Run("error_on_request_matcher_without_server_scope", func(t *testing.T) {
t.Cleanup(frankenphp.Shutdown)
err := frankenphp.Init(
frankenphp.WithWorkers("global", testDataDir+"worker-with-counter.php", 1,
frankenphp.WithWorkerMatcher(func(*http.Request) bool { return true }),
),
)
assert.Error(t, err)
assert.Contains(t, err.Error(), "no server scope")
})
// re-registering a Server must not trip the duplicate filename check on its own workers
t.Run("reregistration_after_shutdown", func(t *testing.T) {
server, err := frankenphp.NewServer(testDataDir)
require.NoError(t, err)
opts := []frankenphp.Option{
frankenphp.WithServer(server),
frankenphp.WithWorkers("counter", testDataDir+"worker-with-counter.php", 1,
frankenphp.WithWorkerServerScope(server),
),
}
initServers(t, opts...)
assert.Equal(t, "requests:1", serverGet(t, server, "http://example.com/worker-with-counter.php"))
assert.Equal(t, "server_0", server.Name())
frankenphp.Shutdown()
initServers(t, opts...)
assert.Equal(t, "requests:1", serverGet(t, server, "http://example.com/worker-with-counter.php"))
assert.Equal(t, "server_0", server.Name())
})
t.Run("error_on_missing_registration", func(t *testing.T) {
server, _ := frankenphp.NewServer(testDataDir)
assert.ErrorIs(t, server.ServeHTTP(nil, nil), frankenphp.ErrNotRunning)
})
t.Run("server_logger", func(t *testing.T) {
logger, buf := newTestLogger(t)
server, _ := frankenphp.NewServer(testDataDir, frankenphp.WithServerLogger(logger))
initServers(t, frankenphp.WithServer(server))
_ = serverGet(t, server, "http://example.com/log-frankenphp_log.php")
_ = serverGet(t, server, "http://example.com/log-frankenphp_log.php")
assert.Contains(t, buf.String(), "some error message")
})
}