<commit_before># vi:ft=
use lib 'lib';
use Test::Nginx::Socket;

#worker_connections(1014);
#master_process_enabled(1);
log_level('warn');

repeat_each(120);

plan tests => blocks() * repeat_each() * 2;

no_long_string();
run_tests();

__DATA__

=== TEST 1: basic
--- config
    location /lua {
        # NOTE: the newline escape sequence must be double-escaped, as nginx config
        # parser will unescape first!
        content_by_lua 'ngx.echo("Hello, Lua!\\n")';
    }
--- request
GET /lua
--- response_body
Hello, Lua!



=== TEST 2: variable
--- config
    location /lua {
        # NOTE: the newline escape sequence must be double-escaped, as nginx config
        # parser will unescape first!
        content_by_lua 'v = ngx.var["request_uri"] ngx.echo("request_uri: ", v, "\\n")';
    }
--- request
GET /lua?a=1&b=2
--- response_body
request_uri: /lua?a=1&b=2



=== TEST 3: variable (file)
--- config
    location /lua {
        content_by_lua_file html/test.lua;
    }
--- user_files
>>> test.lua
v = ngx.var["request_uri"]
ngx.echo("request_uri: ", v, "\n")
--- request
GET /lua?a=1&b=2
--- response_body
request_uri: /lua?a=1&b=2



=== TEST 4: calc expression
--- config
    location /lua {
        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 = ngx.var["arg_exp"]
-- print("exp: '", exp_str, "'\n")
local status, res
status, res = pcall(uri_unescape, exp_str)
if not status then
    ngx.echo("error: ", res, "\n")
    return
end
status, res = pcall(eval_exp, res)
if status then
    ngx.echo("result: ", res, "\n")
else
    ngx.echo("error: ", res, "\n")
end
--- request
GET /lua?exp=1%2B2*math.sin(3)%2Fmath.exp(4)-math.sqrt(2)
--- response_body
result: -0.4090441561579



=== TEST 5: read $arg_xxx
--- config
    location = /lua {
        content_by_lua 'who = ngx.var.arg_who
            ngx.echo("Hello, ", who, "!")';
    }
--- request
GET /lua?who=agentzh
--- response_body chomp
Hello, agentzh!



=== TEST 6: capture location
--- config
    location /other {
        echo "hello, world";
    }

    location /lua {
        content_by_lua 'res = ngx.location.capture("/other"); ngx.echo("status=", res.status, " "); ngx.echo("body=", res.body)';
    }
--- request
GET /lua
--- response_body
status=200 body=hello, world

<commit_msg>added more tests<commit_after># vi:ft=
use lib 'lib';
use Test::Nginx::Socket;

#worker_connections(1014);
#master_process_enabled(1);
log_level('warn');

repeat_each(120);

plan tests => blocks() * repeat_each() * 2;

no_long_string();
run_tests();

__DATA__

=== TEST 1: basic
--- config
    location /lua {
        # NOTE: the newline escape sequence must be double-escaped, as nginx config
        # parser will unescape first!
        content_by_lua 'ngx.echo("Hello, Lua!\\n")';
    }
--- request
GET /lua
--- response_body
Hello, Lua!



=== TEST 2: variable
--- config
    location /lua {
        # NOTE: the newline escape sequence must be double-escaped, as nginx config
        # parser will unescape first!
        content_by_lua 'v = ngx.var["request_uri"] ngx.echo("request_uri: ", v, "\\n")';
    }
--- request
GET /lua?a=1&b=2
--- response_body
request_uri: /lua?a=1&b=2



=== TEST 3: variable (file)
--- config
    location /lua {
        content_by_lua_file html/test.lua;
    }
--- user_files
>>> test.lua
v = ngx.var["request_uri"]
ngx.echo("request_uri: ", v, "\n")
--- request
GET /lua?a=1&b=2
--- response_body
request_uri: /lua?a=1&b=2



=== TEST 4: calc expression
--- config
    location /lua {
        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 = ngx.var["arg_exp"]
-- print("exp: '", exp_str, "'\n")
local status, res
status, res = pcall(uri_unescape, exp_str)
if not status then
    ngx.echo("error: ", res, "\n")
    return
end
status, res = pcall(eval_exp, res)
if status then
    ngx.echo("result: ", res, "\n")
else
    ngx.echo("error: ", res, "\n")
end
--- request
GET /lua?exp=1%2B2*math.sin(3)%2Fmath.exp(4)-math.sqrt(2)
--- response_body
result: -0.4090441561579



=== TEST 5: read $arg_xxx
--- config
    location = /lua {
        content_by_lua 'who = ngx.var.arg_who
            ngx.echo("Hello, ", who, "!")';
    }
--- request
GET /lua?who=agentzh
--- response_body chomp
Hello, agentzh!



=== TEST 6: capture location
--- config
    location /other {
        echo "hello, world";
    }

    location /lua {
        content_by_lua 'res = ngx.location.capture("/other"); ngx.echo("status=", res.status, " "); ngx.echo("body=", res.body)';
    }
--- request
GET /lua
--- response_body
status=200 body=hello, world



=== TEST 7: capture non-existed location
--- config
	location /lua {
		content_by_lua 'res = ngx.location.capture("/other"); ngx.echo("status=", res.status)';
	}
--- request
GET /lua
--- response_body: status=404



=== TEST 8: invalid capture location (not as expected...)
--- config
	location /lua {
		content_by_lua 'res = ngx.location.capture("*(#*"); ngx.echo("res=", res)';
	}
--- request
GET /lua
--- response_body
res=nil
--- SKIP


{'fraction_non_alphanumeric': 0.1744870651204282, 'fraction_numerical': 0.01944692239072257, 'mean_word_length': 2.967445152158528, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1945114', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2029, 'n_words': 496}