Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix JSONP handling of /source URLs

  • Loading branch information...
commit 5fb97c02558cd8b8395383adc24267fe3c5b0bc7 1 parent d039fec
Grant McLean authored
Showing with 28 additions and 1 deletion.
  1. +6 −1 lib/MetaCPAN/Server/View/JSONP.pm
  2. +22 −0 t/server/controller/source.t
7 lib/MetaCPAN/Server/View/JSONP.pm
View
@@ -7,7 +7,12 @@ extends 'Catalyst::View';
sub process {
my ($self, $c) = @_;
return 1 unless(my $cb = $c->req->params->{callback});
- my $body = decode_utf8($c->res->body);
+ my $body = $c->res->body;
+ if( ref($body) ) {
+ local($/);
+ $body = <$body>;
+ }
+ $body = decode_utf8($body);
my $content_type = $c->res->content_type;
return 1 if($content_type eq 'text/javascript');
if($content_type ne 'application/json') {
22 t/server/controller/source.t
View
@@ -7,6 +7,8 @@ use MetaCPAN::Server::Test;
my %tests = (
'/source/DOESNEXIST' => 404,
'/source/DOY/Moose-0.01/' => 200,
+ '/source/DOY/Moose-0.01/MANIFEST' => 200,
+ '/source/DOY/Moose-0.01/MANIFEST?callback=foo' => 200,
'/source/Moose' => 200,
);
@@ -22,6 +24,26 @@ test_psgi app, sub {
'Content-type'
);
}
+ elsif ( $k =~ /MANIFEST/ ) {
+ my $manifest = "MANIFEST\nlib/Moose.pm\nMakefile.PL\n"
+ . "META.yml\nt/00-nop.t";
+ if( $k =~ /callback=foo/ ) {
+ ok( my( $function_args ) = $res->content =~ /^foo\((.*)\)/s, 'JSONP wrapper');
+ ok( my $jsdata = JSON->new->allow_nonref->decode( $function_args ), 'decode json' );
+ is( $jsdata, $manifest, 'JSONP-wrapped manifest' );
+ is( $res->header('content-type'),
+ 'text/javascript; charset=UTF-8',
+ 'Content-type'
+ );
+ }
+ else {
+ is( $res->content, $manifest, 'Plain text manifest' );
+ is( $res->header('content-type'),
+ 'text/plain; charset=UTF-8',
+ 'Content-type'
+ );
+ }
+ }
elsif ( $v eq 200 ) {
like( $res->content, qr/Index of/, 'Index of' );
is( $res->header('content-type'),
Please sign in to comment.
Something went wrong with that request. Please try again.