-
Notifications
You must be signed in to change notification settings - Fork 193
/
002-content.t
248 lines (192 loc) · 4.78 KB
/
002-content.t
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
# vim:set ft= ts=4 sw=4 et fdm=marker:
use Test::Nginx::Socket::Lua::Stream;
#worker_connections(1014);
#master_on();
#workers(2);
#log_level('warn');
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3 + 3);
#no_diff();
#no_long_string();
run_tests();
__DATA__
=== TEST 1: basic print
--- stream_server_config
content_by_lua_block {
local ok, err = ngx.print("Hello, Lua!\n")
if not ok then
ngx.log(ngx.ERR, "print failed: ", err)
end
}
--- stream_response
Hello, Lua!
--- no_error_log
[error]
=== TEST 2: basic say
--- stream_server_config
content_by_lua_block {
local ok, err = ngx.say("Hello, Lua!")
if not ok then
ngx.log(ngx.ERR, "say failed: ", err)
return
end
local ok, err = ngx.say("Yay! ", 123)
if not ok then
ngx.log(ngx.ERR, "say failed: ", err)
return
end
}
--- stream_response
Hello, Lua!
Yay! 123
--- no_error_log
[error]
=== TEST 3: no ngx.echo
--- stream_server_config
content_by_lua_block { ngx.echo("Hello, Lua!\n") }
--- stream_response
--- error_log eval
qr/content_by_lua\(nginx\.conf:\d+\):1: attempt to call field 'echo' \(a nil value\)/
=== TEST 4: calc expression
--- stream_server_config
content_by_lua_file html/calc.lua;
--- user_files
>>> calc.lua
local function uri_unescape(uri)
local function convert(hex)
return string.char(tonumber("0x"..hex))
end
local s = string.gsub(uri, "%%([0-9a-fA-F][0-9a-fA-F])", convert)
return s
end
local function eval_exp(str)
return loadstring("return "..str)()
end
local exp_str = 1+2*math.sin(3)/math.exp(4)-math.sqrt(2)
-- print("exp: '", exp_str, "'\n")
local status, res
status, res = pcall(uri_unescape, exp_str)
if not status then
ngx.print("error: ", res, "\n")
return
end
status, res = pcall(eval_exp, res)
if status then
ngx.print("result: ", res, "\n")
else
ngx.print("error: ", res, "\n")
end
--- stream_response
result: -0.4090441561579
--- no_error_log
[error]
=== TEST 5: nil is "nil"
--- stream_server_config
content_by_lua_block { ngx.say(nil) }
--- stream_response
nil
--- no_error_log
[error]
=== TEST 6: write boolean
--- stream_server_config
content_by_lua_block { ngx.say(true, " ", false) }
--- stream_response
true false
--- no_error_log
[error]
=== TEST 7: nginx quote sql string 1
--- stream_server_config
content_by_lua_block { ngx.say(ngx.quote_sql_str('hello\n\r\'"\\')) }
--- stream_response
'hello\n\r\'\"\\'
--- no_error_log
[error]
=== TEST 8: nginx quote sql string 2
--- stream_server_config
content_by_lua_block { ngx.say(ngx.quote_sql_str("hello\n\r'\"\\")) }
--- stream_response
'hello\n\r\'\"\\'
--- no_error_log
[error]
=== TEST 9: multiple eof
--- stream_server_config
content_by_lua_block {
ngx.say("Hi")
local ok, err = ngx.eof()
if not ok then
ngx.log(ngx.WARN, "eof failed: ", err)
return
end
ok, err = ngx.eof()
if not ok then
ngx.log(ngx.WARN, "eof failed: ", err)
return
end
}
--- stream_response
Hi
--- no_error_log
[error]
--- error_log
lua send eof
eof failed: seen eof
=== TEST 10: ngx.eof before ngx.say
--- stream_server_config
content_by_lua_block {
local ok, err = ngx.eof()
if not ok then
ngx.log(ngx.ERR, "eof failed: ", err)
return
end
ok, err = ngx.say(ngx.headers_sent)
if not ok then
ngx.log(ngx.WARN, "failed to say: ", err)
return
end
}
--- stream_response
--- no_error_log
[error]
--- error_log
failed to say: seen eof
=== TEST 11: ngx.print table arguments (github issue #54)
--- stream_server_config
content_by_lua_block { ngx.print({10, {0, 5}, 15}, 32) }
--- stream_response chop
10051532
--- no_error_log
[error]
=== TEST 12: ngx.say table arguments (github issue #54)
--- stream_server_config
content_by_lua_block { ngx.say({10, {0, "5"}, 15}, 32) }
--- stream_response
10051532
--- no_error_log
[error]
=== TEST 13: Lua file does not exist
--- stream_server_config
content_by_lua_file html/test2.lua;
--- user_files
>>> test.lua
v = ngx.var["request_uri"]
ngx.print("request_uri: ", v, "\n")
--- stream_response
--- error_log eval
qr/failed to load external Lua file ".*?test2\.lua": cannot open .*? No such file or directory/
=== TEST 14: .lua file with shebang
--- stream_server_config
content_by_lua_file html/test.lua;
--- user_files
>>> test.lua
#!/bin/lua
ngx.say("line ", debug.getinfo(1).currentline)
--- stream_response
line 3
--- no_error_log
[error]
=== TEST 15: syntax error in inlined Lua code
--- stream_server_config
content_by_lua_block {for end}
--- stream_response
--- error_log eval
qr/failed to load inlined Lua code: /