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
|
use strict;
use Test::More;
use Plack::Test;
use Plack::Builder;
my $json = '{"foo":"bar"}';
my @app = (
sub {
return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ];
},
sub {
return sub {
my $respond = shift;
$respond->(
[ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ]
);
};
},
);
for my $app ( @app ) {
$app = builder {
enable "Plack::Middleware::JSONP";
$app;
};
test_psgi app => $app, client => sub {
my $cb = shift;
my $res = $cb->(HTTP::Request->new(GET => 'http://localhost/'));
is $res->content_type, 'application/json';
is $res->content, $json;
$res = $cb->(HTTP::Request->new(GET => 'http://localhost/?callback=foo'));
is $res->content_type, 'text/javascript';
is $res->content, "foo($json)";
};
}
done_testing;
|