| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Plack::Test; |
| 5 |
use HTTP::Request::Common; |
| 6 |
|
| 7 |
eval { require AnyEvent; 1; } |
| 8 |
or plan skip_all => 'AnyEvent required for this test'; |
| 9 |
|
| 10 |
plan tests => 6; |
| 11 |
|
| 12 |
{ |
| 13 |
package App::Content; ## no critic |
| 14 |
use Dancer2; |
| 15 |
get '/' => sub { |
| 16 |
::is( $Dancer2::Core::Route::RESPONDER, undef, 'No responder yet' ); |
| 17 |
|
| 18 |
delayed { |
| 19 |
::isa_ok( |
| 20 |
$Dancer2::Core::Route::RESPONDER, |
| 21 |
'CODE', |
| 22 |
'Got a responder in the delayed callback', |
| 23 |
); |
| 24 |
|
| 25 |
::is( $Dancer2::Core::Route::WRITER, undef, 'No writer yet' ); |
| 26 |
|
| 27 |
content 'OK'; |
| 28 |
::ok( $Dancer2::Core::Route::WRITER, 'Got a writer' ); |
| 29 |
|
| 30 |
done; |
| 31 |
}; |
| 32 |
}; |
| 33 |
} |
| 34 |
|
| 35 |
{ |
| 36 |
package App::Content::MultiWrite; ## no critic |
| 37 |
use Dancer2; |
| 38 |
get '/' => sub { |
| 39 |
delayed { |
| 40 |
flush; |
| 41 |
content 'Foo'; |
| 42 |
content 'Bar'; |
| 43 |
done; |
| 44 |
}; |
| 45 |
}; |
| 46 |
} |
| 47 |
|
| 48 |
{ |
| 49 |
package App::NoContent; ## no critic |
| 50 |
use Dancer2; |
| 51 |
get '/' => sub { |
| 52 |
delayed {content;done;'Not OK'}; |
| 53 |
}; |
| 54 |
} |
| 55 |
|
| 56 |
{ |
| 57 |
package App::AddHeader; ## no critic |
| 58 |
use Dancer2; |
| 59 |
|
| 60 |
hook 'after' => sub { |
| 61 |
my $response = shift; |
| 62 |
$response->push_header('Authorization' => 'bar'); |
| 63 |
}; |
| 64 |
|
| 65 |
get '/' => sub { |
| 66 |
delayed { |
| 67 |
response->push_header('Content-Type' => 'foo'); |
| 68 |
flush; |
| 69 |
content "baz"; |
| 70 |
done; |
| 71 |
} |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
{ |
| 76 |
package App::MultipleContent; ## no critic |
| 77 |
use Dancer2; |
| 78 |
get '/' => sub { |
| 79 |
delayed { |
| 80 |
content 'Bar'; |
| 81 |
done; |
| 82 |
}; |
| 83 |
return 'OK'; |
| 84 |
}; |
| 85 |
} |
| 86 |
|
| 87 |
my $caught_error; |
| 88 |
{ |
| 89 |
package App::ErrorHandler; ## no critic |
| 90 |
use Dancer2; |
| 91 |
require AnyEvent; |
| 92 |
set logger => 'Capture'; |
| 93 |
get '/log' => sub { |
| 94 |
delayed { |
| 95 |
flush; |
| 96 |
content "ping\n"; |
| 97 |
done; |
| 98 |
content "failure\n"; |
| 99 |
}; |
| 100 |
}; |
| 101 |
|
| 102 |
get '/cb' => sub { |
| 103 |
delayed { |
| 104 |
flush; |
| 105 |
content "ping\n"; |
| 106 |
done; |
| 107 |
content "failure\n"; |
| 108 |
} on_error => sub { |
| 109 |
$caught_error = shift; |
| 110 |
}; |
| 111 |
}; |
| 112 |
} |
| 113 |
|
| 114 |
subtest 'Testing an app with content keyword' => sub { |
| 115 |
my $test = Plack::Test->create( App::Content->to_app ); |
| 116 |
my $res = $test->request( GET '/' ); |
| 117 |
ok( $res->is_success, 'Successful request' ); |
| 118 |
is( $res->content, 'OK', 'Correct content' ); |
| 119 |
}; |
| 120 |
|
| 121 |
subtest 'Testing an app with multiple content keyword calls' => sub { |
| 122 |
my $test = Plack::Test->create( App::Content::MultiWrite->to_app ); |
| 123 |
my $res = $test->request( GET '/' ); |
| 124 |
ok( $res->is_success, 'Successful request' ); |
| 125 |
is( $res->content, 'FooBar', 'Correct content' ); |
| 126 |
}; |
| 127 |
|
| 128 |
subtest 'Testing an app without content keyword' => sub { |
| 129 |
my $test = Plack::Test->create( App::NoContent->to_app ); |
| 130 |
my $res = $test->request( GET '/' ); |
| 131 |
ok( $res->is_success, 'Successful request' ); |
| 132 |
is( $res->content, '', 'Correct content' ); |
| 133 |
}; |
| 134 |
|
| 135 |
subtest 'Delayed response has push_header method' => sub { |
| 136 |
my $test = Plack::Test->create( App::AddHeader->to_app ); |
| 137 |
my $res = $test->request( GET '/' ); |
| 138 |
ok( $res->is_success, 'Successful request' ); |
| 139 |
is( $res->headers->header('Content-Type'), 'foo', 'Correct header pushed from inside "delayed"' ); |
| 140 |
is( $res->headers->header('Authorization'), 'bar', 'Correct header pushed from inside hook' ); |
| 141 |
}; |
| 142 |
|
| 143 |
subtest 'Delayed response ignored for non-delayed content' => sub { |
| 144 |
my $test = Plack::Test->create( App::MultipleContent->to_app ); |
| 145 |
my $res = $test->request( GET '/' ); |
| 146 |
ok( $res->is_success, 'Successful request' ); |
| 147 |
is( $res->content, 'OK', 'Correct content' ); |
| 148 |
}; |
| 149 |
|
| 150 |
subtest 'Delayed response error handling' => sub { |
| 151 |
my $test = Plack::Test->create( App::ErrorHandler->to_app ); |
| 152 |
|
| 153 |
TODO: { |
| 154 |
local $TODO = 'Does not work in development server'; |
| 155 |
|
| 156 |
my $res = $test->request( GET '/log' ); |
| 157 |
ok( $res->is_success, 'Successful request' ); |
| 158 |
is( $res->content, "ping\n", 'Correct content' ); |
| 159 |
|
| 160 |
my $logger = App::ErrorHandler::app->logger_engine; |
| 161 |
my $logs = $logger->trapper->read; |
| 162 |
isa_ok( $logs, 'ARRAY', 'Got logs' ); |
| 163 |
is( scalar @{$logs}, 1, 'Got a message' ); |
| 164 |
|
| 165 |
my $msg = shift @{$logs}; |
| 166 |
ok( $msg, 'Got message' ); |
| 167 |
isa_ok( $msg, 'HASH', 'Got message' ); |
| 168 |
is( |
| 169 |
$msg->{'level'}, |
| 170 |
'core', |
| 171 |
'Correct error message level', |
| 172 |
); |
| 173 |
|
| 174 |
like( |
| 175 |
$msg->{'message'}, |
| 176 |
qr/^Error in delayed response:/, |
| 177 |
'Got error', |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
TODO: { |
| 182 |
local $TODO = 'Does not work in development server'; |
| 183 |
my $res = $test->request( GET '/cb' ); |
| 184 |
ok( $res->is_success, 'Successful request' ); |
| 185 |
is( $res->content, "ping\n", 'Correct content' ); |
| 186 |
like( $caught_error, qr/^Error in delayed response:/, 'Got error' ); |
| 187 |
} |
| 188 |
}; |