~britco/nginx/master

« back to all changes in this revision

Viewing changes to debian/modules/nginx-lua/t/038-match-o.t

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry, Kartik Mistry
  • Date: 2011-09-26 10:17:04 UTC
  • mfrom: (4.2.38 sid)
  • Revision ID: package-import@ubuntu.com-20110926101704-x8pxngiujrmkxnn3
Tags: 1.1.4-2
[Kartik Mistry]
* debian/modules:
  + Updated nginx-upload-progress module, Thanks to upstream for fixing issue
    that FTBFS nginx on kFreeBSD-* archs.
  + Updated nginx-lua module to latest upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim:set ft= ts=4 sw=4 et fdm=marker:
 
2
use lib 'lib';
 
3
use Test::Nginx::Socket;
 
4
 
 
5
#worker_connections(1014);
 
6
#master_on();
 
7
#workers(2);
 
8
log_level('warn');
 
9
 
 
10
repeat_each(2);
 
11
 
 
12
plan tests => repeat_each() * (blocks() * 2);
 
13
 
 
14
#no_diff();
 
15
#no_long_string();
 
16
run_tests();
 
17
 
 
18
__DATA__
 
19
 
 
20
=== TEST 1: sanity
 
21
--- config
 
22
    location /re {
 
23
        content_by_lua '
 
24
            m = ngx.re.match("hello, 1234", "([0-9]+)", "o")
 
25
            if m then
 
26
                ngx.say(m[0])
 
27
            else
 
28
                ngx.say("not matched!")
 
29
            end
 
30
        ';
 
31
    }
 
32
--- request
 
33
    GET /re
 
34
--- response_body
 
35
1234
 
36
 
 
37
 
 
38
 
 
39
=== TEST 2: escaping sequences
 
40
--- config
 
41
    location /re {
 
42
        content_by_lua '
 
43
            m = ngx.re.match("hello, 1234", "(\\\\d+)", "o")
 
44
            if m then
 
45
                ngx.say(m[0])
 
46
            else
 
47
                ngx.say("not matched!")
 
48
            end
 
49
        ';
 
50
    }
 
51
--- request
 
52
    GET /re
 
53
--- response_body
 
54
1234
 
55
 
 
56
 
 
57
 
 
58
=== TEST 3: escaping sequences (bad)
 
59
--- config
 
60
    location /re {
 
61
        content_by_lua '
 
62
            m = ngx.re.match("hello, 1234", "(\\d+)", "o")
 
63
            if m then
 
64
                ngx.say(m[0])
 
65
            else
 
66
                ngx.say("not matched!")
 
67
            end
 
68
        ';
 
69
    }
 
70
--- request
 
71
    GET /re
 
72
--- response_body
 
73
not matched!
 
74
 
 
75
 
 
76
 
 
77
=== TEST 4: escaping sequences in [[ ... ]]
 
78
--- config
 
79
    location /re {
 
80
        content_by_lua '
 
81
            m = ngx.re.match("hello, 1234", "[[\\d+]]", "o")
 
82
            if m then
 
83
                ngx.say(m[0])
 
84
            else
 
85
                ngx.say("not matched!")
 
86
            end
 
87
        ';
 
88
    }
 
89
--- request
 
90
    GET /re
 
91
--- response_body
 
92
not matched!
 
93
 
 
94
 
 
95
 
 
96
=== TEST 5: single capture
 
97
--- config
 
98
    location /re {
 
99
        content_by_lua '
 
100
            m = ngx.re.match("hello, 1234", "([0-9]{2})[0-9]+", "o")
 
101
            if m then
 
102
                ngx.say(m[0])
 
103
                ngx.say(m[1])
 
104
            else
 
105
                ngx.say("not matched!")
 
106
            end
 
107
        ';
 
108
    }
 
109
--- request
 
110
    GET /re
 
111
--- response_body
 
112
1234
 
113
12
 
114
 
 
115
 
 
116
 
 
117
=== TEST 6: multiple captures
 
118
--- config
 
119
    location /re {
 
120
        content_by_lua '
 
121
            m = ngx.re.match("hello, 1234", "([a-z]+).*?([0-9]{2})[0-9]+", "o")
 
122
            if m then
 
123
                ngx.say(m[0])
 
124
                ngx.say(m[1])
 
125
                ngx.say(m[2])
 
126
            else
 
127
                ngx.say("not matched!")
 
128
            end
 
129
        ';
 
130
    }
 
131
--- request
 
132
    GET /re
 
133
--- response_body
 
134
hello, 1234
 
135
hello
 
136
12
 
137
 
 
138
 
 
139
 
 
140
=== TEST 7: not matched
 
141
--- config
 
142
    location /re {
 
143
        content_by_lua '
 
144
            m = ngx.re.match("hello, 1234", "foo", "o")
 
145
            if m then
 
146
                ngx.say(m[0])
 
147
            else
 
148
                ngx.say("not matched: ", m)
 
149
            end
 
150
        ';
 
151
    }
 
152
--- request
 
153
    GET /re
 
154
--- response_body
 
155
not matched: nil
 
156
 
 
157
 
 
158
 
 
159
=== TEST 8: case sensitive by default
 
160
--- config
 
161
    location /re {
 
162
        content_by_lua '
 
163
            m = ngx.re.match("hello, 1234", "HELLO", "o")
 
164
            if m then
 
165
                ngx.say(m[0])
 
166
            else
 
167
                ngx.say("not matched: ", m)
 
168
            end
 
169
        ';
 
170
    }
 
171
--- request
 
172
    GET /re
 
173
--- response_body
 
174
not matched: nil
 
175
 
 
176
 
 
177
 
 
178
=== TEST 9: case sensitive by default
 
179
--- config
 
180
    location /re {
 
181
        content_by_lua '
 
182
            m = ngx.re.match("hello, 1234", "HELLO", "oi")
 
183
            if m then
 
184
                ngx.say(m[0])
 
185
            else
 
186
                ngx.say("not matched: ", m)
 
187
            end
 
188
        ';
 
189
    }
 
190
--- request
 
191
    GET /re
 
192
--- response_body
 
193
hello
 
194
 
 
195
 
 
196
 
 
197
=== TEST 10: UTF-8 mode
 
198
--- config
 
199
    location /re {
 
200
        content_by_lua '
 
201
            m = ngx.re.match("hello章亦春", "HELLO.{2}", "iou")
 
202
            if m then
 
203
                ngx.say(m[0])
 
204
            else
 
205
                ngx.say("not matched: ", m)
 
206
            end
 
207
        ';
 
208
    }
 
209
--- request
 
210
    GET /re
 
211
--- response_body
 
212
hello章亦
 
213
 
 
214
 
 
215
 
 
216
=== TEST 11: multi-line mode (^ at line head)
 
217
--- config
 
218
    location /re {
 
219
        content_by_lua '
 
220
            m = ngx.re.match("hello\\nworld", "^world", "mo")
 
221
            if m then
 
222
                ngx.say(m[0])
 
223
            else
 
224
                ngx.say("not matched: ", m)
 
225
            end
 
226
        ';
 
227
    }
 
228
--- request
 
229
    GET /re
 
230
--- response_body
 
231
world
 
232
 
 
233
 
 
234
 
 
235
=== TEST 12: multi-line mode (. does not match \n)
 
236
--- config
 
237
    location /re {
 
238
        content_by_lua '
 
239
            m = ngx.re.match("hello\\nworld", ".*", "om")
 
240
            if m then
 
241
                ngx.say(m[0])
 
242
            else
 
243
                ngx.say("not matched: ", m)
 
244
            end
 
245
        ';
 
246
    }
 
247
--- request
 
248
    GET /re
 
249
--- response_body
 
250
hello
 
251
 
 
252
 
 
253
 
 
254
=== TEST 13: single-line mode (^ as normal)
 
255
--- config
 
256
    location /re {
 
257
        content_by_lua '
 
258
            m = ngx.re.match("hello\\nworld", "^world", "so")
 
259
            if m then
 
260
                ngx.say(m[0])
 
261
            else
 
262
                ngx.say("not matched: ", m)
 
263
            end
 
264
        ';
 
265
    }
 
266
--- request
 
267
    GET /re
 
268
--- response_body
 
269
not matched: nil
 
270
 
 
271
 
 
272
 
 
273
=== TEST 14: single-line mode (dot all)
 
274
--- config
 
275
    location /re {
 
276
        content_by_lua '
 
277
            m = ngx.re.match("hello\\nworld", ".*", "os")
 
278
            if m then
 
279
                ngx.say(m[0])
 
280
            else
 
281
                ngx.say("not matched: ", m)
 
282
            end
 
283
        ';
 
284
    }
 
285
--- request
 
286
    GET /re
 
287
--- response_body
 
288
hello
 
289
world
 
290
 
 
291
 
 
292
 
 
293
=== TEST 15: extended mode (ignore whitespaces)
 
294
--- config
 
295
    location /re {
 
296
        content_by_lua '
 
297
            m = ngx.re.match("hello\\nworld", "\\\\w     \\\\w", "xo")
 
298
            if m then
 
299
                ngx.say(m[0])
 
300
            else
 
301
                ngx.say("not matched: ", m)
 
302
            end
 
303
        ';
 
304
    }
 
305
--- request
 
306
    GET /re
 
307
--- response_body
 
308
he
 
309
 
 
310
 
 
311
 
 
312
=== TEST 16: bad pattern
 
313
--- config
 
314
    location /re {
 
315
        content_by_lua '
 
316
            rc, m = pcall(ngx.re.match, "hello\\nworld", "(abc", "o")
 
317
            if rc then
 
318
                if m then
 
319
                    ngx.say(m[0])
 
320
                else
 
321
                    ngx.say("not matched: ", m)
 
322
                end
 
323
            else
 
324
                ngx.say("error: ", m)
 
325
            end
 
326
        ';
 
327
    }
 
328
--- request
 
329
    GET /re
 
330
--- response_body
 
331
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
 
332
 
 
333
 
 
334
 
 
335
=== TEST 17: bad option
 
336
--- config
 
337
    location /re {
 
338
        content_by_lua '
 
339
            rc, m = pcall(ngx.re.match, "hello\\nworld", ".*", "Ho")
 
340
            if rc then
 
341
                if m then
 
342
                    ngx.say(m[0])
 
343
                else
 
344
                    ngx.say("not matched: ", m)
 
345
                end
 
346
            else
 
347
                ngx.say("error: ", m)
 
348
            end
 
349
        ';
 
350
    }
 
351
--- request
 
352
    GET /re
 
353
--- response_body
 
354
error: bad argument #3 to '?' (unknown flag "H")
 
355
 
 
356
 
 
357
 
 
358
=== TEST 18: extended mode (ignore whitespaces)
 
359
--- config
 
360
    location /re {
 
361
        content_by_lua '
 
362
            m = ngx.re.match("hello, world", "(world)|(hello)", "xo")
 
363
            if m then
 
364
                ngx.say(m[0])
 
365
                ngx.say(m[1])
 
366
                ngx.say(m[2])
 
367
            else
 
368
                ngx.say("not matched: ", m)
 
369
            end
 
370
        ';
 
371
    }
 
372
--- request
 
373
    GET /re
 
374
--- response_body
 
375
hello
 
376
nil
 
377
hello
 
378
 
 
379
 
 
380
 
 
381
=== TEST 19: optional trailing captures
 
382
--- config
 
383
    location /re {
 
384
        content_by_lua '
 
385
            m = ngx.re.match("hello, 1234", "([0-9]+)(h?)", "o")
 
386
            if m then
 
387
                ngx.say(m[0])
 
388
                ngx.say(m[1])
 
389
                ngx.say(m[2])
 
390
            else
 
391
                ngx.say("not matched!")
 
392
            end
 
393
        ';
 
394
    }
 
395
--- request
 
396
    GET /re
 
397
--- response_body eval
 
398
"1234
 
399
1234
 
400
 
 
401
"
 
402
 
 
403
 
 
404
 
 
405
=== TEST 20: anchored match (failed)
 
406
--- config
 
407
    location /re {
 
408
        content_by_lua '
 
409
            m = ngx.re.match("hello, 1234", "([0-9]+)", "oa")
 
410
            if m then
 
411
                ngx.say(m[0])
 
412
            else
 
413
                ngx.say("not matched!")
 
414
            end
 
415
        ';
 
416
    }
 
417
--- request
 
418
    GET /re
 
419
--- response_body
 
420
not matched!
 
421
 
 
422
 
 
423
 
 
424
=== TEST 21: anchored match (succeeded)
 
425
--- config
 
426
    location /re {
 
427
        content_by_lua '
 
428
            m = ngx.re.match("1234, hello", "([0-9]+)", "ao")
 
429
            if m then
 
430
                ngx.say(m[0])
 
431
            else
 
432
                ngx.say("not matched!")
 
433
            end
 
434
        ';
 
435
    }
 
436
--- request
 
437
    GET /re
 
438
--- response_body
 
439
1234
 
440
 
 
441
 
 
442
 
 
443
=== TEST 22: match with ctx but no pos
 
444
--- config
 
445
    location /re {
 
446
        content_by_lua '
 
447
            local ctx = {}
 
448
            m = ngx.re.match("1234, hello", "([0-9]+)", "o", ctx)
 
449
            if m then
 
450
                ngx.say(m[0])
 
451
                ngx.say(ctx.pos)
 
452
            else
 
453
                ngx.say("not matched!")
 
454
                ngx.say(ctx.pos)
 
455
            end
 
456
        ';
 
457
    }
 
458
--- request
 
459
    GET /re
 
460
--- response_body
 
461
1234
 
462
4
 
463
 
 
464
 
 
465
 
 
466
=== TEST 23: match with ctx and a pos
 
467
--- config
 
468
    location /re {
 
469
        content_by_lua '
 
470
            local ctx = { pos = 2 }
 
471
            m = ngx.re.match("1234, hello", "([0-9]+)", "o", ctx)
 
472
            if m then
 
473
                ngx.say(m[0])
 
474
                ngx.say(ctx.pos)
 
475
            else
 
476
                ngx.say("not matched!")
 
477
                ngx.say(ctx.pos)
 
478
            end
 
479
        ';
 
480
    }
 
481
--- request
 
482
    GET /re
 
483
--- response_body
 
484
34
 
485
4
 
486
 
 
487
 
 
488
 
 
489
=== TEST 24: sanity (set_by_lua)
 
490
--- config
 
491
    location /re {
 
492
        set_by_lua $res '
 
493
            m = ngx.re.match("hello, 1234", "([0-9]+)", "o")
 
494
            if m then
 
495
                return m[0]
 
496
            else
 
497
                return "not matched!"
 
498
            end
 
499
        ';
 
500
        echo $res;
 
501
    }
 
502
--- request
 
503
    GET /re
 
504
--- response_body
 
505
1234
 
506
 
 
507
 
 
508
 
 
509
=== TEST 25: match (look-behind assertion)
 
510
--- config
 
511
    location /re {
 
512
        content_by_lua '
 
513
            local ctx = {}
 
514
            local m = ngx.re.match("{foobarbaz}", "(?<=foo)bar|(?<=bar)baz", "o", ctx)
 
515
            ngx.say(m and m[0])
 
516
 
 
517
            m = ngx.re.match("{foobarbaz}", "(?<=foo)bar|(?<=bar)baz", "o", ctx)
 
518
            ngx.say(m and m[0])
 
519
        ';
 
520
    }
 
521
--- request
 
522
    GET /re
 
523
--- response_body
 
524
bar
 
525
baz
 
526
 
 
527
 
 
528
 
 
529
=== TEST 26: match (with regex cache)
 
530
--- config
 
531
    location /re {
 
532
        content_by_lua '
 
533
            local m = ngx.re.match("hello, 1234", "([A-Z]+)", "io")
 
534
            ngx.say(m and m[0])
 
535
 
 
536
            m = ngx.re.match("1234, okay", "([A-Z]+)", "io")
 
537
            ngx.say(m and m[0])
 
538
 
 
539
            m = ngx.re.match("hello, 1234", "([A-Z]+)", "o")
 
540
            ngx.say(m and m[0])
 
541
        ';
 
542
    }
 
543
--- request
 
544
    GET /re
 
545
--- response_body
 
546
hello
 
547
okay
 
548
nil
 
549
 
 
550
 
 
551
 
 
552
=== TEST 27: match (with regex cache and ctx)
 
553
--- config
 
554
    location /re {
 
555
        content_by_lua '
 
556
            local ctx = {}
 
557
            local m = ngx.re.match("hello, 1234", "([A-Z]+)", "io", ctx)
 
558
            ngx.say(m and m[0])
 
559
            ngx.say(ctx.pos)
 
560
 
 
561
            m = ngx.re.match("1234, okay", "([A-Z]+)", "io", ctx)
 
562
            ngx.say(m and m[0])
 
563
            ngx.say(ctx.pos)
 
564
 
 
565
            ctx.pos = 0
 
566
            m = ngx.re.match("hi, 1234", "([A-Z]+)", "o", ctx)
 
567
            ngx.say(m and m[0])
 
568
            ngx.say(ctx.pos)
 
569
        ';
 
570
    }
 
571
--- request
 
572
    GET /re
 
573
--- response_body
 
574
hello
 
575
5
 
576
okay
 
577
10
 
578
nil
 
579
0
 
580
 
 
581
 
 
582
 
 
583
=== TEST 28: exceeding regex cache max entries
 
584
--- http_config
 
585
    lua_regex_cache_max_entries 2;
 
586
--- config
 
587
    location /re {
 
588
        content_by_lua '
 
589
            local m = ngx.re.match("hello, 1234", "([0-9]+)", "o")
 
590
            ngx.say(m and m[0])
 
591
 
 
592
            m = ngx.re.match("howdy, 567", "([0-9]+)", "oi")
 
593
            ngx.say(m and m[0])
 
594
 
 
595
            m = ngx.re.match("hiya, 98", "([0-9]+)", "ox")
 
596
            ngx.say(m and m[0])
 
597
        ';
 
598
    }
 
599
--- request
 
600
    GET /re
 
601
--- response_body
 
602
1234
 
603
567
 
604
98
 
605
 
 
606
 
 
607
 
 
608
=== TEST 29: disable regex cache completely
 
609
--- http_config
 
610
    lua_regex_cache_max_entries 0;
 
611
--- config
 
612
    location /re {
 
613
        content_by_lua '
 
614
            local m = ngx.re.match("hello, 1234", "([0-9]+)", "o")
 
615
            ngx.say(m and m[0])
 
616
 
 
617
            m = ngx.re.match("howdy, 567", "([0-9]+)", "oi")
 
618
            ngx.say(m and m[0])
 
619
 
 
620
            m = ngx.re.match("hiya, 98", "([0-9]+)", "ox")
 
621
            ngx.say(m and m[0])
 
622
        ';
 
623
    }
 
624
--- request
 
625
    GET /re
 
626
--- response_body
 
627
1234
 
628
567
 
629
98
 
630