App-Dochazka-REST

 view release on metacpan or  search on metacpan

t/dispatch/top.t  view on Meta::CPAN


note( '- with illegal JSON' );
$status = req( $test, 400, 'root', 'POST', 'echo', '{ "username": "foo", "password": "bar"' );

note( '- with empty request body, as demo' );
$status = req( $test, 403, 'demo', 'POST', 'echo' );

note( '- with empty request body' );
$status = req( $test, 200, 'root', 'POST', 'echo' );
is( $status->level, 'OK' );
is( $status->code, 'ECHO_REQUEST_ENTITY' );
ok( exists $status->{'payload'} );
is( $status->payload, undef );

note( 'DELETE echo -> 405' );
$status = req( $test, 405, 'demo', 'DELETE', 'echo' );
$status = req( $test, 405, 'root', 'DELETE', 'echo' );

note( '=============================' );
note( '"forbidden" resource' );
note( '=============================' );
docu_check($test, "forbidden");
foreach my $user ( qw( demo root ) ) {
    foreach my $method ( qw( GET PUT POST DELETE ) ) {
        $status = req( $test, 403, 'demo', 'GET', 'forbidden' );
    }
}

note( '=============================' );
note( '"param/:type/:param" resource' );
note( '=============================' );
$base = "param";
docu_check($test, "param/:type/:param");

note( 'POST' );
is( $meta->META_DOCHAZKA_UNIT_TESTING, 1 );

$status = req( $test, 200, 'root', 'PUT', "$base/meta/META_DOCHAZKA_UNIT_TESTING", '"foobar"' );
is( $status->level, 'OK' );
is( $status->code, 'CELL_OVERWRITE_META_PARAM' );
is( $meta->META_DOCHAZKA_UNIT_TESTING, 'foobar' );

$status = req( $test, 200, 'root', 'PUT', "$base/meta/META_DOCHAZKA_UNIT_TESTING", '1' );
is( $status->level, 'OK' );
is( $status->code, 'CELL_OVERWRITE_META_PARAM' );
is( $meta->META_DOCHAZKA_UNIT_TESTING, 1 );

note( 'GET' );

note( 'non-existent and otherwise bogus parameters' );
foreach my $base ( "$base/meta", "$base/site" ) {
    foreach my $user ( qw( demo root ) ) {
        # these are bogus in that the resource does not exist
        req( $test, 400, $user, 'GET', "$base/" );
        req( $test, 400, $user, 'GET', "$base/META_DOCHAZKA_UNIT_TESTING/foobar" );
        req( $test, 400, $user, 'GET', "$base/bla bla bal" );
        req( $test, 400, $user, 'GET', "$base//////1/1/234/20" );
        req( $test, 400, $user, 'GET', "$base/{}" );
        req( $test, 400, $user, 'GET', "$base/-1" );
        req( $test, 400, $user, 'GET', "$base/0" );
        req( $test, 400, $user, 'GET', "$base/" . '\b\b\o\o\g\\' );
        req( $test, 400, $user, 'GET', "$base/" . '\b\b\o\o\\' );
        req( $test, 400, $user, 'GET', "$base/**0" );
        req( $test, 400, $user, 'GET', "$base/}lieutenant" );
        req( $test, 400, $user, 'GET', "$base/<HEAD><tail><body>&nbsp;" );
    }
    my $mapping = { "demo" => 403, "root" => 404 };
    foreach my $user ( qw( demo root ) ) {
        # these are bogus in that the parameter does not exist
        req( $test, $mapping->{$user}, $user, 'GET', "$base/DOCHEEEHAWHAZKA_appname" );
        req( $test, $mapping->{$user}, $user, 'GET', "$base/abc123" );
        req( $test, $mapping->{$user}, $user, 'GET', "$base/null" );
    }
}

note( 'metaparam-specific tests' );

note( '- try to use metaparam to access a site parameter' );
req( $test, 404, 'root', 'GET', "param/meta/DOCHAZKA_APPNAME" );

note( '- as root, existent parameter' );
$status = req( $test, 200, 'root', 'GET', 'param/meta/META_DOCHAZKA_UNIT_TESTING/' );
is( $status->level, 'OK' );
is( $status->code, 'MREST_PARAMETER_VALUE' );
is_deeply( $status->payload, { 'META_DOCHAZKA_UNIT_TESTING' => 1 } );

note( "- as root, existent parameter without trailing '/'" );
$status = req( $test, 200, 'root', 'GET', 'param/meta/META_DOCHAZKA_UNIT_TESTING' );
is( $status->level, 'OK' );
is( $status->code, 'MREST_PARAMETER_VALUE' );
is_deeply( $status->payload, { 'META_DOCHAZKA_UNIT_TESTING' => 1 } );

note( 'POST -> 405' );
foreach my $base ( "$base/meta", "$base/site" ) {
    foreach my $user ( qw( demo root ) ) {
        req( $test, 405, $user, 'POST', 'param/meta/META_DOCHAZKA_UNIT_TESTING' );
    }
}


note( '=============================' );
note( '"session" resource' );
note( '=============================' );
docu_check($test, "session");

note( 'GET session' );
$status = req( $test, 200, 'demo', 'GET', 'session' );
is( $status->level, 'OK' );
is( $status->code, 'DISPATCH_SESSION_DATA' );
ok( exists $status->payload->{'session'} );

note( 'N.B.: no session data when running via Plack::Test' );
#ok( exists $status->payload->{'session'}->{'ip_addr'} );
#ok( exists $status->payload->{'session'}->{'last_seen'} );
#ok( exists $status->payload->{'session'}->{'eid'} );

note( 'PUT, POST, DELETE -> 405' );
foreach my $user ( qw( demo root ) ) {
    foreach my $method ( qw( PUT POST DELETE ) ) {
        $status = req( $test, 405, $user, $method, 'session' );
    }



( run in 1.510 second using v1.01-cache-2.11-cpan-39bf76dae61 )