Start making the path handling like it is in trunk. Merge extra tests, add using...
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_engine-prepare_path.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use FindBin qw/$Bin/;
5 use lib "$Bin/../lib";
6 use TestApp;
7 use Catalyst::Engine;
8
9 # mod_rewrite to app root for non / based app
10 {
11     my $r = get_req (0,
12         SCRIPT_NAME => '/comics/dispatch.cgi',
13         REQUEST_URI => '/comics/',
14     );
15     is ''.$r->uri, 'http://www.foo.com/comics/';
16     is ''.$r->base, 'http://www.foo.com/comics/';
17 }
18
19 # mod_rewrite to sub path under app root for non / based app
20 {
21     my $r = get_req (0,
22         PATH_INFO  => '/foo/bar.gif',
23         SCRIPT_NAME => '/comics/dispatch.cgi',
24         REQUEST_URI => '/comics/foo/bar.gif',
25     );
26     is ''.$r->uri, 'http://www.foo.com/comics/foo/bar.gif';
27     is ''.$r->base, 'http://www.foo.com/comics/';
28 }
29
30 # Standard CGI hit for non / based app
31 {
32     my $r = get_req (0,
33         PATH_INFO => '/static/css/blueprint/screen.css',
34         SCRIPT_NAME => '/~bobtfish/Gitalist/script/gitalist.cgi',
35         REQUEST_URI => '/~bobtfish/Gitalist/script/gitalist.cgi/static/css/blueprint/screen.css',
36     );
37     is ''.$r->uri, 'http://www.foo.com/~bobtfish/Gitalist/script/gitalist.cgi/static/css/blueprint/screen.css';
38     is ''.$r->base, 'http://www.foo.com/~bobtfish/Gitalist/script/gitalist.cgi/';
39 }
40 # / %2F %252F escaping case.
41 {
42     my $r = get_req (1,
43         PATH_INFO => '/%2F/%2F',
44         SCRIPT_NAME => '/~bobtfish/Gitalist/script/gitalist.cgi',
45         REQUEST_URI => '/~bobtfish/Gitalist/script/gitalist.cgi/%252F/%252F',
46     );
47     is ''.$r->uri, 'http://www.foo.com/~bobtfish/Gitalist/script/gitalist.cgi/%252F/%252F';
48     is ''.$r->base, 'http://www.foo.com/~bobtfish/Gitalist/script/gitalist.cgi/';
49 }
50
51 # Using rewrite rules to ask for a sub-path in your app.
52 # E.g. RewriteRule ^(.*)$ /path/to/fastcgi/domainprofi.fcgi/iframeredirect$1 [L,NS]
53 {
54     my $r = get_req (0,
55         PATH_INFO => '/iframeredirect/info',
56         SCRIPT_NAME => '',
57         REQUEST_URI => '/info',
58     );
59     is ''.$r->uri, 'http://www.foo.com/iframeredirect/info';
60     is ''.$r->base, 'http://www.foo.com/';
61 }
62
63 # nginx example from espent with path /"foo"
64 {
65     my $r = get_req (0,
66         PATH_INFO => '"foo"',
67         SCRIPT_NAME => '/',
68         REQUEST_URI => '/%22foo%22',
69     );
70     is ''.$r->path, '%22foo%22';
71     is ''.$r->uri, 'http://www.foo.com/%22foo%22';
72     is ''.$r->base, 'http://www.foo.com/';
73 }
74
75 # nginx example from espent with path /"foo" and the app based at /oslobilder
76 {
77     my $r = get_req (1,
78         PATH_INFO => 'oslobilder/"foo"',
79         SCRIPT_NAME => '/oslobilder/',
80         REQUEST_URI => '/oslobilder/%22foo%22',
81     );
82     is ''.$r->path, '%22foo%22', 'path correct';
83     is ''.$r->uri, 'http://www.foo.com/oslobilder/%22foo%22', 'uri correct';
84     is ''.$r->base, 'http://www.foo.com/oslobilder/', 'base correct';
85 }
86 {
87     local $TODO = 'Another mod_rewrite case';
88     my $r = get_req (0,
89         PATH_INFO => '/auth/login',
90         SCRIPT_NAME => '/tx',
91         REQUEST_URI => '/login',
92     );
93     is ''.$r->path, 'auth/login', 'path correct';
94     is ''.$r->uri, 'http://www.foo.com/tx/auth/login', 'uri correct';
95     is ''.$r->base, 'http://www.foo.com/tx/', 'base correct';
96 }
97
98 # test req->base and c->uri_for work correctly after an internally redirected request
99 # (i.e. REDIRECT_URL set) when the PATH_INFO contains a regex
100 {
101     my $path = '/engine/request/uri/Rx(here)';
102     my $r = get_req (0,
103         SCRIPT_NAME => '/',
104         PATH_INFO => $path,
105         REQUEST_URI => $path,
106         REDIRECT_URL => $path,
107     );
108
109     is $r->path, 'engine/request/uri/Rx(here)', 'URI contains correct path';
110     is $r->base, 'http://www.foo.com/', 'Base is correct';
111 }
112
113
114 # FIXME - Test proxy logic
115 #       - Test query string
116 #       - Test non standard port numbers
117 #       - Test // in PATH_INFO
118 #       - Test scheme (secure request on port 80)
119
120 sub get_req {
121     my $use_request_uri_for_path = shift;
122
123     my %template = (
124         HTTP_HOST => 'www.foo.com',
125         PATH_INFO => '/',
126     );
127
128     my $engine = Catalyst::Engine->new(
129         env => { %template, @_ },
130     );
131     my $i = TestApp->new;
132     $i->setup_finished(0);
133     $i->config(use_request_uri_for_path => $use_request_uri_for_path);
134     $i->setup_finished(1);
135     $engine->prepare_path($i);
136     return $i->req;
137 }
138
139 done_testing;
140