Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
99 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use MetaCPAN::Server::Test; | ||
use Path::Class qw(file); | ||
use Cpanel::JSON::XS (); | ||
use HTTP::Request::Common qw( GET ); | ||
use MetaCPAN::Server (); | ||
use MetaCPAN::Server::App; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
oalders
Author
Member
|
||
use Path::Class qw(dir); | ||
use Plack::Test; | ||
use Test::More; | ||
use Try::Tiny qw( catch try ); | ||
|
||
file( | ||
MetaCPAN::Server->model('Source')->base_dir, | ||
'DOY/Moose-0.02/Moose-0.02/binary.bin' | ||
)->openw->print( "\x00" x 10 ); | ||
my $dir = dir( MetaCPAN::Server->model('Source')->base_dir, | ||
'DOY/Moose-0.02/Moose-0.02' ); | ||
$dir->mkpath; | ||
|
||
my $file = $dir->file('binary.bin'); | ||
$file->openw->print( "\x00" x 10 ); | ||
|
||
my %tests = ( | ||
|
||
|
@@ -21,82 +28,88 @@ my %tests = ( | |
'/pod/Pod::Pm' => 200, | ||
); | ||
|
||
test_psgi app, sub { | ||
my $cb = shift; | ||
while ( my ( $k, $v ) = each %tests ) { | ||
ok( my $res = $cb->( GET $k), "GET $k" ); | ||
is( $res->code, $v, "code $v" ); | ||
is( | ||
$res->header('content-type'), | ||
$v == 200 | ||
? 'text/html; charset=UTF-8' | ||
: 'application/json; charset=utf-8', | ||
'Content-type' | ||
); | ||
my $app = MetaCPAN::Server->new->to_app(); | ||
my $test = Plack::Test->create($app); | ||
|
||
if ( $k eq '/pod/Pod::Pm' ) { | ||
like( $res->content, qr/Pod::Pm - abstract/, 'NAME section' ); | ||
} | ||
elsif ( $v == 200 ) { | ||
like( $res->content, qr/Moose - abstract/, 'NAME section' ); | ||
ok( $res = $cb->( GET "$k?content-type=text/plain" ), | ||
'GET plain' ); | ||
is( | ||
$res->header('content-type'), | ||
'text/plain; charset=UTF-8', | ||
'Content-type' | ||
); | ||
} | ||
elsif ( $v == 404 ) { | ||
like( $res->content, qr/Not found/, '404 correct error' ); | ||
} | ||
while ( my ( $k, $v ) = each %tests ) { | ||
my $res = $test->request( GET $k); | ||
ok( $res, "GET $k" ); | ||
is( $res->code, $v, "code $v" ); | ||
is( | ||
$res->header('content-type'), | ||
$v == 200 | ||
? 'text/html; charset=UTF-8' | ||
: 'application/json; charset=utf-8', | ||
'Content-type' | ||
); | ||
|
||
my $ct = $k =~ /Moose[.]pm$/ ? '&content-type=text/x-pod' : q[]; | ||
ok( $res = $cb->( GET "$k?callback=foo$ct" ), | ||
"GET $k with callback" ); | ||
is( $res->code, $v, "code $v" ); | ||
if ( $k eq '/pod/Pod::Pm' ) { | ||
like( $res->content, qr/Pod::Pm - abstract/, 'NAME section' ); | ||
} | ||
elsif ( $v == 200 ) { | ||
like( $res->content, qr/Moose - abstract/, 'NAME section' ); | ||
$res = $test->request( GET "$k?content-type=text/plain" ); | ||
is( | ||
$res->header('content-type'), | ||
'text/javascript; charset=UTF-8', | ||
'text/plain; charset=UTF-8', | ||
'Content-type' | ||
); | ||
} | ||
elsif ( $v == 404 ) { | ||
like( $res->content, qr/Not found/, '404 correct error' ); | ||
} | ||
|
||
my $ct = $k =~ /Moose[.]pm$/ ? '&content-type=text/x-pod' : q[]; | ||
$res = $test->request( GET "$k?callback=foo$ct" ); | ||
is( $res->code, $v, "code $v" ); | ||
is( | ||
$res->header('content-type'), | ||
'text/javascript; charset=UTF-8', | ||
'Content-type' | ||
); | ||
|
||
ok( my ($function_args) = $res->content =~ /^\/\*\*\/foo\((.*)\)/s, | ||
'callback included' ); | ||
my $js_data; | ||
try { | ||
$js_data | ||
= Cpanel::JSON::XS->new->allow_blessed->allow_nonref->binary | ||
->decode($function_args); | ||
}; | ||
ok( $js_data, 'decode json' ); | ||
|
||
if ( $v eq 200 ) { | ||
|
||
ok( my ($function_args) = $res->content =~ /^\/\*\*\/foo\((.*)\)/s, | ||
'callback included' ); | ||
ok( my $jsdata = JSON->new->allow_nonref->decode($function_args), | ||
'decode json' ); | ||
|
||
if ( $v eq 200 ) { | ||
|
||
if ($ct) { | ||
like( $jsdata, qr{=head1 NAME}, 'POD body was JSON encoded' ); | ||
} | ||
else { | ||
like( | ||
$jsdata, | ||
qr{<h1 id="NAME">NAME</h1>}, | ||
'HTML body was JSON encoded' | ||
); | ||
} | ||
if ($ct) { | ||
like( $js_data, qr{=head1 NAME}, 'POD body was JSON encoded' ); | ||
} | ||
else { | ||
ok( $jsdata->{message}, 'error response body was JSON encoded' ); | ||
like( | ||
$js_data, | ||
qr{<h1 id="NAME">NAME</h1>}, | ||
'HTML body was JSON encoded' | ||
); | ||
} | ||
} | ||
}; | ||
|
||
test_psgi app, sub { | ||
my $cb = shift; | ||
else { | ||
ok( $js_data->{message}, 'error response body was JSON encoded' ); | ||
} | ||
} | ||
|
||
my $res; | ||
{ | ||
my $path = '/pod/BadPod'; | ||
ok( $res = $cb->( GET $path), "GET $path" ); | ||
my $res = $test->request( GET $path ); | ||
ok( $res, "GET $path" ); | ||
is( $res->code, 200, 'code 200' ); | ||
unlike( $res->content, qr/<div[^>]*id="pod-errors"/, | ||
'no POD errors section' ); | ||
|
||
$path = '/pod/BadPod?show_errors=1'; | ||
ok( $res = $cb->( GET $path), "GET $path" ); | ||
} | ||
|
||
{ | ||
my $path = '/pod/BadPod?show_errors=1'; | ||
my $res = $test->request( GET $path); | ||
ok( $res, "GET $path" ); | ||
is( $res->code, 200, 'code 200' ); | ||
like( $res->content, qr/<div[^>]*id="pod-errors"/, | ||
'got POD errors section' ); | ||
|
@@ -105,6 +118,6 @@ test_psgi app, sub { | |
is( scalar(@err), 2, 'two parse errors listed ' ); | ||
like( $err[0], qr/=head\b/, 'first error mentions =head' ); | ||
like( $err[1], qr/C</, 'first error mentions C< ... >' ); | ||
}; | ||
} | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
what is
MetaCPAN::Server::App
? can't find it