File: 16-live_cgi.t

package info (click to toggle)
libhtml-mason-perl 1%3A1.26-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,636 kB
  • ctags: 1,260
  • sloc: perl: 13,880; sh: 154; makefile: 47
file content (218 lines) | stat: -rw-r--r-- 4,128 bytes parent folder | download
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/perl -w

use strict;

unless ($ENV{MASON_MAINTAINER} && -e "$ENV{APACHE_DIR}/CGIHandler.cgi")
{
    print "1..0\n";
    exit;
}

use vars qw($VERBOSE $DEBUG);

BEGIN
{
    $VERBOSE = $ENV{MASON_DEBUG} || $ENV{MASON_VERBOSE};
    $DEBUG = $ENV{MASON_DEBUG};
}

use File::Spec;
use HTML::Mason::Tests;
use Test;

use lib 'lib', File::Spec->catdir('t', 'lib');

require File::Spec->catfile( 't', 'live_server_lib.pl' );

use Apache::test qw(skip_test have_httpd have_module);
skip_test unless have_httpd;

kill_httpd(1);
test_load_apache();

plan(tests => 12);

write_test_comps();
run_tests();

sub write_test_comps
{
    write_comp( 'basic', <<'EOF',
Basic test.
2 + 2 = <% 2 + 2 %>.
EOF
	      );

    write_comp( 'cgi_foo_param', <<'EOF',
CGI foo param is <% $r->query->param('foo') %>
EOF
	      );

    write_comp( 'print', <<'EOF',
This is first.
% print "This is second.\n";
This is third.
EOF
	      );

    write_comp( 'redirect', <<'EOF',
<%init>
$m->redirect('/comps/basic');
</%init>
EOF
	      );

    write_comp( 'params', <<'EOF',
% foreach (sort keys %ARGS) {
<% $_ %>: <% ref $ARGS{$_} ? join ', ', sort @{ $ARGS{$_} }, 'array' : $ARGS{$_} %>
% }
EOF
	      );

    write_comp( 'error_as_html', <<'EOF',
% my $x = undef; @$x;
EOF
	      );

    write_comp( 'abort_with_ok', <<'EOF',
All is well
% $m->abort(200);
Will not be seen
EOF
	      );

    write_comp( 'abort_with_not_ok', <<'EOF',
All is well
% $m->abort(500);
Will not be seen
EOF
	      );

    write_comp( 'foo/dhandler', <<'EOF',
dhandler
% $m->decline;
EOF
	      );

}


sub run_tests
{
    start_httpd('CGIHandler');

    {
	my $path = '/comps/basic';
	my $response = Apache::test->fetch($path);
	ok $response->content, <<'EOF';
Basic test.
2 + 2 = 4.
EOF
    }

    {
	my $path = '/comps/print';
	my $response = Apache::test->fetch($path);
	ok $response->content, <<'EOF';
This is first.
This is second.
This is third.
EOF
    }

    {
	my $path = '/comps/print/autoflush';
	my $response = Apache::test->fetch($path);
	ok $response->content, <<'EOF';
This is first.
This is second.
This is third.
EOF
    }

    {
	my $path = '/comps/print/handle_comp';
	my $response = Apache::test->fetch($path);
	ok $response->content, <<'EOF';
This is first.
This is second.
This is third.
EOF
    }

    {
	my $path = '/comps/print/handle_cgi_object';
	my $response = Apache::test->fetch($path);
	ok $response->content, <<'EOF';
This is first.
This is second.
This is third.
EOF
    }

    {
	my $path = '/comps/cgi_foo_param/handle_cgi_object';
	my $response = Apache::test->fetch($path);
	ok $response->content, <<'EOF';
CGI foo param is bar
EOF
    }

    {
	my $path = '/comps/redirect';
	my $response = Apache::test->fetch($path);
	ok $response->content, <<'EOF';
Basic test.
2 + 2 = 4.
EOF
    }


    {
        my $path = '/comps/params?qs1=foo&qs2=bar&mixed=A';
        my $response = Apache::test->fetch( { uri => $path,
                                              method => 'POST',
                                              content => 'post1=a&post2=b&mixed=B',
                                            } );
        ok $response->content, <<'EOF',
mixed: A, B, array
post1: a
post2: b
qs1: foo
qs2: bar
EOF
    }

    {
        my $path = '/comps/error_as_html';
        my $response = Apache::test->fetch($path);
        ok $response->content, qr{<b>error:</b>.*Error during compilation}s;
    }

    {
        my $path = '/comps/abort_with_ok';
        my $response = Apache::test->fetch($path);
        ok $response->content, <<'EOF';
All is well
EOF
    }

    {
        my $path = '/comps/abort_with_not_ok';
        my $response = Apache::test->fetch($path);
        ok $response->content, <<'EOF';
All is well
EOF
    }

    # Having decline generate an error like this is bad, but there's
    # not much else we can do without rewriting more of CGIHandler,
    # which isn't a good idea for stable, methinks.
    {
        my $path = '/comps/foo/will_decline';
        my $response = Apache::test->fetch($path);
        ok $response->content, qr{could not find component for initial path}is;
    }

    kill_httpd();
}