~ubuntu-branches/ubuntu/quantal/nginx/quantal-updates

« back to all changes in this revision

Viewing changes to debian/modules/nginx-lua/t/023-rewrite/sanity.t

  • Committer: Bazaar Package Importer
  • Author(s): Kartik Mistry
  • Date: 2011-04-16 13:47:58 UTC
  • mfrom: (4.2.31 sid)
  • Revision ID: james.westby@ubuntu.com-20110416134758-yqca2qp5crh2hw2f
Tags: 1.0.0-2
* debian/rules:
  + Removed --with-file-aio support. Fixed FTBFS on kFreeBSD-* arch
    (Closes: #621882)

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_process_enabled(1);
 
7
#log_level('warn');
 
8
#no_nginx_manager();
 
9
 
 
10
repeat_each(2);
 
11
 
 
12
plan tests => repeat_each() * (blocks() * 2 + 7);
 
13
 
 
14
#no_diff();
 
15
#no_long_string();
 
16
run_tests();
 
17
 
 
18
__DATA__
 
19
 
 
20
=== TEST 1: basic print
 
21
--- config
 
22
    location /lua {
 
23
        # NOTE: the newline escape sequence must be double-escaped, as nginx config
 
24
        # parser will unescape first!
 
25
        rewrite_by_lua 'ngx.print("Hello, Lua!\\n")';
 
26
        content_by_lua return;
 
27
    }
 
28
--- request
 
29
GET /lua
 
30
--- response_body
 
31
Hello, Lua!
 
32
 
 
33
 
 
34
 
 
35
=== TEST 2: basic say
 
36
--- config
 
37
    location /say {
 
38
        # NOTE: the newline escape sequence must be double-escaped, as nginx config
 
39
        # parser will unescape first!
 
40
        rewrite_by_lua '
 
41
            ngx.say("Hello, Lua!")
 
42
            ngx.say("Yay! ", 123)';
 
43
 
 
44
        content_by_lua 'ngx.exit(ngx.OK)';
 
45
    }
 
46
--- request
 
47
GET /say
 
48
--- response_body
 
49
Hello, Lua!
 
50
Yay! 123
 
51
 
 
52
 
 
53
 
 
54
=== TEST 3: no ngx.echo
 
55
--- config
 
56
    location /lua {
 
57
        rewrite_by_lua 'ngx.echo("Hello, Lua!\\n")';
 
58
        content_by_lua 'ngx.exit(ngx.OK)';
 
59
    }
 
60
--- request
 
61
GET /lua
 
62
--- response_body_like: 500 Internal Server Error
 
63
--- error_code: 500
 
64
 
 
65
 
 
66
 
 
67
=== TEST 4: variable
 
68
--- config
 
69
    location /lua {
 
70
        # NOTE: the newline escape sequence must be double-escaped, as nginx config
 
71
        # parser will unescape first!
 
72
        rewrite_by_lua 'v = ngx.var["request_uri"] ngx.print("request_uri: ", v, "\\n")';
 
73
        content_by_lua 'ngx.exit(ngx.OK)';
 
74
    }
 
75
--- request
 
76
GET /lua?a=1&b=2
 
77
--- response_body
 
78
request_uri: /lua?a=1&b=2
 
79
 
 
80
 
 
81
 
 
82
=== TEST 5: variable (file)
 
83
--- config
 
84
    location /lua {
 
85
        rewrite_by_lua_file html/test.lua;
 
86
        content_by_lua 'ngx.exit(ngx.OK)';
 
87
    }
 
88
--- user_files
 
89
>>> test.lua
 
90
v = ngx.var["request_uri"]
 
91
ngx.print("request_uri: ", v, "\n")
 
92
--- request
 
93
GET /lua?a=1&b=2
 
94
--- response_body
 
95
request_uri: /lua?a=1&b=2
 
96
 
 
97
 
 
98
 
 
99
=== TEST 6: calc expression
 
100
--- config
 
101
    location /lua {
 
102
        rewrite_by_lua_file html/calc.lua;
 
103
        content_by_lua 'ngx.exit(ngx.OK)';
 
104
    }
 
105
--- user_files
 
106
>>> calc.lua
 
107
local function uri_unescape(uri)
 
108
    local function convert(hex)
 
109
        return string.char(tonumber("0x"..hex))
 
110
    end
 
111
    local s = string.gsub(uri, "%%([0-9a-fA-F][0-9a-fA-F])", convert)
 
112
    return s
 
113
end
 
114
 
 
115
local function eval_exp(str)
 
116
    return loadstring("return "..str)()
 
117
end
 
118
 
 
119
local exp_str = ngx.var["arg_exp"]
 
120
-- print("exp: '", exp_str, "'\n")
 
121
local status, res
 
122
status, res = pcall(uri_unescape, exp_str)
 
123
if not status then
 
124
    ngx.print("error: ", res, "\n")
 
125
    return
 
126
end
 
127
status, res = pcall(eval_exp, res)
 
128
if status then
 
129
    ngx.print("result: ", res, "\n")
 
130
else
 
131
    ngx.print("error: ", res, "\n")
 
132
end
 
133
--- request
 
134
GET /lua?exp=1%2B2*math.sin(3)%2Fmath.exp(4)-math.sqrt(2)
 
135
--- response_body
 
136
result: -0.4090441561579
 
137
 
 
138
 
 
139
 
 
140
=== TEST 7: read $arg_xxx
 
141
--- config
 
142
    location = /lua {
 
143
        rewrite_by_lua 'who = ngx.var.arg_who
 
144
            ngx.print("Hello, ", who, "!")';
 
145
        content_by_lua 'ngx.exit(ngx.OK)';
 
146
    }
 
147
--- request
 
148
GET /lua?who=agentzh
 
149
--- response_body chomp
 
150
Hello, agentzh!
 
151
 
 
152
 
 
153
 
 
154
=== TEST 8: capture location
 
155
--- config
 
156
    location /other {
 
157
        echo "hello, world";
 
158
    }
 
159
 
 
160
    location /lua {
 
161
        rewrite_by_lua '
 
162
res = ngx.location.capture("/other")
 
163
ngx.print("status=", res.status, " ")
 
164
ngx.print("body=", res.body)
 
165
';
 
166
        content_by_lua 'ngx.exit(ngx.OK)';
 
167
    }
 
168
--- request
 
169
GET /lua
 
170
--- response_body
 
171
status=200 body=hello, world
 
172
 
 
173
 
 
174
 
 
175
=== TEST 9: capture non-existed location
 
176
--- config
 
177
    location /lua {
 
178
        rewrite_by_lua 'res = ngx.location.capture("/other"); ngx.print("status=", res.status)';
 
179
        content_by_lua 'ngx.exit(ngx.OK)';
 
180
    }
 
181
--- request
 
182
GET /lua
 
183
--- response_body: status=404
 
184
 
 
185
 
 
186
 
 
187
=== TEST 10: invalid capture location (not as expected...)
 
188
--- config
 
189
    location /lua {
 
190
        rewrite_by_lua 'res = ngx.location.capture("*(#*"); ngx.say("res=", res.status)';
 
191
        content_by_lua 'ngx.exit(ngx.OK)';
 
192
    }
 
193
--- request
 
194
GET /lua
 
195
--- response_body
 
196
res=404
 
197
 
 
198
 
 
199
 
 
200
=== TEST 11: nil is "nil"
 
201
--- config
 
202
    location /lua {
 
203
        rewrite_by_lua 'ngx.print(nil)';
 
204
        content_by_lua 'ngx.exit(ngx.OK)';
 
205
    }
 
206
--- request
 
207
GET /lua
 
208
--- response_body_like: 500 Internal Server Error
 
209
--- error_code: 500
 
210
 
 
211
 
 
212
 
 
213
=== TEST 12: bad argument type to ngx.location.capture
 
214
--- config
 
215
    location /lua {
 
216
        rewrite_by_lua 'ngx.location.capture(nil)';
 
217
        content_by_lua 'ngx.exit(ngx.OK)';
 
218
    }
 
219
--- request
 
220
GET /lua
 
221
--- response_body_like: 500 Internal Server Error
 
222
--- error_code: 500
 
223
 
 
224
 
 
225
 
 
226
=== TEST 13: capture location (default 0);
 
227
--- config
 
228
 location /recur {
 
229
       rewrite_by_lua '
 
230
           local num = tonumber(ngx.var.arg_num) or 0;
 
231
           ngx.print("num is: ", num, "\\n");
 
232
 
 
233
           if (num > 0) then
 
234
               res = ngx.location.capture("/recur?num="..tostring(num - 1));
 
235
               ngx.print("status=", res.status, " ");
 
236
               ngx.print("body=", res.body, "\\n");
 
237
           else
 
238
               ngx.print("end\\n");
 
239
           end
 
240
           ';
 
241
 
 
242
           content_by_lua 'ngx.exit(ngx.OK)';
 
243
   }
 
244
--- request
 
245
GET /recur
 
246
--- response_body
 
247
num is: 0
 
248
end
 
249
 
 
250
 
 
251
 
 
252
=== TEST 14: capture location
 
253
--- config
 
254
 location /recur {
 
255
       rewrite_by_lua '
 
256
           local num = tonumber(ngx.var.arg_num) or 0;
 
257
           ngx.print("num is: ", num, "\\n");
 
258
 
 
259
           if (num > 0) then
 
260
               res = ngx.location.capture("/recur?num="..tostring(num - 1));
 
261
               ngx.print("status=", res.status, " ");
 
262
               ngx.print("body=", res.body);
 
263
           else
 
264
               ngx.print("end\\n");
 
265
           end
 
266
           ';
 
267
 
 
268
           content_by_lua 'ngx.exit(ngx.OK)';
 
269
   }
 
270
--- request
 
271
GET /recur?num=3
 
272
--- response_body
 
273
num is: 3
 
274
status=200 body=num is: 2
 
275
status=200 body=num is: 1
 
276
status=200 body=num is: 0
 
277
end
 
278
 
 
279
 
 
280
 
 
281
=== TEST 15: setting nginx variables from within Lua
 
282
--- config
 
283
 location /set {
 
284
       set $a "";
 
285
       rewrite_by_lua 'ngx.var.a = 32; ngx.say(ngx.var.a)';
 
286
       content_by_lua 'ngx.exit(ngx.OK)';
 
287
       add_header Foo $a;
 
288
   }
 
289
--- request
 
290
GET /set
 
291
--- response_headers
 
292
Foo: 32
 
293
--- response_body
 
294
32
 
295
 
 
296
 
 
297
 
 
298
=== TEST 16: nginx quote sql string 1
 
299
--- config
 
300
 location /set {
 
301
       set $a 'hello\n\r\'"\\'; # this runs after rewrite_by_lua
 
302
       rewrite_by_lua 'ngx.say(ngx.quote_sql_str(ngx.var.a))';
 
303
       content_by_lua 'ngx.exit(ngx.OK)';
 
304
   }
 
305
--- request
 
306
GET /set
 
307
--- response_body
 
308
'hello\n\r\'\"\\'
 
309
 
 
310
 
 
311
 
 
312
=== TEST 17: nginx quote sql string 2
 
313
--- config
 
314
location /set {
 
315
    #set $a "hello\n\r'\"\\";
 
316
    rewrite_by_lua 'ngx.say(ngx.quote_sql_str("hello\\n\\r\'\\"\\\\"))';
 
317
    content_by_lua 'ngx.exit(ngx.OK)';
 
318
}
 
319
--- request
 
320
GET /set
 
321
--- response_body
 
322
'hello\n\r\'\"\\'
 
323
 
 
324
 
 
325
 
 
326
=== TEST 18: use dollar
 
327
--- config
 
328
location /set {
 
329
    rewrite_by_lua '
 
330
        local s = "hello 112";
 
331
        ngx.say(string.find(s, "%d+$"))';
 
332
 
 
333
    content_by_lua 'ngx.exit(ngx.OK)';
 
334
}
 
335
--- request
 
336
GET /set
 
337
--- response_body
 
338
79
 
339
 
 
340
 
 
341
 
 
342
=== TEST 19: subrequests do not share variables of main requests by default
 
343
--- config
 
344
location /sub {
 
345
    echo $a;
 
346
}
 
347
location /parent {
 
348
    set $a 12;
 
349
    rewrite_by_lua 'res = ngx.location.capture("/sub"); ngx.print(res.body)';
 
350
    content_by_lua 'ngx.exit(ngx.OK)';
 
351
}
 
352
--- request
 
353
GET /parent
 
354
--- response_body eval: "\n"
 
355
 
 
356
 
 
357
 
 
358
=== TEST 20: subrequests can share variables of main requests
 
359
--- config
 
360
location /sub {
 
361
    echo $a;
 
362
}
 
363
location /parent {
 
364
    set $a '';
 
365
    rewrite_by_lua '
 
366
        ngx.var.a = 12;
 
367
        res = ngx.location.capture(
 
368
            "/sub",
 
369
            { share_all_vars = true }
 
370
        );
 
371
        ngx.print(res.body)
 
372
    ';
 
373
    content_by_lua 'ngx.exit(ngx.OK)';
 
374
}
 
375
--- request
 
376
GET /parent
 
377
--- response_body
 
378
12
 
379
 
 
380
 
 
381
 
 
382
=== TEST 21: main requests use subrequests' variables
 
383
--- config
 
384
location /sub {
 
385
    set $a 12;
 
386
}
 
387
location /parent {
 
388
    rewrite_by_lua '
 
389
        res = ngx.location.capture("/sub", { share_all_vars = true });
 
390
        ngx.say(ngx.var.a)
 
391
    ';
 
392
 
 
393
    content_by_lua 'ngx.exit(ngx.OK)';
 
394
}
 
395
--- request
 
396
GET /parent
 
397
--- response_body
 
398
12
 
399
 
 
400
 
 
401
 
 
402
=== TEST 22: main requests do NOT use subrequests' variables
 
403
--- config
 
404
location /sub {
 
405
    set $a 12;
 
406
    content_by_lua return;
 
407
}
 
408
 
 
409
location /parent {
 
410
    rewrite_by_lua '
 
411
        res = ngx.location.capture("/sub", { share_all_vars = false });
 
412
        ngx.say(ngx.var.a)
 
413
    ';
 
414
    content_by_lua return;
 
415
}
 
416
--- request
 
417
GET /parent
 
418
--- response_body_like eval: "\n"
 
419
 
 
420
 
 
421
 
 
422
=== TEST 23: capture location headers
 
423
--- config
 
424
    location /other {
 
425
        default_type 'foo/bar';
 
426
        echo "hello, world";
 
427
    }
 
428
 
 
429
    location /lua {
 
430
        rewrite_by_lua '
 
431
            res = ngx.location.capture("/other");
 
432
            ngx.say("type: ", res.header["Content-Type"]);
 
433
        ';
 
434
 
 
435
        content_by_lua 'ngx.exit(ngx.OK)';
 
436
    }
 
437
--- request
 
438
GET /lua
 
439
--- response_body
 
440
type: foo/bar
 
441
 
 
442
 
 
443
 
 
444
=== TEST 24: capture location headers
 
445
--- config
 
446
    location /other {
 
447
        default_type 'foo/bar';
 
448
        rewrite_by_lua '
 
449
            ngx.header.Bar = "Bah";
 
450
        ';
 
451
        content_by_lua 'ngx.exit(ngx.OK)';
 
452
    }
 
453
 
 
454
    location /lua {
 
455
        rewrite_by_lua '
 
456
            res = ngx.location.capture("/other");
 
457
            ngx.say("type: ", res.header["Content-Type"]);
 
458
            ngx.say("Bar: ", res.header["Bar"]);
 
459
        ';
 
460
 
 
461
        content_by_lua 'ngx.exit(ngx.OK)';
 
462
    }
 
463
--- request
 
464
GET /lua
 
465
--- response_body
 
466
type: foo/bar
 
467
Bar: Bah
 
468
 
 
469
 
 
470
 
 
471
=== TEST 25: capture location headers
 
472
--- config
 
473
    location /other {
 
474
        default_type 'foo/bar';
 
475
        rewrite_by_lua '
 
476
            ngx.header.Bar = "Bah";
 
477
            ngx.header.Bar = nil;
 
478
        ';
 
479
        content_by_lua 'ngx.exit(ngx.OK)';
 
480
    }
 
481
 
 
482
    location /lua {
 
483
        rewrite_by_lua '
 
484
            res = ngx.location.capture("/other");
 
485
            ngx.say("type: ", res.header["Content-Type"]);
 
486
            ngx.say("Bar: ", res.header["Bar"] or "nil");
 
487
        ';
 
488
        content_by_lua 'ngx.exit(ngx.OK)';
 
489
    }
 
490
--- request
 
491
GET /lua
 
492
--- response_body
 
493
type: foo/bar
 
494
Bar: nil
 
495
 
 
496
 
 
497
 
 
498
=== TEST 26: rewrite_by_lua runs before ngx_access
 
499
--- config
 
500
    location /lua {
 
501
        deny all;
 
502
 
 
503
        rewrite_by_lua '
 
504
            ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
 
505
        ';
 
506
 
 
507
        content_by_lua return;
 
508
    }
 
509
--- request
 
510
GET /lua
 
511
--- response_body_like: 500 Internal Server Error
 
512
--- error_code: 500
 
513
 
 
514
 
 
515
 
 
516
=== TEST 27: rewrite_by_lua shouldn't send headers automatically (on simple return)
 
517
--- config
 
518
    location /lua {
 
519
        rewrite_by_lua 'return';
 
520
 
 
521
        proxy_pass http://127.0.0.1:$server_port/foo;
 
522
    }
 
523
 
 
524
    location = /foo {
 
525
        default_type 'text/css';
 
526
        add_header Bar Baz;
 
527
        echo foo;
 
528
    }
 
529
--- request
 
530
GET /lua
 
531
--- response_headers
 
532
Bar: Baz
 
533
Content-Type: text/css
 
534
--- response_body
 
535
foo
 
536
 
 
537
 
 
538
 
 
539
=== TEST 28: rewrite_by_lua shouldn't send headers automatically (on simple exit)
 
540
--- config
 
541
    location /lua {
 
542
        rewrite_by_lua 'ngx.exit(ngx.OK)';
 
543
 
 
544
        proxy_pass http://127.0.0.1:$server_port/foo;
 
545
    }
 
546
 
 
547
    location = /foo {
 
548
        default_type 'text/css';
 
549
        add_header Bar Baz;
 
550
        echo foo;
 
551
    }
 
552
--- request
 
553
GET /lua
 
554
--- response_headers
 
555
Bar: Baz
 
556
Content-Type: text/css
 
557
--- response_body
 
558
foo
 
559
 
 
560
 
 
561
 
 
562
=== TEST 29: short circuit
 
563
--- config
 
564
    location /lua {
 
565
        rewrite_by_lua '
 
566
            ngx.say("Hi")
 
567
            ngx.eof()
 
568
            ngx.exit(ngx.HTTP_OK)
 
569
        ';
 
570
 
 
571
        content_by_lua '
 
572
            print("HERE")
 
573
            ngx.print("BAD")
 
574
        ';
 
575
    }
 
576
--- request
 
577
GET /lua
 
578
--- response_body
 
579
Hi
 
580
 
 
581
 
 
582
 
 
583
=== TEST 30: nginx vars in script path
 
584
--- config
 
585
    location ~ /lua/(.+)$ {
 
586
        rewrite_by_lua_file html/$1.lua;
 
587
 
 
588
        content_by_lua '
 
589
            print("HERE")
 
590
            ngx.print("BAD")
 
591
        ';
 
592
    }
 
593
--- user_files
 
594
>>> hi.lua
 
595
ngx.say("Hi")
 
596
ngx.eof()
 
597
ngx.exit(ngx.HTTP_OK)
 
598
--- request
 
599
GET /lua/hi
 
600
--- response_body
 
601
Hi
 
602
 
 
603
 
 
604
 
 
605
=== TEST 31: phase postponing works for various locations
 
606
--- config
 
607
    location ~ '^/lua/(.+)' {
 
608
        set $path $1;
 
609
        rewrite_by_lua 'ngx.say(ngx.var.path)';
 
610
        content_by_lua return;
 
611
    }
 
612
    location ~ '^/lua2/(.+)' {
 
613
        set $path $1;
 
614
        rewrite_by_lua 'ngx.say(ngx.var.path)';
 
615
        content_by_lua return;
 
616
    }
 
617
    location /main {
 
618
        echo_location /lua/foo;
 
619
        echo_location /lua/bar;
 
620
        echo_location /lua2/baz;
 
621
        echo_location /lua2/bah;
 
622
    }
 
623
--- request
 
624
GET /main
 
625
--- response_body
 
626
foo
 
627
bar
 
628
baz
 
629
bah
 
630
 
 
631
 
 
632
=== TEST 33: server rewrite_by_lua
 
633
--- config
 
634
    rewrite_by_lua 'ngx.header["X-Foo"] = "bar" ngx.send_headers()';
 
635
--- request
 
636
GET /
 
637
--- response_body chop
 
638
<html><head><title>It works!</title></head><body>It works!</body></html>
 
639
--- response_headers
 
640
X-Foo: bar
 
641
 
 
642
 
 
643
 
 
644
=== TEST 34: server rewrite_by_lua_file
 
645
--- config
 
646
    rewrite_by_lua_file html/foo.lua;
 
647
--- user_files
 
648
>>> foo.lua
 
649
ngx.header["X-Foo"] = "bar" ngx.send_headers()
 
650
--- request
 
651
GET /
 
652
--- response_body chop
 
653
<html><head><title>It works!</title></head><body>It works!</body></html>
 
654
--- response_headers
 
655
X-Foo: bar
 
656