# HG changeset patch # User Valentin Bartenev # Date 1324632566 -10800 # Node ID 1d66a6a509d085b484dc331378d241a5bcf5d59a # Parent 802fc0786165a52f218addeef35cbe22ef4c5124 Tests: SSI "if" statement tests. diff --git a/ssi_if.t b/ssi_if.t new file mode 100644 --- /dev/null +++ b/ssi_if.t @@ -0,0 +1,271 @@ +#!/usr/bin/perl + +# (C) Maxim Dounin +# (C) Valentin Bartenev + +# Tests for nginx ssi module, "if" statement. + +############################################################################### + +use warnings; +use strict; + +use Test::More; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +my $t = Test::Nginx->new()->has(qw/http ssi/)->plan(44); + +$t->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + server { + listen 127.0.0.1:8080; + server_name localhost; + location / { + ssi on; + } + } +} + +EOF + + +my $if_elif_else = + 'IF' + . 'ELIF' + . 'ELSE' + . ''; + +my $zig = 'GOOD'; +my $zag = 'GOOD'; + +foreach my $i (reverse 1 .. 15) { + if ($i % 2) { + $zig = + "$i$zig"; + $zag = + "$zag$i"; + } else { + $zig = + "$zig$i"; + $zag = + "$i$zag"; + } +} + +$t->run(); + +############################################################################### + +$t->write_file('if_var.html', 'xOKx'); + +like(http_get('/if_var.html?v=1'), qr/^xOKx$/m, 'if variable exists'); +like(http_get('/if_var.html'), qr/^xx$/m, 'if variable not exists'); + + +$t->write_file('if_eq.html', + 'xOKx'); + +like(http_get('/if_eq.html?v=equal'), qr/^xOKx$/m, 'if var = text'); +like(http_get('/if_eq.html?v=notequal'), qr/^xx$/m, 'if var = text (false)'); + + +$t->write_file('if_neq.html', + 'xOKx'); + +like(http_get('/if_neq.html?v=notequal'), qr/^xOKx$/m, 'if text != var'); +like(http_get('/if_neq.html?v=equal'), qr/^xx$/m, 'if text != var (false)'); + + +$t->write_file('if_eq_re.html', + 'xOKx'); + +like(http_get('/if_eq_re.html?v=XreeeegexX'), qr/^xOKx$/m, 'if var = /regex/'); +like(http_get('/if_eq_re.html?v=XrgxX'), qr/^xx$/m, 'if var = /regex/ (false)'); + + +$t->write_file('if_neq_re.html', + 'xOKx'); + +like(http_get('/if_neq_re.html?v=XrgxX'), qr/^xOKx$/m, 'if var != /regex/'); +like(http_get('/if_neq_re.html?v=XreeeegexX'), qr/^xx$/m, + 'if var != /regex/ (false)'); + + +$t->write_file('if_varvar.html', + 'xOKx'); + +like(http_get('/if_varvar.html?v=varHERE&v2=HERE'), qr/^xOKx$/m, + 'if var = complex'); + + +TODO: { +local $TODO = 'support for captures in regexps'; + +$t->write_file('if_cap_re.html', + 'x' + . 'x' + . 'x'); + +like(http_get('/if_cap_re.html?v=hereCAP1andCAP2'), qr/^xCAP1xCAP2x$/m, + 'if regex with captures'); + + +$t->write_file('if_ncap_re.html', + 'x' + . '' + . 'x'); + +like(http_get('/if_ncap_re.html?v=captureHEREeee'), qr/^xHEREx$/m, + 'if regex with named capture'); + +} + + +$t->write_file('if.html', 'x' . $if_elif_else . 'x'); + +like(http_get('/if.html?if=1'), qr/^xIFx$/m, 'if'); +like(http_get('/if.html?if=1&elif=1'), qr/^xIFx$/m, 'if suppresses elif'); +like(http_get('/if.html?elif=1'), qr/^xELIFx$/m, 'elif'); +like(http_get('/if.html'), qr/^xELSEx$/m, 'else'); + + +$t->write_file('if_multi.html', + 'xIF1ELSE1' + . 'xIF2ELSE2' + . 'xIF3ELSE3' + . 'xIF4ELSE4' + . 'xIF5ELSE5x'); + +like(http_get('/if_multi.html?1=t&2=t&3=t&4=t&5=t'), + qr/^xIF1xIF2xIF3xIF4xIF5x$/m, 'multiple if (sequentially)'); +like(http_get('/if_multi.html?1=t&3=t&5=t'), qr/^xIF1xELSE2xIF3xELSE4xIF5x$/m, + 'multiple if (interlaced)'); +like(http_get('/if_multi.html?2=t&4=t'), qr/^xELSE1xIF2xELSE3xIF4xELSE5x$/m, + 'multiple if (interlaced reversed)'); + + +TODO: { +local $TODO = 'fix "if" conditions inside the "block" command'; + +$t->write_file('if_in_block.html', + '' . $if_elif_else . '' + . 'xx'); + +like(http_get('/if_in_block.html?if=1'), qr/^xIFx$/m, 'if (in block)'); +like(http_get('/if_in_block.html?if=1&elif=1'), qr/^xIFx$/m, + 'if suppresses elif (in block)'); +like(http_get('/if_in_block.html?elif=1'), qr/^xELIFx$/m, 'elif (in block)'); +like(http_get('/if_in_block.html'), qr/^xELSEx$/m, 'else (in block)'); + +} + + +$t->write_file('if_config_set_echo.html', + 'x' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . 'x'); + +like(http_get('/if_config_set_echo.html?if=1'), qr/^xIFx$/m, + 'if config-set-echo'); +like(http_get('/if_config_set_echo.html'), qr/^xELSEx$/m, + 'else config-set-echo'); + + +$t->write_file('if_include.html', + 'x' + . '' + . '' + . '' + . 'x'); + +like(http_get('/if_include.html?if=1'), qr/^xxIFxx$/m, + 'if include'); +like(http_get('/if_include.html'), qr/^xxELSExx$/m, + 'else include'); + + +$t->write_file('if_block.html', + '' + . 'IF' + . '' + . 'ELSE' + . '' + . 'xx'); + +like(http_get('/if_block.html?if=1'), qr/^xIFx$/m, 'if block'); +like(http_get('/if_block.html'), qr/^xELSEx$/m, 'else block'); + + +TODO: { +local $TODO = 'support for nested ifs'; + +$t->write_file('ifif.html', + 'xIFx' . $if_elif_else + . 'ELIFx' . $if_elif_else + . 'ELSEx' . $if_elif_else + . 'x'); + +like(http_get('/ifif.html?_if=1&if=1'), qr/^xIFxIFx$/m, 'if if'); +like(http_get('/ifif.html?_if=1&elif=1'), qr/^xIFxELIFx$/m, 'if elif'); +like(http_get('/ifif.html?_if=1'), qr/^xIFxELSEx$/m, 'if else'); + +like(http_get('/ifif.html?_elif=1&if=1'), qr/^xELIFxIFx$/m, 'elif if'); +like(http_get('/ifif.html?_elif=1&elif=1'), qr/^xELIFxELIFx$/m, 'elif elif'); +like(http_get('/ifif.html?_elif=1'), qr/^xELIFxELSEx$/m, 'elif else'); + +like(http_get('/ifif.html?if=1'), qr/^xELSExIFx$/m, 'else if'); +like(http_get('/ifif.html?elif=1'), qr/^xELSExELIFx$/m, 'else elif'); +like(http_get('/ifif.html'), qr/^xELSExELSEx$/m, 'else else'); + + +$t->write_file('zigzag.html', + "x$zig$zagx"); + +like(http_get('/zigzag.html?0=t&2=t&4=t&6=t&8=t&10=t&12=t&14=t'), + qr/^xGOODx$/m, 'zigzag'); +like(http_get('/zigzag.html?1=t&3=t&5=t&7=t&9=t&11=t&13=t&15=t'), + qr/^xGOODx$/m, 'zagzig'); + + +$t->write_file('zigzag_block.html', + '' + . "x$zig$zagx" + . '' + . 'xx'); + +like(http_get('/zigzag_block.html?0=t&2=t&4=t&6=t&8=t&10=t&12=t&14=t'), + qr/^xGOODx$/m, 'zigzag block'); +like(http_get('/zigzag_block.html?1=t&3=t&5=t&7=t&9=t&11=t&13=t&15=t'), + qr/^xGOODx$/m, 'zagzig block'); + +} + + +like(`grep -F '[alert]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no alerts'); + +###############################################################################