forked from openresty/lua-nginx-module
/
001-set.t
196 lines (148 loc) · 3.19 KB
/
001-set.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
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib 'lib';
use Test::Nginx::Socket;
repeat_each(2);
#repeat_each(1);
plan tests => blocks() * repeat_each() * 2;
no_long_string();
run_tests();
__DATA__
=== TEST 1: simple set (integer)
--- config
location /lua {
set_by_lua $res "return 1+1";
echo $res;
}
--- request
GET /lua
--- response_body
2
=== TEST 2: simple set (string)
--- config
location /lua {
set_by_lua $res "return 'hello' .. 'world'";
echo $res;
}
--- request
GET /lua
--- response_body
helloworld
=== TEST 3: internal only
--- config
location /lua {
set_by_lua $res "function fib(n) if n > 2 then return fib(n-1)+fib(n-2) else return 1 end end return fib(10)";
echo $res;
}
--- request
GET /lua
--- response_body
55
=== TEST 4: internal script with argument
--- config
location /lua {
set_by_lua $res "return ngx.arg[1]+ngx.arg[2]" $arg_a $arg_b;
echo $res;
}
--- request
GET /lua?a=1&b=2
--- response_body
3
=== TEST 5: fib by arg
--- config
location /fib {
set_by_lua $res "function fib(n) if n > 2 then return fib(n-1)+fib(n-2) else return 1 end end return fib(tonumber(ngx.arg[1]))" $arg_n;
echo $res;
}
--- request
GET /fib?n=10
--- response_body
55
=== TEST 6: adder
--- config
location = /adder {
set_by_lua $res
"local a = tonumber(ngx.arg[1])
local b = tonumber(ngx.arg[2])
return a + b" $arg_a $arg_b;
echo $res;
}
--- request
GET /adder?a=25&b=75
--- response_body
100
=== TEST 7: read nginx variables directly from within Lua
--- config
location = /set-both {
set $b 32;
set_by_lua $a "return tonumber(ngx.var.b) + 1";
echo "a = $a";
}
--- request
GET /set-both
--- response_body
a = 33
=== TEST 8: set nginx variables directly from within Lua
--- config
location = /set-both {
set $b "";
set_by_lua $a "ngx.var.b = 32; return 7";
echo "a = $a";
echo "b = $b";
}
--- request
GET /set-both
--- response_body
a = 7
b = 32
=== TEST 9: set non-existent nginx variables
--- config
location = /set-both {
#set $b "";
set_by_lua $a "ngx.var.b = 32; return 7";
echo "a = $a";
}
--- request
GET /set-both
--- response_body_like: 500 Internal Server Error
--- error_code: 500
=== TEST 10: set quote sql str
--- config
location = /set {
set $a "";
set_by_lua $a "return ngx.quote_sql_str(ngx.var.a)";
echo $a;
}
--- request
GET /set
--- response_body
''
=== TEST 11: set md5
--- config
location = /md5 {
set_by_lua $a 'return ngx.md5("hello")';
echo $a;
}
--- request
GET /md5
--- response_body
5d41402abc4b2a76b9719d911017c592
=== TEST 12: no ngx.print
--- config
location /lua {
set_by_lua $res "ngx.print(32) return 1";
echo $res;
}
--- request
GET /lua
--- response_body_like: 500 Internal Server Error
--- error_code: 500
=== TEST 13: no ngx.say
--- config
location /lua {
set_by_lua $res "ngx.say(32) return 1";
echo $res;
}
--- request
GET /lua
--- response_body_like: 500 Internal Server Error
--- error_code: 500