1
# vim:set ft= ts=4 sw=4 et fdm=marker:
3
use Test::Nginx::Socket;
5
#worker_connections(1014);
6
#master_process_enabled(1);
12
plan tests => repeat_each() * (blocks() * 2 + 1);
20
=== TEST 1: basic print
23
# NOTE: the newline escape sequence must be double-escaped, as nginx config
24
# parser will unescape first!
25
content_by_lua 'ngx.print("Hello, Lua!\\n")';
37
# NOTE: the newline escape sequence must be double-escaped, as nginx config
38
# parser will unescape first!
40
ngx.say("Hello, Lua!")
41
ngx.say("Yay! ", 123)';
51
=== TEST 3: no ngx.echo
54
content_by_lua 'ngx.echo("Hello, Lua!\\n")';
58
--- response_body_like: 500 Internal Server Error
66
# NOTE: the newline escape sequence must be double-escaped, as nginx config
67
# parser will unescape first!
68
content_by_lua 'v = ngx.var["request_uri"] ngx.print("request_uri: ", v, "\\n")';
73
request_uri: /lua?a=1&b=2
77
=== TEST 5: variable (file)
80
content_by_lua_file html/test.lua;
84
v = ngx.var["request_uri"]
85
ngx.print("request_uri: ", v, "\n")
89
request_uri: /lua?a=1&b=2
93
=== TEST 6: calc expression
96
content_by_lua_file html/calc.lua;
100
local function uri_unescape(uri)
101
local function convert(hex)
102
return string.char(tonumber("0x"..hex))
104
local s = string.gsub(uri, "%%([0-9a-fA-F][0-9a-fA-F])", convert)
108
local function eval_exp(str)
109
return loadstring("return "..str)()
112
local exp_str = ngx.var["arg_exp"]
113
-- print("exp: '", exp_str, "'\n")
115
status, res = pcall(uri_unescape, exp_str)
117
ngx.print("error: ", res, "\n")
120
status, res = pcall(eval_exp, res)
122
ngx.print("result: ", res, "\n")
124
ngx.print("error: ", res, "\n")
127
GET /lua?exp=1%2B2*math.sin(3)%2Fmath.exp(4)-math.sqrt(2)
129
result: -0.4090441561579
133
=== TEST 7: read $arg_xxx
136
content_by_lua 'who = ngx.var.arg_who
137
ngx.print("Hello, ", who, "!")';
141
--- response_body chomp
146
=== TEST 8: capture location
153
content_by_lua 'res = ngx.location.capture("/other"); ngx.print("status=", res.status, " "); ngx.print("body=", res.body)';
158
status=200 body=hello, world
162
=== TEST 9: capture non-existed location
165
content_by_lua 'res = ngx.location.capture("/other"); ngx.print("status=", res.status)';
169
--- response_body: status=404
173
=== TEST 10: invalid capture location (not as expected...)
176
content_by_lua 'res = ngx.location.capture("*(#*"); ngx.say("res=", res.status)';
185
=== TEST 11: nil is "nil"
188
content_by_lua 'ngx.print(nil)';
192
--- response_body_like: 500 Internal Server Error
197
=== TEST 12: bad argument type to ngx.location.capture
200
content_by_lua 'ngx.location.capture(nil)';
204
--- response_body_like: 500 Internal Server Error
209
=== TEST 13: capture location (default 0);
213
local num = tonumber(ngx.var.arg_num) or 0;
214
ngx.print("num is: ", num, "\\n");
217
res = ngx.location.capture("/recur?num="..tostring(num - 1));
218
ngx.print("status=", res.status, " ");
219
ngx.print("body=", res.body, "\\n");
233
=== TEST 14: capture location
237
local num = tonumber(ngx.var.arg_num) or 0;
238
ngx.print("num is: ", num, "\\n");
241
res = ngx.location.capture("/recur?num="..tostring(num - 1));
242
ngx.print("status=", res.status, " ");
243
ngx.print("body=", res.body);
253
status=200 body=num is: 2
254
status=200 body=num is: 1
255
status=200 body=num is: 0
260
=== TEST 15: setting nginx variables from within Lua
264
content_by_lua 'ngx.var.a = 32; ngx.say(ngx.var.a)';
276
=== TEST 16: nginx quote sql string 1
279
set $a 'hello\n\r\'"\\';
280
content_by_lua 'ngx.say(ngx.quote_sql_str(ngx.var.a))';
289
=== TEST 17: nginx quote sql string 2
292
set $a "hello\n\r'\"\\";
293
content_by_lua 'ngx.say(ngx.quote_sql_str(ngx.var.a))';
302
=== TEST 18: use dollar
306
local s = "hello 112";
307
ngx.say(string.find(s, "%d+$"))';
316
=== TEST 19: subrequests do not share variables of main requests by default
323
content_by_lua 'res = ngx.location.capture("/sub"); ngx.print(res.body)';
327
--- response_body eval: "\n"
331
=== TEST 20: subrequests can share variables of main requests
339
res = ngx.location.capture(
341
{ share_all_vars = true }
353
=== TEST 21: main requests use subrequests' variables
360
res = ngx.location.capture("/sub", { share_all_vars = true });
371
=== TEST 22: main requests do NOT use subrequests' variables
378
res = ngx.location.capture("/sub", { share_all_vars = false });
384
--- response_body_like eval: "\n"
388
=== TEST 23: capture location headers
391
default_type 'foo/bar';
397
res = ngx.location.capture("/other");
398
ngx.say("type: ", res.header["Content-Type"]);
408
=== TEST 24: capture location headers
411
default_type 'foo/bar';
413
ngx.header.Bar = "Bah";
419
res = ngx.location.capture("/other");
420
ngx.say("type: ", res.header["Content-Type"]);
421
ngx.say("Bar: ", res.header["Bar"]);
432
=== TEST 25: capture location headers
435
default_type 'foo/bar';
437
ngx.header.Bar = "Bah";
438
ngx.header.Bar = nil;
444
res = ngx.location.capture("/other");
445
ngx.say("type: ", res.header["Content-Type"]);
446
ngx.say("Bar: ", res.header["Bar"] or "nil");