-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsgi_is_current_page.t
executable file
·156 lines (127 loc) · 4.13 KB
/
psgi_is_current_page.t
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env perl
# -*- mode: perl; coding: utf-8 -*-
#----------------------------------------
use strict;
use warnings qw(FATAL all NONFATAL misc);
use FindBin; BEGIN { do "$FindBin::Bin/t_lib.pl" }
#----------------------------------------
use Test::Kantan;
use File::Temp qw/tempdir/;
use Plack::Request;
use Plack::Response;
use HTTP::Request::Common;
use HTTP::Message::PSGI;
use YATT::t::t_preload; # To make Devel::Cover happy.
use YATT::Lite::WebMVC0::SiteApp;
use YATT::Lite::Util qw/combination/;
use YATT::Lite::Util::File qw/mkfile/;
use File::Path qw(make_path);
use Cwd;
use YATT::Lite::PSGIEnv;
my $tempdir = tempdir(CLEANUP => 1);
my $testno = 0;
my $CT = ["Content-Type", q{text/html; charset="utf-8"}];
my $cwd = cwd();
foreach my $test (combination(['', '/meta/super']
, [[path_translated => 1], [direct => 0]]))
{
my ($script_name, $mode) = @$test;
my ($theme, $use_path_translated) = @$mode;
my $make_test_env = sub {
my $app_root = "$tempdir/t" . ++$testno . $script_name;
my $real_dir = "$app_root/html";
make_path($real_dir);
my $mkpsgi = sub {
my ($url) = @_;
my Env $env = (GET $url)->to_psgi;
if ($use_path_translated) {
# Test for Apache config like:
#
# Action x-psgi-handler $script_name/cgi-bin/dispatch.cgi
# AddHandler x-psgi-handler .yatt .ytmpl .ydo
#
#
# With above config, SCRIPT_NAME contains /cgi-bin/dispatch.cgi part.
# This harms use of SCRIPT_NAME for path abstraction.
# So, &yatt:script_name(); tries to trim it.
#
$env->{SCRIPT_NAME} = "$script_name/cgi-bin/dispatch.cgi";
$env->{SCRIPT_FILENAME} = "$real_dir/cgi-bin/dispatch.cgi";
# Below mimics DirectoryIndex behavior.
#
$url =~ s,/$,/index,;
$env->{PATH_TRANSLATED} = "$real_dir$url.yatt";
# Other cgi fields set by Apache.
#
$env->{REDIRECT_HANDLER} = 'x-psgi-handler';
$env->{REDIRECT_STATUS} = 200;
$env->{REDIRECT_URL} = "$url.yatt";
} else {
#
# Normal case.
#
$env->{SCRIPT_NAME} = $script_name;
}
$env;
};
my $site = YATT::Lite::WebMVC0::SiteApp
->new(app_ns => "Test$testno"
, app_root => $app_root
, doc_root => $real_dir);
($real_dir, $site, $mkpsgi);
};
describe "When expected script_name is ($script_name) with $theme mode,", sub {
my ($real_dir, $site, $mkpsgi) = $make_test_env->();
my $item = 0;
$item++;
foreach my $req (['/' => 'index']
, ["/test$item", "test$item"]
, ['/subdir/' => 'subdir/index']
, ["/subdir/test$item" => "subdir/test$item"])
{
my ($url, $wname) = @$req;
{
MY->mkfile("$real_dir/$wname.yatt", <<'END');
<yatt:tab/>
<!yatt:page "/foo">
<yatt:tab/>
<!yatt:page "/bar">
<yatt:tab/>
<!yatt:widget tab>
&yatt:if(:is_current_page(),yes,no);
&yatt:if(:is_current_page(/),yes,no);
-
&yatt:if(:is_current_page(foo),yes,no);
&yatt:if(:is_current_page(/foo),yes,no);
-
&yatt:if(:is_current_page(bar),yes,no);
&yatt:if(:is_current_page(/bar),yes,no);
END
my $test = sub {
my ($arg, $page, $to_be) = @_;
my $real_url = "$url$page";
$real_url =~ s,//+,/,g;
describe ":is_current_page($arg) for subpage='$page' (real_url=$real_url real_file=/$wname.yatt)", sub {
my $psgi = (GET $real_url)->to_psgi;
my $psgi_result = sub {
my ($psgi) = @_;
my ($status, $header, $body) = @{$site->call($psgi)};
[$status, join(" ", split " ", $body->[0])];
};
it "should return ($to_be)", sub {
expect($psgi_result->($psgi))->to_be([200, $to_be]);
};
};
};
$test->("" => "" => q(yes yes - no no - no no));
$test->("/" => "" => q(yes yes - no no - no no));
$test->(foo => "/foo" => q(no no - yes yes - no no));
$test->("/foo" => "/foo" => q(no no - yes yes - no no));
$test->(bar => "/bar" => q(no no - no no - yes yes));
$test->("/bar" => "/bar" => q(no no - no no - yes yes));
}
};
}
}
chdir($cwd);
done_testing();