Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # (C) Maxim Dounin |
| 4 | |
| 5 | # Tests for try_files directive. |
| 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 | 09ed37c | 2015-08-21 15:59:05 +0300 | [diff] [blame] | 24 | my $t = Test::Nginx->new()->has(qw/http proxy rewrite/)->plan(10) |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 25 | ->write_file_expand('nginx.conf', <<'EOF'); |
| 26 | |
| 27 | %%TEST_GLOBALS%% |
| 28 | |
Maxim Dounin | 35773f6 | 2013-01-24 02:17:36 +0400 | [diff] [blame] | 29 | daemon off; |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 30 | |
| 31 | events { |
| 32 | } |
| 33 | |
| 34 | http { |
| 35 | %%TEST_GLOBALS_HTTP%% |
| 36 | |
| 37 | server { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 38 | listen 127.0.0.1:8080; |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 39 | server_name localhost; |
| 40 | |
| 41 | location / { |
| 42 | try_files $uri /fallback; |
| 43 | } |
| 44 | |
| 45 | location /nouri/ { |
Andrey Zelenkov | c4ec79a | 2015-08-21 16:12:56 +0300 | [diff] [blame] | 46 | try_files $uri /fallback-nouri; |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 47 | } |
| 48 | |
Maxim Dounin | 8954f58 | 2012-04-24 14:21:59 +0400 | [diff] [blame] | 49 | location /short/ { |
| 50 | try_files /short $uri =404; |
| 51 | } |
| 52 | |
Damien Tournoud | 17b90d9 | 2015-01-21 00:57:09 +0100 | [diff] [blame] | 53 | 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 Zelenkov | c4ec79a | 2015-08-21 16:12:56 +0300 | [diff] [blame] | 69 | location ~ /alias-re.html { |
Sergey Kandaurov | 7e154a2 | 2015-07-27 13:01:41 +0300 | [diff] [blame] | 70 | alias %%TESTDIR%%/directory; |
| 71 | try_files $uri =404; |
| 72 | } |
| 73 | |
Andrey Zelenkov | 09ed37c | 2015-08-21 15:59:05 +0300 | [diff] [blame] | 74 | location /alias-nested/ { |
| 75 | alias %%TESTDIR%%/; |
| 76 | location ~ html { |
| 77 | try_files $uri =404; |
| 78 | } |
| 79 | } |
| 80 | |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 81 | location /fallback { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 82 | proxy_pass http://127.0.0.1:8081/fallback; |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 83 | } |
Andrey Zelenkov | c4ec79a | 2015-08-21 16:12:56 +0300 | [diff] [blame] | 84 | location /fallback-nouri { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 85 | proxy_pass http://127.0.0.1:8081; |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | server { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 90 | listen 127.0.0.1:8081; |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 91 | server_name localhost; |
| 92 | |
| 93 | location / { |
| 94 | add_header X-URI $request_uri; |
| 95 | return 204; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | EOF |
| 101 | |
Damien Tournoud | 17b90d9 | 2015-01-21 00:57:09 +0100 | [diff] [blame] | 102 | mkdir($t->testdir() . '/directory'); |
Andrey Zelenkov | c4ec79a | 2015-08-21 16:12:56 +0300 | [diff] [blame] | 103 | $t->write_file('directory/alias-re.html', 'SEE THIS'); |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 104 | $t->write_file('found.html', 'SEE THIS'); |
| 105 | $t->run(); |
| 106 | |
| 107 | ############################################################################### |
| 108 | |
| 109 | like(http_get('/found.html'), qr!SEE THIS!, 'found'); |
| 110 | like(http_get('/uri/notfound'), qr!X-URI: /fallback!, 'not found uri'); |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 111 | like(http_get('/nouri/notfound'), qr!X-URI: /fallback!, 'not found nouri'); |
Maxim Dounin | ebdcfc6 | 2012-06-03 14:47:10 +0400 | [diff] [blame] | 112 | like(http_get('/short/long'), qr!404 Not!, 'short uri in try_files'); |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 113 | |
Damien Tournoud | 17b90d9 | 2015-01-21 00:57:09 +0100 | [diff] [blame] | 114 | like(http_get('/file-file/'), qr!SEE THIS!, 'file matches file'); |
Damien Tournoud | 17b90d9 | 2015-01-21 00:57:09 +0100 | [diff] [blame] | 115 | like(http_get('/file-dir/'), qr!404 Not!, 'file does not match dir'); |
Damien Tournoud | 17b90d9 | 2015-01-21 00:57:09 +0100 | [diff] [blame] | 116 | like(http_get('/dir-dir/'), qr!301 Moved Permanently!, 'dir matches dir'); |
| 117 | like(http_get('/dir-file/'), qr!404 Not!, 'dir does not match file'); |
| 118 | |
Andrey Zelenkov | c4ec79a | 2015-08-21 16:12:56 +0300 | [diff] [blame] | 119 | like(http_get('/alias-re.html'), qr!SEE THIS!, 'alias in regex location'); |
Andrey Zelenkov | 09ed37c | 2015-08-21 15:59:05 +0300 | [diff] [blame] | 120 | like(http_get('/alias-nested/found.html'), qr!SEE THIS!, |
| 121 | 'alias with nested location'); |
| 122 | |
Maxim Dounin | af4f0ea | 2011-12-19 17:08:18 +0300 | [diff] [blame] | 123 | ############################################################################### |