blob: 7a6d6a9674095b0d5ed41389fa5101887b39482e [file] [log] [blame]
Maxim Douninaf4f0ea2011-12-19 17:08:18 +03001#!/usr/bin/perl
2
3# (C) Maxim Dounin
4
5# Tests for try_files directive.
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 Zelenkov09ed37c2015-08-21 15:59:05 +030024my $t = Test::Nginx->new()->has(qw/http proxy rewrite/)->plan(10)
Maxim Douninaf4f0ea2011-12-19 17:08:18 +030025 ->write_file_expand('nginx.conf', <<'EOF');
26
27%%TEST_GLOBALS%%
28
Maxim Dounin35773f62013-01-24 02:17:36 +040029daemon off;
Maxim Douninaf4f0ea2011-12-19 17:08:18 +030030
31events {
32}
33
34http {
35 %%TEST_GLOBALS_HTTP%%
36
37 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030038 listen 127.0.0.1:8080;
Maxim Douninaf4f0ea2011-12-19 17:08:18 +030039 server_name localhost;
40
41 location / {
42 try_files $uri /fallback;
43 }
44
45 location /nouri/ {
Andrey Zelenkovc4ec79a2015-08-21 16:12:56 +030046 try_files $uri /fallback-nouri;
Maxim Douninaf4f0ea2011-12-19 17:08:18 +030047 }
48
Maxim Dounin8954f582012-04-24 14:21:59 +040049 location /short/ {
50 try_files /short $uri =404;
51 }
52
Damien Tournoud17b90d92015-01-21 00:57:09 +010053 location /file-file/ {
54 try_files /found.html =404;
55 }
56
57 location /file-dir/ {
58 try_files /found.html/ =404;
59 }
60
61 location /dir-dir/ {
62 try_files /directory/ =404;
63 }
64
65 location /dir-file/ {
66 try_files /directory =404;
67 }
68
Andrey Zelenkovc4ec79a2015-08-21 16:12:56 +030069 location ~ /alias-re.html {
Sergey Kandaurov7e154a22015-07-27 13:01:41 +030070 alias %%TESTDIR%%/directory;
71 try_files $uri =404;
72 }
73
Andrey Zelenkov09ed37c2015-08-21 15:59:05 +030074 location /alias-nested/ {
75 alias %%TESTDIR%%/;
76 location ~ html {
77 try_files $uri =404;
78 }
79 }
80
Maxim Douninaf4f0ea2011-12-19 17:08:18 +030081 location /fallback {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030082 proxy_pass http://127.0.0.1:8081/fallback;
Maxim Douninaf4f0ea2011-12-19 17:08:18 +030083 }
Andrey Zelenkovc4ec79a2015-08-21 16:12:56 +030084 location /fallback-nouri {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030085 proxy_pass http://127.0.0.1:8081;
Maxim Douninaf4f0ea2011-12-19 17:08:18 +030086 }
87 }
88
89 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030090 listen 127.0.0.1:8081;
Maxim Douninaf4f0ea2011-12-19 17:08:18 +030091 server_name localhost;
92
93 location / {
94 add_header X-URI $request_uri;
95 return 204;
96 }
97 }
98}
99
100EOF
101
Damien Tournoud17b90d92015-01-21 00:57:09 +0100102mkdir($t->testdir() . '/directory');
Andrey Zelenkovc4ec79a2015-08-21 16:12:56 +0300103$t->write_file('directory/alias-re.html', 'SEE THIS');
Maxim Douninaf4f0ea2011-12-19 17:08:18 +0300104$t->write_file('found.html', 'SEE THIS');
105$t->run();
106
107###############################################################################
108
109like(http_get('/found.html'), qr!SEE THIS!, 'found');
110like(http_get('/uri/notfound'), qr!X-URI: /fallback!, 'not found uri');
Maxim Douninaf4f0ea2011-12-19 17:08:18 +0300111like(http_get('/nouri/notfound'), qr!X-URI: /fallback!, 'not found nouri');
Maxim Douninebdcfc62012-06-03 14:47:10 +0400112like(http_get('/short/long'), qr!404 Not!, 'short uri in try_files');
Maxim Douninaf4f0ea2011-12-19 17:08:18 +0300113
Damien Tournoud17b90d92015-01-21 00:57:09 +0100114like(http_get('/file-file/'), qr!SEE THIS!, 'file matches file');
Damien Tournoud17b90d92015-01-21 00:57:09 +0100115like(http_get('/file-dir/'), qr!404 Not!, 'file does not match dir');
Damien Tournoud17b90d92015-01-21 00:57:09 +0100116like(http_get('/dir-dir/'), qr!301 Moved Permanently!, 'dir matches dir');
117like(http_get('/dir-file/'), qr!404 Not!, 'dir does not match file');
118
Andrey Zelenkovc4ec79a2015-08-21 16:12:56 +0300119like(http_get('/alias-re.html'), qr!SEE THIS!, 'alias in regex location');
Andrey Zelenkov09ed37c2015-08-21 15:59:05 +0300120like(http_get('/alias-nested/found.html'), qr!SEE THIS!,
121 'alias with nested location');
122
Maxim Douninaf4f0ea2011-12-19 17:08:18 +0300123###############################################################################