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]+)")
28
ngx.say("not matched!")
39
=== TEST 2: escaping sequences
43
m = ngx.re.match("hello, 1234", "(\\\\d+)")
47
ngx.say("not matched!")
58
=== TEST 3: escaping sequences (bad)
62
m = ngx.re.match("hello, 1234", "(\\d+)")
66
ngx.say("not matched!")
77
=== TEST 4: escaping sequences in [[ ... ]]
81
m = ngx.re.match("hello, 1234", "[[\\d+]]")
85
ngx.say("not matched!")
96
=== TEST 5: single capture
100
m = ngx.re.match("hello, 1234", "([0-9]{2})[0-9]+")
105
ngx.say("not matched!")
117
=== TEST 6: multiple captures
121
m = ngx.re.match("hello, 1234", "([a-z]+).*?([0-9]{2})[0-9]+", "")
127
ngx.say("not matched!")
140
=== TEST 7: multiple captures (with o)
144
m = ngx.re.match("hello, 1234", "([a-z]+).*?([0-9]{2})[0-9]+", "o")
150
ngx.say("not matched!")
163
=== TEST 8: not matched
167
m = ngx.re.match("hello, 1234", "foo")
171
ngx.say("not matched: ", m)
182
=== TEST 9: case sensitive by default
186
m = ngx.re.match("hello, 1234", "HELLO")
190
ngx.say("not matched: ", m)
201
=== TEST 10: case sensitive by default
205
m = ngx.re.match("hello, 1234", "HELLO", "i")
209
ngx.say("not matched: ", m)
220
=== TEST 11: UTF-8 mode
224
m = ngx.re.match("hello章亦春", "HELLO.{2}", "iu")
228
ngx.say("not matched: ", m)
239
=== TEST 12: multi-line mode (^ at line head)
243
m = ngx.re.match("hello\\nworld", "^world", "m")
247
ngx.say("not matched: ", m)
258
=== TEST 13: multi-line mode (. does not match \n)
262
m = ngx.re.match("hello\\nworld", ".*", "m")
266
ngx.say("not matched: ", m)
277
=== TEST 14: single-line mode (^ as normal)
281
m = ngx.re.match("hello\\nworld", "^world", "s")
285
ngx.say("not matched: ", m)
296
=== TEST 15: single-line mode (dot all)
300
m = ngx.re.match("hello\\nworld", ".*", "s")
304
ngx.say("not matched: ", m)
316
=== TEST 16: extended mode (ignore whitespaces)
320
m = ngx.re.match("hello\\nworld", "\\\\w \\\\w", "x")
324
ngx.say("not matched: ", m)
335
=== TEST 17: bad pattern
339
rc, m = pcall(ngx.re.match, "hello\\nworld", "(abc")
344
ngx.say("not matched: ", m)
347
ngx.say("error: ", m)
354
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
358
=== TEST 18: bad option
362
rc, m = pcall(ngx.re.match, "hello\\nworld", ".*", "H")
367
ngx.say("not matched: ", m)
370
ngx.say("error: ", m)
377
error: bad argument #3 to '?' (unknown flag "H")
381
=== TEST 19: extended mode (ignore whitespaces)
385
m = ngx.re.match("hello, world", "(world)|(hello)", "x")
391
ngx.say("not matched: ", m)
404
=== TEST 20: optional trailing captures
408
m = ngx.re.match("hello, 1234", "([0-9]+)(h?)")
414
ngx.say("not matched!")
420
--- response_body eval
428
=== TEST 21: anchored match (failed)
432
m = ngx.re.match("hello, 1234", "([0-9]+)", "a")
436
ngx.say("not matched!")
447
=== TEST 22: anchored match (succeeded)
451
m = ngx.re.match("1234, hello", "([0-9]+)", "a")
455
ngx.say("not matched!")
466
=== TEST 23: match with ctx but no pos
471
m = ngx.re.match("1234, hello", "([0-9]+)", "", ctx)
476
ngx.say("not matched!")
489
=== TEST 24: match with ctx and a pos
493
local ctx = { pos = 2 }
494
m = ngx.re.match("1234, hello", "([0-9]+)", "", ctx)
499
ngx.say("not matched!")
512
=== TEST 25: sanity (set_by_lua)
516
m = ngx.re.match("hello, 1234", "([0-9]+)")
520
return "not matched!"
532
=== TEST 26: match (look-behind assertion)
537
local m = ngx.re.match("{foobarbaz}", "(?<=foo)bar|(?<=bar)baz", "", ctx)
540
m = ngx.re.match("{foobarbaz}", "(?<=foo)bar|(?<=bar)baz", "", ctx)