~ubuntu-branches/ubuntu/raring/nginx/raring-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry, Kartik Mistry
  • Date: 2011-09-26 10:17:04 UTC
  • Revision ID: package-import@ubuntu.com-20110926101704-xij7mky6b7i8vx41
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]+)")
 
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+)")
 
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+)")
 
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+]]")
 
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]+")
 
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]+", "")
 
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: multiple captures (with o)
 
141
--- config
 
142
    location /re {
 
143
        content_by_lua '
 
144
            m = ngx.re.match("hello, 1234", "([a-z]+).*?([0-9]{2})[0-9]+", "o")
 
145
            if m then
 
146
                ngx.say(m[0])
 
147
                ngx.say(m[1])
 
148
                ngx.say(m[2])
 
149
            else
 
150
                ngx.say("not matched!")
 
151
            end
 
152
        ';
 
153
    }
 
154
--- request
 
155
    GET /re
 
156
--- response_body
 
157
hello, 1234
 
158
hello
 
159
12
 
160
 
 
161
 
 
162
 
 
163
=== TEST 8: not matched
 
164
--- config
 
165
    location /re {
 
166
        content_by_lua '
 
167
            m = ngx.re.match("hello, 1234", "foo")
 
168
            if m then
 
169
                ngx.say(m[0])
 
170
            else
 
171
                ngx.say("not matched: ", m)
 
172
            end
 
173
        ';
 
174
    }
 
175
--- request
 
176
    GET /re
 
177
--- response_body
 
178
not matched: nil
 
179
 
 
180
 
 
181
 
 
182
=== TEST 9: case sensitive by default
 
183
--- config
 
184
    location /re {
 
185
        content_by_lua '
 
186
            m = ngx.re.match("hello, 1234", "HELLO")
 
187
            if m then
 
188
                ngx.say(m[0])
 
189
            else
 
190
                ngx.say("not matched: ", m)
 
191
            end
 
192
        ';
 
193
    }
 
194
--- request
 
195
    GET /re
 
196
--- response_body
 
197
not matched: nil
 
198
 
 
199
 
 
200
 
 
201
=== TEST 10: case sensitive by default
 
202
--- config
 
203
    location /re {
 
204
        content_by_lua '
 
205
            m = ngx.re.match("hello, 1234", "HELLO", "i")
 
206
            if m then
 
207
                ngx.say(m[0])
 
208
            else
 
209
                ngx.say("not matched: ", m)
 
210
            end
 
211
        ';
 
212
    }
 
213
--- request
 
214
    GET /re
 
215
--- response_body
 
216
hello
 
217
 
 
218
 
 
219
 
 
220
=== TEST 11: UTF-8 mode
 
221
--- config
 
222
    location /re {
 
223
        content_by_lua '
 
224
            m = ngx.re.match("hello章亦春", "HELLO.{2}", "iu")
 
225
            if m then
 
226
                ngx.say(m[0])
 
227
            else
 
228
                ngx.say("not matched: ", m)
 
229
            end
 
230
        ';
 
231
    }
 
232
--- request
 
233
    GET /re
 
234
--- response_body
 
235
hello章亦
 
236
 
 
237
 
 
238
 
 
239
=== TEST 12: multi-line mode (^ at line head)
 
240
--- config
 
241
    location /re {
 
242
        content_by_lua '
 
243
            m = ngx.re.match("hello\\nworld", "^world", "m")
 
244
            if m then
 
245
                ngx.say(m[0])
 
246
            else
 
247
                ngx.say("not matched: ", m)
 
248
            end
 
249
        ';
 
250
    }
 
251
--- request
 
252
    GET /re
 
253
--- response_body
 
254
world
 
255
 
 
256
 
 
257
 
 
258
=== TEST 13: multi-line mode (. does not match \n)
 
259
--- config
 
260
    location /re {
 
261
        content_by_lua '
 
262
            m = ngx.re.match("hello\\nworld", ".*", "m")
 
263
            if m then
 
264
                ngx.say(m[0])
 
265
            else
 
266
                ngx.say("not matched: ", m)
 
267
            end
 
268
        ';
 
269
    }
 
270
--- request
 
271
    GET /re
 
272
--- response_body
 
273
hello
 
274
 
 
275
 
 
276
 
 
277
=== TEST 14: single-line mode (^ as normal)
 
278
--- config
 
279
    location /re {
 
280
        content_by_lua '
 
281
            m = ngx.re.match("hello\\nworld", "^world", "s")
 
282
            if m then
 
283
                ngx.say(m[0])
 
284
            else
 
285
                ngx.say("not matched: ", m)
 
286
            end
 
287
        ';
 
288
    }
 
289
--- request
 
290
    GET /re
 
291
--- response_body
 
292
not matched: nil
 
293
 
 
294
 
 
295
 
 
296
=== TEST 15: single-line mode (dot all)
 
297
--- config
 
298
    location /re {
 
299
        content_by_lua '
 
300
            m = ngx.re.match("hello\\nworld", ".*", "s")
 
301
            if m then
 
302
                ngx.say(m[0])
 
303
            else
 
304
                ngx.say("not matched: ", m)
 
305
            end
 
306
        ';
 
307
    }
 
308
--- request
 
309
    GET /re
 
310
--- response_body
 
311
hello
 
312
world
 
313
 
 
314
 
 
315
 
 
316
=== TEST 16: extended mode (ignore whitespaces)
 
317
--- config
 
318
    location /re {
 
319
        content_by_lua '
 
320
            m = ngx.re.match("hello\\nworld", "\\\\w     \\\\w", "x")
 
321
            if m then
 
322
                ngx.say(m[0])
 
323
            else
 
324
                ngx.say("not matched: ", m)
 
325
            end
 
326
        ';
 
327
    }
 
328
--- request
 
329
    GET /re
 
330
--- response_body
 
331
he
 
332
 
 
333
 
 
334
 
 
335
=== TEST 17: bad pattern
 
336
--- config
 
337
    location /re {
 
338
        content_by_lua '
 
339
            rc, m = pcall(ngx.re.match, "hello\\nworld", "(abc")
 
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 #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
 
355
 
 
356
 
 
357
 
 
358
=== TEST 18: bad option
 
359
--- config
 
360
    location /re {
 
361
        content_by_lua '
 
362
            rc, m = pcall(ngx.re.match, "hello\\nworld", ".*", "H")
 
363
            if rc then
 
364
                if m then
 
365
                    ngx.say(m[0])
 
366
                else
 
367
                    ngx.say("not matched: ", m)
 
368
                end
 
369
            else
 
370
                ngx.say("error: ", m)
 
371
            end
 
372
        ';
 
373
    }
 
374
--- request
 
375
    GET /re
 
376
--- response_body
 
377
error: bad argument #3 to '?' (unknown flag "H")
 
378
 
 
379
 
 
380
 
 
381
=== TEST 19: extended mode (ignore whitespaces)
 
382
--- config
 
383
    location /re {
 
384
        content_by_lua '
 
385
            m = ngx.re.match("hello, world", "(world)|(hello)", "x")
 
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: ", m)
 
392
            end
 
393
        ';
 
394
    }
 
395
--- request
 
396
    GET /re
 
397
--- response_body
 
398
hello
 
399
nil
 
400
hello
 
401
 
 
402
 
 
403
 
 
404
=== TEST 20: optional trailing captures
 
405
--- config
 
406
    location /re {
 
407
        content_by_lua '
 
408
            m = ngx.re.match("hello, 1234", "([0-9]+)(h?)")
 
409
            if m then
 
410
                ngx.say(m[0])
 
411
                ngx.say(m[1])
 
412
                ngx.say(m[2])
 
413
            else
 
414
                ngx.say("not matched!")
 
415
            end
 
416
        ';
 
417
    }
 
418
--- request
 
419
    GET /re
 
420
--- response_body eval
 
421
"1234
 
422
1234
 
423
 
 
424
"
 
425
 
 
426
 
 
427
 
 
428
=== TEST 21: anchored match (failed)
 
429
--- config
 
430
    location /re {
 
431
        content_by_lua '
 
432
            m = ngx.re.match("hello, 1234", "([0-9]+)", "a")
 
433
            if m then
 
434
                ngx.say(m[0])
 
435
            else
 
436
                ngx.say("not matched!")
 
437
            end
 
438
        ';
 
439
    }
 
440
--- request
 
441
    GET /re
 
442
--- response_body
 
443
not matched!
 
444
 
 
445
 
 
446
 
 
447
=== TEST 22: anchored match (succeeded)
 
448
--- config
 
449
    location /re {
 
450
        content_by_lua '
 
451
            m = ngx.re.match("1234, hello", "([0-9]+)", "a")
 
452
            if m then
 
453
                ngx.say(m[0])
 
454
            else
 
455
                ngx.say("not matched!")
 
456
            end
 
457
        ';
 
458
    }
 
459
--- request
 
460
    GET /re
 
461
--- response_body
 
462
1234
 
463
 
 
464
 
 
465
 
 
466
=== TEST 23: match with ctx but no pos
 
467
--- config
 
468
    location /re {
 
469
        content_by_lua '
 
470
            local ctx = {}
 
471
            m = ngx.re.match("1234, hello", "([0-9]+)", "", 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
1234
 
485
4
 
486
 
 
487
 
 
488
 
 
489
=== TEST 24: match with ctx and a pos
 
490
--- config
 
491
    location /re {
 
492
        content_by_lua '
 
493
            local ctx = { pos = 2 }
 
494
            m = ngx.re.match("1234, hello", "([0-9]+)", "", ctx)
 
495
            if m then
 
496
                ngx.say(m[0])
 
497
                ngx.say(ctx.pos)
 
498
            else
 
499
                ngx.say("not matched!")
 
500
                ngx.say(ctx.pos)
 
501
            end
 
502
        ';
 
503
    }
 
504
--- request
 
505
    GET /re
 
506
--- response_body
 
507
34
 
508
4
 
509
 
 
510
 
 
511
 
 
512
=== TEST 25: sanity (set_by_lua)
 
513
--- config
 
514
    location /re {
 
515
        set_by_lua $res '
 
516
            m = ngx.re.match("hello, 1234", "([0-9]+)")
 
517
            if m then
 
518
                return m[0]
 
519
            else
 
520
                return "not matched!"
 
521
            end
 
522
        ';
 
523
        echo $res;
 
524
    }
 
525
--- request
 
526
    GET /re
 
527
--- response_body
 
528
1234
 
529
 
 
530
 
 
531
 
 
532
=== TEST 26: match (look-behind assertion)
 
533
--- config
 
534
    location /re {
 
535
        content_by_lua '
 
536
            local ctx = {}
 
537
            local m = ngx.re.match("{foobarbaz}", "(?<=foo)bar|(?<=bar)baz", "", ctx)
 
538
            ngx.say(m and m[0])
 
539
 
 
540
            m = ngx.re.match("{foobarbaz}", "(?<=foo)bar|(?<=bar)baz", "", ctx)
 
541
            ngx.say(m and m[0])
 
542
        ';
 
543
    }
 
544
--- request
 
545
    GET /re
 
546
--- response_body
 
547
bar
 
548
baz
 
549