1
# vim:set ft= ts=4 sw=4 et fdm=marker:
3
use Test::Nginx::Socket;
5
#worker_connections(1014);
12
plan tests => repeat_each() * (blocks() * 2);
24
m = ngx.re.match("hello, 1234", "([0-9]+)", "o")
28
ngx.say("not matched!")
39
=== TEST 2: escaping sequences
43
m = ngx.re.match("hello, 1234", "(\\\\d+)", "o")
47
ngx.say("not matched!")
58
=== TEST 3: escaping sequences (bad)
62
m = ngx.re.match("hello, 1234", "(\\d+)", "o")
66
ngx.say("not matched!")
77
=== TEST 4: escaping sequences in [[ ... ]]
81
m = ngx.re.match("hello, 1234", "[[\\d+]]", "o")
85
ngx.say("not matched!")
96
=== TEST 5: single capture
100
m = ngx.re.match("hello, 1234", "([0-9]{2})[0-9]+", "o")
105
ngx.say("not matched!")
117
=== TEST 6: multiple captures
121
m = ngx.re.match("hello, 1234", "([a-z]+).*?([0-9]{2})[0-9]+", "o")
127
ngx.say("not matched!")
140
=== TEST 7: not matched
144
m = ngx.re.match("hello, 1234", "foo", "o")
148
ngx.say("not matched: ", m)
159
=== TEST 8: case sensitive by default
163
m = ngx.re.match("hello, 1234", "HELLO", "o")
167
ngx.say("not matched: ", m)
178
=== TEST 9: case sensitive by default
182
m = ngx.re.match("hello, 1234", "HELLO", "oi")
186
ngx.say("not matched: ", m)
197
=== TEST 10: UTF-8 mode
201
m = ngx.re.match("hello章亦春", "HELLO.{2}", "iou")
205
ngx.say("not matched: ", m)
216
=== TEST 11: multi-line mode (^ at line head)
220
m = ngx.re.match("hello\\nworld", "^world", "mo")
224
ngx.say("not matched: ", m)
235
=== TEST 12: multi-line mode (. does not match \n)
239
m = ngx.re.match("hello\\nworld", ".*", "om")
243
ngx.say("not matched: ", m)
254
=== TEST 13: single-line mode (^ as normal)
258
m = ngx.re.match("hello\\nworld", "^world", "so")
262
ngx.say("not matched: ", m)
273
=== TEST 14: single-line mode (dot all)
277
m = ngx.re.match("hello\\nworld", ".*", "os")
281
ngx.say("not matched: ", m)
293
=== TEST 15: extended mode (ignore whitespaces)
297
m = ngx.re.match("hello\\nworld", "\\\\w \\\\w", "xo")
301
ngx.say("not matched: ", m)
312
=== TEST 16: bad pattern
316
rc, m = pcall(ngx.re.match, "hello\\nworld", "(abc", "o")
321
ngx.say("not matched: ", m)
324
ngx.say("error: ", m)
331
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
335
=== TEST 17: bad option
339
rc, m = pcall(ngx.re.match, "hello\\nworld", ".*", "Ho")
344
ngx.say("not matched: ", m)
347
ngx.say("error: ", m)
354
error: bad argument #3 to '?' (unknown flag "H")
358
=== TEST 18: extended mode (ignore whitespaces)
362
m = ngx.re.match("hello, world", "(world)|(hello)", "xo")
368
ngx.say("not matched: ", m)
381
=== TEST 19: optional trailing captures
385
m = ngx.re.match("hello, 1234", "([0-9]+)(h?)", "o")
391
ngx.say("not matched!")
397
--- response_body eval
405
=== TEST 20: anchored match (failed)
409
m = ngx.re.match("hello, 1234", "([0-9]+)", "oa")
413
ngx.say("not matched!")
424
=== TEST 21: anchored match (succeeded)
428
m = ngx.re.match("1234, hello", "([0-9]+)", "ao")
432
ngx.say("not matched!")
443
=== TEST 22: match with ctx but no pos
448
m = ngx.re.match("1234, hello", "([0-9]+)", "o", ctx)
453
ngx.say("not matched!")
466
=== TEST 23: match with ctx and a pos
470
local ctx = { pos = 2 }
471
m = ngx.re.match("1234, hello", "([0-9]+)", "o", ctx)
476
ngx.say("not matched!")
489
=== TEST 24: sanity (set_by_lua)
493
m = ngx.re.match("hello, 1234", "([0-9]+)", "o")
497
return "not matched!"
509
=== TEST 25: match (look-behind assertion)
514
local m = ngx.re.match("{foobarbaz}", "(?<=foo)bar|(?<=bar)baz", "o", ctx)
517
m = ngx.re.match("{foobarbaz}", "(?<=foo)bar|(?<=bar)baz", "o", ctx)
529
=== TEST 26: match (with regex cache)
533
local m = ngx.re.match("hello, 1234", "([A-Z]+)", "io")
536
m = ngx.re.match("1234, okay", "([A-Z]+)", "io")
539
m = ngx.re.match("hello, 1234", "([A-Z]+)", "o")
552
=== TEST 27: match (with regex cache and ctx)
557
local m = ngx.re.match("hello, 1234", "([A-Z]+)", "io", ctx)
561
m = ngx.re.match("1234, okay", "([A-Z]+)", "io", ctx)
566
m = ngx.re.match("hi, 1234", "([A-Z]+)", "o", ctx)
583
=== TEST 28: exceeding regex cache max entries
585
lua_regex_cache_max_entries 2;
589
local m = ngx.re.match("hello, 1234", "([0-9]+)", "o")
592
m = ngx.re.match("howdy, 567", "([0-9]+)", "oi")
595
m = ngx.re.match("hiya, 98", "([0-9]+)", "ox")
608
=== TEST 29: disable regex cache completely
610
lua_regex_cache_max_entries 0;
614
local m = ngx.re.match("hello, 1234", "([0-9]+)", "o")
617
m = ngx.re.match("howdy, 567", "([0-9]+)", "oi")
620
m = ngx.re.match("hiya, 98", "([0-9]+)", "ox")