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
|
use lib '.';
use t::TestCore;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 5 + 4);
add_block_preprocessor(sub {
my $block = shift;
my $http_config = $block->http_config || '';
$http_config .= <<_EOC_;
$t::TestCore::HttpConfig
_EOC_
$block->set_value("http_config", $http_config);
if (!defined $block->error_log) {
my $no_error_log = <<_EOC_;
[error]
[alert]
[emerg]
-- NYI:
stitch
_EOC_
$block->set_value("no_error_log", $no_error_log);
}
if (!defined $block->request) {
$block->set_value("request", "GET /t");
}
});
check_accum_error_log();
run_tests();
__DATA__
=== TEST 1: sanity
--- config
location /t {
content_by_lua_block {
local s = ndk.set_var.set_escape_uri(" :")
local r = ndk.set_var.set_unescape_uri("a%20b")
ngx.say(s)
ngx.say(r)
local set_escape_uri = ndk.set_var.set_escape_uri
local set_unescape_uri = ndk.set_var.set_unescape_uri
ngx.say(set_escape_uri(" :"))
ngx.say(set_unescape_uri("a%20b"))
local res
for i = 1, $TEST_NGINX_HOTLOOP * 10 do
res = set_escape_uri(" :")
end
for i = 1, $TEST_NGINX_HOTLOOP * 10 do
res = set_unescape_uri("a%20b")
end
}
}
--- error_log eval
qr/\[TRACE\s+\d+ content_by_lua\(nginx\.conf:\d+\):13 loop\]/,
qr/\[TRACE\s+\d+ content_by_lua\(nginx\.conf:\d+\):17 loop\]/
--- no_error_log
[error]
[alert]
[emerg]
-- NYI:
stitch
--- response_body
%20%3A
a b
%20%3A
a b
=== TEST 2: directive not found
--- config
location /t {
content_by_lua_block {
local s = ndk.set_var.set_escape_uri_blah_blah(" :")
ngx.say(s)
}
}
--- response_body_like: 500 Internal Server Error
--- error_code: 500
--- error_log
ndk.set_var: directive "set_escape_uri_blah_blah" not found or does not use ndk_set_var_value
=== TEST 3: ndk.set_var initialize ngx_http_variable_value_t variable properly
--- config
location /t {
content_by_lua_block {
local version = '2011.10.13+0000'
local e_version = ndk.set_var.set_encode_base32(version)
local s_version= ndk.set_var.set_quote_sql_str(version)
ngx.say(e_version)
ngx.say(s_version)
}
}
--- response_body
68o32c9e64o2sc9j5co30c1g
'2011.10.13+0000'
=== TEST 4: set directive not allowed
--- config
location /t {
content_by_lua_block {
ndk.set_var.set_escape_uri = "hack it"
}
}
--- response_body_like: 500 Internal Server Error
--- error_code: 500
--- error_log
not allowed
=== TEST 5: call directive failed
--- config
location /t {
content_by_lua_block {
ndk.set_var.set_decode_hex('a')
}
}
--- response_body_like: 500 Internal Server Error
--- error_code: 500
--- error_log
calling directive set_decode_hex failed with code -1
=== TEST 6: convert directive type to string
--- config
location /t {
content_by_lua_block {
ndk.set_var[1]()
}
}
--- response_body_like: 500 Internal Server Error
--- error_code: 500
--- error_log
ndk.set_var: directive "1" not found or does not use ndk_set_var_value
=== TEST 7: convert directive argument to string
--- config
location /t {
content_by_lua_block {
local s = ndk.set_var.set_escape_uri(1)
ngx.say(s)
}
}
--- response_body
1
=== TEST 8: call in set_by_lua
--- config
location /t {
set_by_lua_block $s {
return ndk.set_var.set_escape_uri(" :")
}
echo $s;
}
--- response_body
%20%3A
=== TEST 9: call in timer
--- config
location /t {
content_by_lua_block {
ngx.timer.at(0, function()
local s = ndk.set_var.set_escape_uri(" :")
ngx.log(ngx.WARN, "s = ", s)
end)
ngx.sleep(0.01)
}
}
--- error_log
s = %20%3A
--- no_error_log
[error]
=== TEST 10: call in header_filter_by_lua
--- config
location /t {
content_by_lua_block {
ngx.send_headers()
ngx.say(package.loaded.s)
}
header_filter_by_lua_block {
package.loaded.s = ndk.set_var.set_escape_uri(" :")
}
}
--- response_body
%20%3A
=== TEST 11: call in log_by_lua
--- config
location /t {
echo ok;
log_by_lua_block {
local s = ndk.set_var.set_escape_uri(" :")
ngx.log(ngx.WARN, "s = ", s)
}
}
--- response_body
ok
--- error_log
s = %20%3A
--- no_error_log
[error]
=== TEST 12: call in init_worker_by_lua
--- http_config
init_worker_by_lua_block {
package.loaded.s = ndk.set_var.set_escape_uri(" :")
}
--- config
location /t {
content_by_lua_block {
ngx.say(package.loaded.s)
}
}
--- response_body
%20%3A
=== TEST 13: cache the function in init_worker_by_lua and call in other phases
--- http_config
init_worker_by_lua_block {
package.loaded.set_escape_uri = ndk.set_var.set_escape_uri
}
--- config
location /t {
content_by_lua_block {
ngx.say(package.loaded.set_escape_uri(" :"))
}
}
--- response_body
%20%3A
|