blob: 621af74d45b4a0ee4a6708ed2fc55d57878a93fb [file] [log] [blame]
Maxim Dounin90330782009-02-22 14:02:07 +03001#!/usr/bin/perl
2
3# (C) Maxim Dounin
4
5# Tests for nginx ssi module.
6
7###############################################################################
8
9use warnings;
10use strict;
11
12use Test::More;
13
14BEGIN { use FindBin; chdir($FindBin::Bin); }
15
16use lib 'lib';
17use Test::Nginx;
18
19###############################################################################
20
21select STDERR; $| = 1;
22select STDOUT; $| = 1;
23
Andrey Zelenkov0760b2d2016-08-31 18:21:07 +030024my $t = Test::Nginx->new()->has(qw/http ssi cache proxy rewrite/)
Sergey Kandaurovddb8c6f2021-06-29 11:57:39 +030025 ->plan(30);
Maxim Dounin90330782009-02-22 14:02:07 +030026
27$t->write_file_expand('nginx.conf', <<'EOF');
28
Maxim Douninc6acedb2009-10-14 02:23:52 +040029%%TEST_GLOBALS%%
30
Maxim Dounin35773f62013-01-24 02:17:36 +040031daemon off;
Maxim Dounin90330782009-02-22 14:02:07 +030032
33events {
34}
35
36http {
Maxim Douninc6acedb2009-10-14 02:23:52 +040037 %%TEST_GLOBALS_HTTP%%
Maxim Dounin90330782009-02-22 14:02:07 +030038
Maxim Dounin1e846132009-10-13 03:05:19 +040039 proxy_cache_path %%TESTDIR%%/cache levels=1:2
Maxim Dounincf59b892014-11-27 07:39:55 +030040 keys_zone=NAME:1m;
Maxim Dounin1e846132009-10-13 03:05:19 +040041
Maxim Dounin90330782009-02-22 14:02:07 +030042 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030043 listen 127.0.0.1:8080;
Maxim Dounin90330782009-02-22 14:02:07 +030044 server_name localhost;
Maxim Dounin0c14d4a2010-02-20 05:52:56 +030045
46 if ($args = "found") {
47 return 204;
48 }
49
Maxim Dounin96c4e832009-05-26 17:21:59 +040050 location / {
51 ssi on;
52 }
53 location /proxy/ {
54 ssi on;
Andrey Zelenkove59bf362016-07-12 17:39:03 +030055 proxy_pass http://127.0.0.1:8080/local/;
Maxim Dounin96c4e832009-05-26 17:21:59 +040056 }
Maxim Dounin1e846132009-10-13 03:05:19 +040057 location /cache/ {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030058 proxy_pass http://127.0.0.1:8080/local/;
Maxim Dounin1e846132009-10-13 03:05:19 +040059 proxy_cache NAME;
60 proxy_cache_valid 200 1h;
61 }
Maxim Dounin96c4e832009-05-26 17:21:59 +040062 location /local/ {
63 ssi off;
64 alias %%TESTDIR%%/;
65 }
Maxim Dounin2775b732014-11-27 07:00:10 +030066 location = /test-empty-postpone.html {
67 ssi on;
68 postpone_output 0;
69 }
Sergey Kandaurov71721ac2014-01-28 16:18:04 +040070 location /var {
71 ssi on;
Sergey Kandaurov9ac80ba2015-05-13 16:57:45 +030072 add_header X-Var x${date_gmt}x;
Sergey Kandaurov71721ac2014-01-28 16:18:04 +040073 }
74 location /var_noformat {
75 ssi on;
Sergey Kandaurov9ac80ba2015-05-13 16:57:45 +030076 add_header X-Var x${date_gmt}x;
Sergey Kandaurov71721ac2014-01-28 16:18:04 +040077 return 200;
78 }
79 location /var_nossi {
Sergey Kandaurov9ac80ba2015-05-13 16:57:45 +030080 add_header X-Var x${date_gmt}x;
Sergey Kandaurov71721ac2014-01-28 16:18:04 +040081 return 200;
82 }
Maxim Dounin90330782009-02-22 14:02:07 +030083 }
84}
85
86EOF
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 Kandaurovddb8c6f2021-06-29 11:57:39 +030093$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 Dounin0c14d4a2010-02-20 05:52:56 +0300102$t->write_file('test-args-rewrite.html',
103 'X<!--#include virtual="/check?found" -->X');
Maxim Dounin8ccebf62009-10-02 18:19:43 +0400104$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 Dounin1e846132009-10-13 03:05:19 +0400107$t->write_file('test-empty3.html',
108 'X<!--#include virtual="/cache/empty.html" -->X');
Maxim Dounin2775b732014-11-27 07:00:10 +0300109$t->write_file('test-empty-postpone.html',
110 'X<!--#include virtual="/proxy/empty.html" -->X');
Maxim Dounin8ccebf62009-10-02 18:19:43 +0400111$t->write_file('empty.html', '');
Maxim Dounin90330782009-02-22 14:02:07 +0300112
Sergey Kandaurov9e3e1a92015-05-14 14:46:13 +0300113$t->write_file('unescape.html?', 'SEE-THIS') unless $^O eq 'MSWin32';
Sergey Kandaurov468cbf82013-12-25 12:03:40 +0400114$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 Kandaurov71721ac2014-01-28 16:18:04 +0400121$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 Dounin90330782009-02-22 14:02:07 +0300131$t->run();
132
133###############################################################################
134
135like(http_get('/test1.html'), qr/^X\(none\)X$/m, 'echo no argument');
136like(http_get('/test1.html?test='), qr/^XX$/m, 'empty argument');
137like(http_get('/test1.html?test=test'), qr/^XtestX$/m, 'argument');
138like(http_get('/test1.html?test=test&a=b'), qr/^XtestX$/m, 'argument 2');
139like(http_get('/test1.html?a=b&test=test'), qr/^XtestX$/m, 'argument 3');
140like(http_get('/test1.html?a=b&test=test&d=c'), qr/^XtestX$/m, 'argument 4');
141like(http_get('/test1.html?atest=a&testb=b&ctestc=c&test=test'), qr/^XtestX$/m,
142 'argument 5');
143
144like(http_get('/test2.html'), qr/^XXtestXX$/m, 'argument via include');
145
146like(http_get('/test3.html'), qr/^XtestX$/m, 'set');
147
Sergey Kandaurovddb8c6f2021-06-29 11:57:39 +0300148like(http_get('/test4-echo-none.html'), qr/^X<test>X$/m,
149 'echo encoding none');
150
151TODO: {
152local $TODO = 'no strict URI escaping yet' unless $t->has_version('1.21.1');
153
154like(http_get('/test5-echo-url.html'), qr/^X%3Ctest%3EX$/m,
155 'echo encoding url');
156
157}
158
159like(http_get('/test6-echo-entity.html'), qr/^X&lt;test&gt;X$/m,
160 'echo encoding entity');
161
Maxim Dounin0c14d4a2010-02-20 05:52:56 +0300162# 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 Dounin0c14d4a2010-02-20 05:52:56 +0300165like(http_get('/test-args-rewrite.html'), qr/^XX$/m, 'args only subrequest');
166
Maxim Dounin0c14d4a2010-02-20 05:52:56 +0300167like(http_get('/test-args-rewrite.html?wasargs'), qr/^XX$/m,
168 'args was in main request');
169
Maxim Dounin96c4e832009-05-26 17:21:59 +0400170# Last-Modified and Accept-Ranges headers should be cleared
171
172unlike(http_get('/test1.html'), qr/Last-Modified|Accept-Ranges/im,
173 'cleared headers');
Maxim Dounin96c4e832009-05-26 17:21:59 +0400174unlike(http_get('/proxy/test1.html'), qr/Last-Modified|Accept-Ranges/im,
175 'cleared headers from proxy');
176
Maxim Dounin2775b732014-11-27 07:00:10 +0300177# empty subrequests
178
Maxim Dounin8ccebf62009-10-02 18:19:43 +0400179like(http_get('/test-empty1.html'), qr/HTTP/, 'empty with ssi');
180like(http_get('/test-empty2.html'), qr/HTTP/, 'empty without ssi');
Maxim Dounin1e846132009-10-13 03:05:19 +0400181like(http_get('/test-empty3.html'), qr/HTTP/, 'empty with proxy');
182like(http_get('/test-empty3.html'), qr/HTTP/, 'empty with proxy cached');
183
Maxim Dounin2775b732014-11-27 07:00:10 +0300184like(http_get('/test-empty-postpone.html'), qr/HTTP.*XX/ms,
185 'empty with postpone_output 0');
186
Sergey Kandaurov468cbf82013-12-25 12:03:40 +0400187# handling of escaped URIs
188
189like(http_get('/unescape1.html'), qr/^XXtestXX$/m, 'escaped in path');
Sergey Kandaurov9e3e1a92015-05-14 14:46:13 +0300190
191SKIP: {
192skip 'incorrect filename on win32', 2 if $^O eq 'MSWin32';
193
Sergey Kandaurov468cbf82013-12-25 12:03:40 +0400194like(http_get('/unescape2.html'), qr/^XSEE-THISX$/m,
195 'escaped question in path');
196like(http_get('/unescape3.html'), qr/404 Not Found/,
197 'escaped query separator');
198
Sergey Kandaurov9e3e1a92015-05-14 14:46:13 +0300199}
200
Sergey Kandaurov71721ac2014-01-28 16:18:04 +0400201# handling of embedded date variables
202
Sergey Kandaurovd3f22ba2015-07-03 14:50:26 +0300203my $re_date_gmt = qr/X-Var: x.+, \d\d-.+-\d{4} \d\d:\d\d:\d\d .+x/;
Sergey Kandaurov71721ac2014-01-28 16:18:04 +0400204
Sergey Kandaurov9ac80ba2015-05-13 16:57:45 +0300205like(http_get('/var_nossi.html'), $re_date_gmt, 'no ssi');
206like(http_get('/var_noformat.html'), $re_date_gmt, 'no format');
207
208like(http_get('/var_format.html?custom=1'), $re_date_gmt, 'custom header');
209like(http_get('/var_format.html'), $re_date_gmt, 'default header');
Sergey Kandaurov71721ac2014-01-28 16:18:04 +0400210
Sergey Kandaurov71721ac2014-01-28 16:18:04 +0400211like(http_get('/var_format.html?custom=1'),
Sergey Kandaurovd3f22ba2015-07-03 14:50:26 +0300212 qr/x.+, \d\d:\d\d:\d\dx/, 'custom ssi');
Sergey Kandaurov71721ac2014-01-28 16:18:04 +0400213like(http_get('/var_format.html'),
Sergey Kandaurovd3f22ba2015-07-03 14:50:26 +0300214 qr/x.+, \d\d-.+-\d{4} \d\d:\d\d:\d\d .+x/, 'default ssi');
Sergey Kandaurov71721ac2014-01-28 16:18:04 +0400215
Maxim Dounin90330782009-02-22 14:02:07 +0300216###############################################################################