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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#!perl -w
use strict;
use Test::More;
BEGIN {
plan skip_all => 'Testing of apache_req requires Apache::Test 1.04'
unless eval { require Apache::Test };
plan skip_all => 'Test of apache_req requires mod_perl'
unless Apache::Test::have_module('mod_perl.c');
require Apache::TestRequest;
Apache::TestRequest->import(qw(GET POST));
plan tests => 178;
}
Apache::TestRequest::user_agent(
reset => 1,
requests_redirectable => 0,
);
my $key = 'myCallbackTester';
my @keys = (
myCallbackTester => '/test',
OOCBTester => '/oop',
OOCBTester => '/ooconf',
);
##############################################################################
# Just make sure it works.
while (my $key = shift @keys) {
my $uri = shift @keys;
ok( my $res = GET("$uri?$key|simple_cb=1"), "Get response" );
is( $res->code, 200, "Check simple response code" );
is( $res->content, "Success", 'Check simple content' );
# Make sure that POST works.
ok( $res = POST($uri, ["$key|simple_cb" => 1]), "Get POST response" );
is( $res->code, 200, "Check simple POST response code" );
is( $res->content, "Success", 'Check simple POST content' );
# Check that multiple callbacks execute in priority order.
ok( $res = POST($uri,
[ "$key|priority_cb0" => 0,
"$key|priority_cb2" => 2,
"$key|priority_cb9" => 9,
"$key|priority_cb7" => 7,
"$key|priority_cb1" => 1,
"$key|priority_cb4" => 4,
"$key|priority_cb" => 'def' ]
),
"Get execution order response" );
is( $res->code, 200, "Check execution order response code" );
is( $res->content, " 0 1 2 4 5 7 9", 'Check execution order content' );
# Execute the one callback with an array of values
ok( $res = POST($uri,
[ "$key|multi_cb" => 1,
"$key|multi_cb" => 1,
"$key|multi_cb" => 1,
"$key|multi_cb" => 1,
"$key|multi_cb" => 1 ]
),
"Get array response" );
is( $res->code, 200, "Check array response code" );
is( $res->content, 5, 'Check array content' );
# Emmulate the sumission of an <input type="image" /> button.
ok( $res = POST($uri,
[ "$key|simple_cb.x" => 18,
"$key|simple_cb.y" => 24 ]
),
"Get image button response" );
is( $res->code, 200, "Check image button code" );
is( $res->content, "Success", 'Check image button content' );
# Make sure that an image submit doesn't cause the callback to be called
# twice.
ok( $res = POST($uri,
[ "$key|count_cb.x" => 18,
"$key|count_cb.y" => 24 ]
),
"Get image button count response" );
is( $res->code, 200, "Check image button count response code" );
is( $res->content, 1, 'Check image button count content' );
# Try the pre request callback.
ok( $res = POST($uri,
[ do_upper => 1,
result => 'yowza!' ]),
"Get pre request callback response" );
is( $res->code, 200, "Check pre request callback code" );
is( $res->content, "YOWZA!", 'Check pre request callback content' );
# Try the post request callback.
ok( $res = POST($uri,
[ "$key|simple_cb" => 1,
do_lower => 1 ]),
"Get post request callback response" );
is( $res->code, 200, "Check post request callback code" );
is( $res->content, "success", 'Check post request callback content' );
##########################################################################
# Fun with notes.
my $note_key = 'myNote';
my $note = 'Test note';
ok( $res = POST($uri,
[ "$key|add_note_cb1" => $note_key, # Executes first.
note => $note,
"$key|get_note_cb" => $note_key]),
"Get note response" );
is( $res->code, 200, "Check note callback code" );
is( $res->content, $note, "Check note callback result" );
# Make sure the note isn't available on the next request.
ok( $res = POST($uri, ["$key|get_note_cb" => $note_key]),
"Get no note response" );
is( $res->code, 200, "Check no note code" );
is( $res->content, '', "Check no note result" );
# Add multiple notes.
ok( $res = POST($uri,
[ "$key|add_note_cb1" => $note_key, # Executes first.
"$key|add_note_cb2" => $note_key . 1, # Executes second.
note => $note,
"$key|list_notes_cb" => 1 ]),
"Get multiple note response" );
is( $res->code, 200, "Check no note code" );
is( $res->content, "$note_key => $note\n${note_key}1 => $note\n",
"Check multiple note result" );
# Make sure that notes percolate back to Mason.
ok( $res = POST($uri,
[ "$key|add_note_cb1" => $note_key,
note => $note,
"$key|mason_note_cb" => $note_key,
]),
"Get mason note response" );
is( $res->code, 200, "Check mason note code" );
is( $res->content, $note, "Check mason note result" );
# Make sure that we can still get at the notes via the callback request
# object in Mason components.
ok( $res = POST($uri,
[ "$key|add_note_cb" => $note_key,
note => $note,
"$key|cbr_note_cb" => $note_key,
]),
"Get cb_request note respone" );
is( $res->code, 200, "Check cb_request note code" );
is( $res->content, $note, "Check cb_request note result" );
# Finally, make sure that if we clear it in callbacks, that no one gets it.
ok( $res = POST($uri,
[ "$key|add_note_cb1" => $note_key, # Executes first.
note => $note,
"$key|clear_cb" => 1,
"$key|mason_note_cb" => 1,
]),
"Get Mason cleared note response" );
is( $res->code, 200, "Check Mason cleared note code" );
is( $res->content, '', "Check Mason cleared note result" );
ok( $res = POST($uri,
[ "$key|add_note_cb1" => $note_key, # Executes first.
note => $note,
"$key|clear_cb" => 1,
"$key|cbr_note_cb" => $note_key,
]),
"Get cb_request cleared note response" );
is( $res->code, 200, "Check cb_request cleared note code" );
is( $res->content, '', "Check cb_request cleared note result" );
}
##############################################################################
# Make sure an exception get thrown for a non-existant package.
ok( my $res = POST("/test?myNoSuchLuck|foo_cb=1"),
"Get non-existent callback response" );
is( $res->code, 500, "Check non-existent callback response code" );
# Make sure that redirects work.
SKIP: {
skip "Redirect tests rrequire Apache::Test 1.04 or newer", 8
unless Apache::Test->VERSION >= 1.04;
ok( $res = POST("/test",
["$key|redir_cb" => 0,
"$key|add_header_cb9" => 1,
"header" => 'Age',
"value" => 42 ]
),
"Get redirect response" );
is( $res->code, 302, "Check redirect response code" );
is( $res->header('Location'), 'https://example.com/',
"Check redirect location" );
is( $res->header('Age'), undef, "Check redirect Age header" );
# Make sure that redirect without abort works.
ok( $res = POST("/test",
["$key|redir_cb0" => 1,
"$key|add_header_cb9" => 1,
"header" => 'Age',
"value" => 42 ]
),
"Get redirect without abort response" );
is( $res->code, 302, "Check redirect without abort response code" );
is( $res->header('Location'), 'https://example.com/',
"Check redirect without abort location" );
is( $res->header('Age'), 42, "Check redirect without abort Age header" );
}
# Make sure that abort 200 works.
ok( $res = POST("/test", [ "$key|test_abort_cb" => 200 ]),
"Get abort 200 request" );
is( $res->code, 200, "Check abort 200 response code" );
# Now try to die in the callback.
ok( $res = POST("/test", [ "$key|exception_cb" => 0 ]), "Get die response" );
is( $res->code, 500, "Check die response code" );
# Now try to throw and handle an exception in the callback.
ok( $res = POST("/exception_handler", [ "$key|exception_cb" => 1 ]),
"Get exception handler response" );
is( $res->code, 200, "Check exception handler response code" );
# Now make sure that a callback with a value executes.
ok( $res = POST("/no_null", [ "$key|simple_cb" => 1 ]),
"Get simle no null response" );
is( $res->code, 200, "Check simple no null code" );
is( $res->content, "Success", 'Check simple no null content' );
# Now make sure that a callback with a null string does not execute.
ok( $res = POST("/no_null", [ "$key|simple_cb" => '' ]),
"Get simle no null response" );
is( $res->code, 200, "Check simple null code" );
is( $res->content, "", 'Check simple null content' );
# Test MasonCallbacks + MasonDefaultPkgKey.
ok( $res = POST("/conf", ["CBFoo|pkg_key_cb" => 1]),
"Get MasonDefaultPkgKey response" );
is( $res->code, 200, "Check MasonDefaultPkgKey code" );
is( $res->content, 'CBFoo', 'Check MasonDefaultPkgKey content' );
# Test MasonCallbacks + MasonDefaultPriority.
ok( $res = POST("/conf", ["CBFoo|priority_cb" => 1]),
"Get MasonDefaultPriority response" );
is( $res->code, 200, "Check MasonDefaultPriority code" );
is( $res->content, 3, 'Check MasonDefaultPriority content' );
# Test MasonPreCallbacks.
ok( $res = POST("/conf", [result => 'yes', do_upper => 1]),
"Get MasonPreCallbacks response" );
is( $res->code, 200, "Check MasonPreCallbacks code" );
is( $res->content, 'YES', 'Check MasonPreCallbacks content' );
# Test MasonCbExceptionHandler.
ok( $res = POST("/conf", ["CBFoo|die_cb" => 1]),
"Get MasonCbExceptionHandler response" );
is( $res->code, 200, "Check MasonCbExceptionHandler code" );
is( $res->content, '', 'Check MasonCbExceptionHandler content' );
# Test MasonPostCallbacks.
ok( $res = POST("/conf", [result => 'YES', do_lower => 1]),
"Get MasonPostCallbacks response" );
is( $res->code, 200, "Check MasonPostCallbacks code" );
is( $res->content, 'yes', 'Check MasonPostCallbacks content' );
# Test MasonIgnoreNulls.
ok( $res = POST("/nulls_conf", ["CBFoo|exec_cb" => 1]),
"Get MasonIgnoreNulls response" );
is( $res->code, 200, "Check MasonIgnoreNulls code" );
is( $res->content, 'executed', 'Check MasonIgnoreNulls content' );
# Test MasonIgnoreNulls.
ok( $res = POST("/nulls_conf", ["CBFoo|exec_cb" => '']),
"Get MasonIgnoreNulls null response" );
is( $res->code, 200, "Check MasonIgnoreNulls null code" );
is( $res->content, '', 'Check MasonIgnoreNulls null content' );
1;
__END__
|