File: unit_core_engine-prepare_path.t

package info (click to toggle)
libcatalyst-perl 5.90132-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,016 kB
  • sloc: perl: 11,061; makefile: 7
file content (140 lines) | stat: -rw-r--r-- 4,311 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use strict;
use warnings;
use Test::More;
use FindBin qw/$Bin/;
use lib "$Bin/../lib";
use TestApp;
use Catalyst::Engine;

# mod_rewrite to app root for non / based app
{
    my $r = get_req (0,
        REDIRECT_URL => '/comics/',
        SCRIPT_NAME => '/comics/dispatch.cgi',
        REQUEST_URI => '/comics/',
    );
    is ''.$r->uri, 'http://www.foo.com/comics/';
    is ''.$r->base, 'http://www.foo.com/comics/';
}

# mod_rewrite to sub path under app root for non / based app
{
    my $r = get_req (0,
        PATH_INFO  => '/foo/bar.gif',
        REDIRECT_URL => '/comics/foo/bar.gif',
        SCRIPT_NAME => '/comics/dispatch.cgi',
        REQUEST_URI => '/comics/foo/bar.gif',
    );
    is ''.$r->uri, 'http://www.foo.com/comics/foo/bar.gif';
    is ''.$r->base, 'http://www.foo.com/comics/';
}

# Standard CGI hit for non / based app
{
    my $r = get_req (0,
        PATH_INFO => '/static/css/blueprint/screen.css',
        SCRIPT_NAME => '/~bobtfish/Gitalist/script/gitalist.cgi',
        REQUEST_URI => '/~bobtfish/Gitalist/script/gitalist.cgi/static/css/blueprint/screen.css',
    );
    is ''.$r->uri, 'http://www.foo.com/~bobtfish/Gitalist/script/gitalist.cgi/static/css/blueprint/screen.css';
    is ''.$r->base, 'http://www.foo.com/~bobtfish/Gitalist/script/gitalist.cgi/';
}
# / %2F %252F escaping case.
{
    my $r = get_req (1,
        PATH_INFO => '/%2F/%2F',
        SCRIPT_NAME => '/~bobtfish/Gitalist/script/gitalist.cgi',
        REQUEST_URI => '/~bobtfish/Gitalist/script/gitalist.cgi/%252F/%252F',
    );
    is ''.$r->uri, 'http://www.foo.com/~bobtfish/Gitalist/script/gitalist.cgi/%252F/%252F';
    is ''.$r->base, 'http://www.foo.com/~bobtfish/Gitalist/script/gitalist.cgi/';
}

# Using rewrite rules to ask for a sub-path in your app.
# E.g. RewriteRule ^(.*)$ /path/to/fastcgi/domainprofi.fcgi/iframeredirect$1 [L,NS]
{
    my $r = get_req (0,
        PATH_INFO => '/iframeredirect/info',
        SCRIPT_NAME => '',
        REQUEST_URI => '/info',
    );
    is ''.$r->uri, 'http://www.foo.com/iframeredirect/info';
    is ''.$r->base, 'http://www.foo.com/';
}

# nginx example from espent with path /"foo"
{
    my $r = get_req (0,
        PATH_INFO => '"foo"',
        SCRIPT_NAME => '/',
        REQUEST_URI => '/%22foo%22',
    );
    is ''.$r->path, '%22foo%22';
    is ''.$r->uri, 'http://www.foo.com/%22foo%22';
    is ''.$r->base, 'http://www.foo.com/';
}

# nginx example from espent with path /"foo" and the app based at /oslobilder
{
    my $r = get_req (1,
        PATH_INFO => 'oslobilder/"foo"',
        SCRIPT_NAME => '/oslobilder/',
        REQUEST_URI => '/oslobilder/%22foo%22',
    );
    is ''.$r->path, '%22foo%22', 'path correct';
    is ''.$r->uri, 'http://www.foo.com/oslobilder/%22foo%22', 'uri correct';
    is ''.$r->base, 'http://www.foo.com/oslobilder/', 'base correct';
}
{
    my $r = get_req (0,
        PATH_INFO => '/auth/login',
        SCRIPT_NAME => '/tx',
        REQUEST_URI => '/login',
    );
    is ''.$r->path, 'auth/login', 'path correct';
    is ''.$r->uri, 'http://www.foo.com/tx/auth/login', 'uri correct';
    is ''.$r->base, 'http://www.foo.com/tx/', 'base correct';
}

# test req->base and c->uri_for work correctly after an internally redirected request
# (i.e. REDIRECT_URL set) when the PATH_INFO contains a regex
{
    my $path = '/engine/request/uri/Rx(here)';
    my $r = get_req (0,
        SCRIPT_NAME => '/',
        PATH_INFO => $path,
        REQUEST_URI => $path,
        REDIRECT_URL => $path,
    );

    is $r->path, 'engine/request/uri/Rx(here)', 'URI contains correct path';
    is $r->base, 'http://www.foo.com/', 'Base is correct';
}


# FIXME - Test proxy logic
#       - Test query string
#       - Test non standard port numbers
#       - Test // in PATH_INFO
#       - Test scheme (secure request on port 80)

sub get_req {
    my $use_request_uri_for_path = shift;

    my %template = (
        HTTP_HOST => 'www.foo.com',
        PATH_INFO => '/',
    );

    my $engine = Catalyst::Engine->new();
    my $i = TestApp->new;
    $i->setup_finished(0);
    $i->config(use_request_uri_for_path => $use_request_uri_for_path);
    $i->setup_finished(1);
    $engine->prepare_request($i, env => { %template, @_ }, response_cb => sub {});
    $engine->prepare_path($i);
    return $i->req;
}

done_testing;