Maxim Dounin | 9033078 | 2009-02-22 14:02:07 +0300 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # (C) Maxim Dounin |
| 4 | |
| 5 | # Tests for nginx ssi module. |
| 6 | |
| 7 | ############################################################################### |
| 8 | |
| 9 | use warnings; |
| 10 | use strict; |
| 11 | |
| 12 | use Test::More; |
| 13 | |
| 14 | BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 15 | |
| 16 | use lib 'lib'; |
| 17 | use Test::Nginx; |
| 18 | |
| 19 | ############################################################################### |
| 20 | |
| 21 | select STDERR; $| = 1; |
| 22 | select STDOUT; $| = 1; |
| 23 | |
Andrey Zelenkov | 0760b2d | 2016-08-31 18:21:07 +0300 | [diff] [blame] | 24 | my $t = Test::Nginx->new()->has(qw/http ssi cache proxy rewrite/) |
Sergey Kandaurov | ddb8c6f | 2021-06-29 11:57:39 +0300 | [diff] [blame] | 25 | ->plan(30); |
Maxim Dounin | 9033078 | 2009-02-22 14:02:07 +0300 | [diff] [blame] | 26 | |
| 27 | $t->write_file_expand('nginx.conf', <<'EOF'); |
| 28 | |
Maxim Dounin | c6acedb | 2009-10-14 02:23:52 +0400 | [diff] [blame] | 29 | %%TEST_GLOBALS%% |
| 30 | |
Maxim Dounin | 35773f6 | 2013-01-24 02:17:36 +0400 | [diff] [blame] | 31 | daemon off; |
Maxim Dounin | 9033078 | 2009-02-22 14:02:07 +0300 | [diff] [blame] | 32 | |
| 33 | events { |
| 34 | } |
| 35 | |
| 36 | http { |
Maxim Dounin | c6acedb | 2009-10-14 02:23:52 +0400 | [diff] [blame] | 37 | %%TEST_GLOBALS_HTTP%% |
Maxim Dounin | 9033078 | 2009-02-22 14:02:07 +0300 | [diff] [blame] | 38 | |
Maxim Dounin | 1e84613 | 2009-10-13 03:05:19 +0400 | [diff] [blame] | 39 | proxy_cache_path %%TESTDIR%%/cache levels=1:2 |
Maxim Dounin | cf59b89 | 2014-11-27 07:39:55 +0300 | [diff] [blame] | 40 | keys_zone=NAME:1m; |
Maxim Dounin | 1e84613 | 2009-10-13 03:05:19 +0400 | [diff] [blame] | 41 | |
Maxim Dounin | 9033078 | 2009-02-22 14:02:07 +0300 | [diff] [blame] | 42 | server { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 43 | listen 127.0.0.1:8080; |
Maxim Dounin | 9033078 | 2009-02-22 14:02:07 +0300 | [diff] [blame] | 44 | server_name localhost; |
Maxim Dounin | 0c14d4a | 2010-02-20 05:52:56 +0300 | [diff] [blame] | 45 | |
| 46 | if ($args = "found") { |
| 47 | return 204; |
| 48 | } |
| 49 | |
Maxim Dounin | 96c4e83 | 2009-05-26 17:21:59 +0400 | [diff] [blame] | 50 | location / { |
| 51 | ssi on; |
| 52 | } |
| 53 | location /proxy/ { |
| 54 | ssi on; |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 55 | proxy_pass http://127.0.0.1:8080/local/; |
Maxim Dounin | 96c4e83 | 2009-05-26 17:21:59 +0400 | [diff] [blame] | 56 | } |
Maxim Dounin | 1e84613 | 2009-10-13 03:05:19 +0400 | [diff] [blame] | 57 | location /cache/ { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 58 | proxy_pass http://127.0.0.1:8080/local/; |
Maxim Dounin | 1e84613 | 2009-10-13 03:05:19 +0400 | [diff] [blame] | 59 | proxy_cache NAME; |
| 60 | proxy_cache_valid 200 1h; |
| 61 | } |
Maxim Dounin | 96c4e83 | 2009-05-26 17:21:59 +0400 | [diff] [blame] | 62 | location /local/ { |
| 63 | ssi off; |
| 64 | alias %%TESTDIR%%/; |
| 65 | } |
Maxim Dounin | 2775b73 | 2014-11-27 07:00:10 +0300 | [diff] [blame] | 66 | location = /test-empty-postpone.html { |
| 67 | ssi on; |
| 68 | postpone_output 0; |
| 69 | } |
Sergey Kandaurov | 71721ac | 2014-01-28 16:18:04 +0400 | [diff] [blame] | 70 | location /var { |
| 71 | ssi on; |
Sergey Kandaurov | 9ac80ba | 2015-05-13 16:57:45 +0300 | [diff] [blame] | 72 | add_header X-Var x${date_gmt}x; |
Sergey Kandaurov | 71721ac | 2014-01-28 16:18:04 +0400 | [diff] [blame] | 73 | } |
| 74 | location /var_noformat { |
| 75 | ssi on; |
Sergey Kandaurov | 9ac80ba | 2015-05-13 16:57:45 +0300 | [diff] [blame] | 76 | add_header X-Var x${date_gmt}x; |
Sergey Kandaurov | 71721ac | 2014-01-28 16:18:04 +0400 | [diff] [blame] | 77 | return 200; |
| 78 | } |
| 79 | location /var_nossi { |
Sergey Kandaurov | 9ac80ba | 2015-05-13 16:57:45 +0300 | [diff] [blame] | 80 | add_header X-Var x${date_gmt}x; |
Sergey Kandaurov | 71721ac | 2014-01-28 16:18:04 +0400 | [diff] [blame] | 81 | return 200; |
| 82 | } |
Maxim Dounin | 9033078 | 2009-02-22 14:02:07 +0300 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | EOF |
| 87 | |
| 88 | $t->write_file('test1.html', 'X<!--#echo var="arg_test" -->X'); |
| 89 | $t->write_file('test2.html', |
| 90 | 'X<!--#include virtual="/test1.html?test=test" -->X'); |
| 91 | $t->write_file('test3.html', |
| 92 | 'X<!--#set var="blah" value="test" --><!--#echo var="blah" -->X'); |
Sergey Kandaurov | ddb8c6f | 2021-06-29 11:57:39 +0300 | [diff] [blame] | 93 | $t->write_file('test4-echo-none.html', |
| 94 | 'X<!--#set var="blah" value="<test>" -->' |
| 95 | . '<!--#echo var="blah" encoding="none" -->X'); |
| 96 | $t->write_file('test5-echo-url.html', |
| 97 | 'X<!--#set var="blah" value="<test>" -->' |
| 98 | . '<!--#echo var="blah" encoding="url" -->X'); |
| 99 | $t->write_file('test6-echo-entity.html', |
| 100 | 'X<!--#set var="blah" value="<test>" -->' |
| 101 | . '<!--#echo var="blah" encoding="entity" -->X'); |
Maxim Dounin | 0c14d4a | 2010-02-20 05:52:56 +0300 | [diff] [blame] | 102 | $t->write_file('test-args-rewrite.html', |
| 103 | 'X<!--#include virtual="/check?found" -->X'); |
Maxim Dounin | 8ccebf6 | 2009-10-02 18:19:43 +0400 | [diff] [blame] | 104 | $t->write_file('test-empty1.html', 'X<!--#include virtual="/empty.html" -->X'); |
| 105 | $t->write_file('test-empty2.html', |
| 106 | 'X<!--#include virtual="/local/empty.html" -->X'); |
Maxim Dounin | 1e84613 | 2009-10-13 03:05:19 +0400 | [diff] [blame] | 107 | $t->write_file('test-empty3.html', |
| 108 | 'X<!--#include virtual="/cache/empty.html" -->X'); |
Maxim Dounin | 2775b73 | 2014-11-27 07:00:10 +0300 | [diff] [blame] | 109 | $t->write_file('test-empty-postpone.html', |
| 110 | 'X<!--#include virtual="/proxy/empty.html" -->X'); |
Maxim Dounin | 8ccebf6 | 2009-10-02 18:19:43 +0400 | [diff] [blame] | 111 | $t->write_file('empty.html', ''); |
Maxim Dounin | 9033078 | 2009-02-22 14:02:07 +0300 | [diff] [blame] | 112 | |
Sergey Kandaurov | 9e3e1a9 | 2015-05-14 14:46:13 +0300 | [diff] [blame] | 113 | $t->write_file('unescape.html?', 'SEE-THIS') unless $^O eq 'MSWin32'; |
Sergey Kandaurov | 468cbf8 | 2013-12-25 12:03:40 +0400 | [diff] [blame] | 114 | $t->write_file('unescape1.html', |
| 115 | 'X<!--#include virtual="/tes%741.html?test=test" -->X'); |
| 116 | $t->write_file('unescape2.html', |
| 117 | 'X<!--#include virtual="/unescape.html%3f" -->X'); |
| 118 | $t->write_file('unescape3.html', |
| 119 | 'X<!--#include virtual="/test1.html%3ftest=test" -->X'); |
| 120 | |
Sergey Kandaurov | 71721ac | 2014-01-28 16:18:04 +0400 | [diff] [blame] | 121 | $t->write_file('var_format.html', |
| 122 | 'x<!--#if expr="$arg_custom" -->' |
| 123 | . '<!--#config timefmt="%A, %H:%M:%S" -->' |
| 124 | . '<!--#set var="v" value="$date_gmt" -->' |
| 125 | . '<!--#echo var="v" -->' |
| 126 | . '<!--#else -->' |
| 127 | . '<!--#set var="v" value="$date_gmt" -->' |
| 128 | . '<!--#echo var="v" -->' |
| 129 | . '<!--#endif -->x'); |
| 130 | |
Maxim Dounin | 9033078 | 2009-02-22 14:02:07 +0300 | [diff] [blame] | 131 | $t->run(); |
| 132 | |
| 133 | ############################################################################### |
| 134 | |
| 135 | like(http_get('/test1.html'), qr/^X\(none\)X$/m, 'echo no argument'); |
| 136 | like(http_get('/test1.html?test='), qr/^XX$/m, 'empty argument'); |
| 137 | like(http_get('/test1.html?test=test'), qr/^XtestX$/m, 'argument'); |
| 138 | like(http_get('/test1.html?test=test&a=b'), qr/^XtestX$/m, 'argument 2'); |
| 139 | like(http_get('/test1.html?a=b&test=test'), qr/^XtestX$/m, 'argument 3'); |
| 140 | like(http_get('/test1.html?a=b&test=test&d=c'), qr/^XtestX$/m, 'argument 4'); |
| 141 | like(http_get('/test1.html?atest=a&testb=b&ctestc=c&test=test'), qr/^XtestX$/m, |
| 142 | 'argument 5'); |
| 143 | |
| 144 | like(http_get('/test2.html'), qr/^XXtestXX$/m, 'argument via include'); |
| 145 | |
| 146 | like(http_get('/test3.html'), qr/^XtestX$/m, 'set'); |
| 147 | |
Sergey Kandaurov | ddb8c6f | 2021-06-29 11:57:39 +0300 | [diff] [blame] | 148 | like(http_get('/test4-echo-none.html'), qr/^X<test>X$/m, |
| 149 | 'echo encoding none'); |
| 150 | |
| 151 | TODO: { |
| 152 | local $TODO = 'no strict URI escaping yet' unless $t->has_version('1.21.1'); |
| 153 | |
| 154 | like(http_get('/test5-echo-url.html'), qr/^X%3Ctest%3EX$/m, |
| 155 | 'echo encoding url'); |
| 156 | |
| 157 | } |
| 158 | |
| 159 | like(http_get('/test6-echo-entity.html'), qr/^X<test>X$/m, |
| 160 | 'echo encoding entity'); |
| 161 | |
Maxim Dounin | 0c14d4a | 2010-02-20 05:52:56 +0300 | [diff] [blame] | 162 | # args should be in subrequest even if original request has no args and that |
| 163 | # was queried somehow (e.g. by server rewrites) |
| 164 | |
Maxim Dounin | 0c14d4a | 2010-02-20 05:52:56 +0300 | [diff] [blame] | 165 | like(http_get('/test-args-rewrite.html'), qr/^XX$/m, 'args only subrequest'); |
| 166 | |
Maxim Dounin | 0c14d4a | 2010-02-20 05:52:56 +0300 | [diff] [blame] | 167 | like(http_get('/test-args-rewrite.html?wasargs'), qr/^XX$/m, |
| 168 | 'args was in main request'); |
| 169 | |
Maxim Dounin | 96c4e83 | 2009-05-26 17:21:59 +0400 | [diff] [blame] | 170 | # Last-Modified and Accept-Ranges headers should be cleared |
| 171 | |
| 172 | unlike(http_get('/test1.html'), qr/Last-Modified|Accept-Ranges/im, |
| 173 | 'cleared headers'); |
Maxim Dounin | 96c4e83 | 2009-05-26 17:21:59 +0400 | [diff] [blame] | 174 | unlike(http_get('/proxy/test1.html'), qr/Last-Modified|Accept-Ranges/im, |
| 175 | 'cleared headers from proxy'); |
| 176 | |
Maxim Dounin | 2775b73 | 2014-11-27 07:00:10 +0300 | [diff] [blame] | 177 | # empty subrequests |
| 178 | |
Maxim Dounin | 8ccebf6 | 2009-10-02 18:19:43 +0400 | [diff] [blame] | 179 | like(http_get('/test-empty1.html'), qr/HTTP/, 'empty with ssi'); |
| 180 | like(http_get('/test-empty2.html'), qr/HTTP/, 'empty without ssi'); |
Maxim Dounin | 1e84613 | 2009-10-13 03:05:19 +0400 | [diff] [blame] | 181 | like(http_get('/test-empty3.html'), qr/HTTP/, 'empty with proxy'); |
| 182 | like(http_get('/test-empty3.html'), qr/HTTP/, 'empty with proxy cached'); |
| 183 | |
Maxim Dounin | 2775b73 | 2014-11-27 07:00:10 +0300 | [diff] [blame] | 184 | like(http_get('/test-empty-postpone.html'), qr/HTTP.*XX/ms, |
| 185 | 'empty with postpone_output 0'); |
| 186 | |
Sergey Kandaurov | 468cbf8 | 2013-12-25 12:03:40 +0400 | [diff] [blame] | 187 | # handling of escaped URIs |
| 188 | |
| 189 | like(http_get('/unescape1.html'), qr/^XXtestXX$/m, 'escaped in path'); |
Sergey Kandaurov | 9e3e1a9 | 2015-05-14 14:46:13 +0300 | [diff] [blame] | 190 | |
| 191 | SKIP: { |
| 192 | skip 'incorrect filename on win32', 2 if $^O eq 'MSWin32'; |
| 193 | |
Sergey Kandaurov | 468cbf8 | 2013-12-25 12:03:40 +0400 | [diff] [blame] | 194 | like(http_get('/unescape2.html'), qr/^XSEE-THISX$/m, |
| 195 | 'escaped question in path'); |
| 196 | like(http_get('/unescape3.html'), qr/404 Not Found/, |
| 197 | 'escaped query separator'); |
| 198 | |
Sergey Kandaurov | 9e3e1a9 | 2015-05-14 14:46:13 +0300 | [diff] [blame] | 199 | } |
| 200 | |
Sergey Kandaurov | 71721ac | 2014-01-28 16:18:04 +0400 | [diff] [blame] | 201 | # handling of embedded date variables |
| 202 | |
Sergey Kandaurov | d3f22ba | 2015-07-03 14:50:26 +0300 | [diff] [blame] | 203 | my $re_date_gmt = qr/X-Var: x.+, \d\d-.+-\d{4} \d\d:\d\d:\d\d .+x/; |
Sergey Kandaurov | 71721ac | 2014-01-28 16:18:04 +0400 | [diff] [blame] | 204 | |
Sergey Kandaurov | 9ac80ba | 2015-05-13 16:57:45 +0300 | [diff] [blame] | 205 | like(http_get('/var_nossi.html'), $re_date_gmt, 'no ssi'); |
| 206 | like(http_get('/var_noformat.html'), $re_date_gmt, 'no format'); |
| 207 | |
| 208 | like(http_get('/var_format.html?custom=1'), $re_date_gmt, 'custom header'); |
| 209 | like(http_get('/var_format.html'), $re_date_gmt, 'default header'); |
Sergey Kandaurov | 71721ac | 2014-01-28 16:18:04 +0400 | [diff] [blame] | 210 | |
Sergey Kandaurov | 71721ac | 2014-01-28 16:18:04 +0400 | [diff] [blame] | 211 | like(http_get('/var_format.html?custom=1'), |
Sergey Kandaurov | d3f22ba | 2015-07-03 14:50:26 +0300 | [diff] [blame] | 212 | qr/x.+, \d\d:\d\d:\d\dx/, 'custom ssi'); |
Sergey Kandaurov | 71721ac | 2014-01-28 16:18:04 +0400 | [diff] [blame] | 213 | like(http_get('/var_format.html'), |
Sergey Kandaurov | d3f22ba | 2015-07-03 14:50:26 +0300 | [diff] [blame] | 214 | qr/x.+, \d\d-.+-\d{4} \d\d:\d\d:\d\d .+x/, 'default ssi'); |
Sergey Kandaurov | 71721ac | 2014-01-28 16:18:04 +0400 | [diff] [blame] | 215 | |
Maxim Dounin | 9033078 | 2009-02-22 14:02:07 +0300 | [diff] [blame] | 216 | ############################################################################### |