APISchema

 view release on metacpan or  search on metacpan

eg/bmi.psgi  view on Meta::CPAN

my $router = do {
    my $generator = APISchema::Generator::Router::Simple->new;
    $generator->generate_router($schema);
};

my $app = sub {
    my $env = shift;

    my $match = $router->match($env);

    return [404, [], ['not found']] unless $match;

    my $req = Plack::Request->new($env);

    my $payload = decode_json($req->content);

    my $bmi = $payload->{weight} / ($payload->{height} * $payload->{height});

    return [200, ['Content-Type' => 'application/json'], [encode_json({value => $bmi})]];
};

lib/Plack/App/APISchema/MockServer.pm  view on Meta::CPAN

use APISchema::Generator::Markdown::ExampleFormatter;

sub call {
    my ($self, $env) = @_;

    my $req = Plack::Request->new($env);

    my ($matched, $router_simple_route) = $self->router->routematch($env);

    unless ($matched) {
        return [404, ['Content-Type' => 'text/plain; charset=utf-8'], ['not found']];
    }

    my $root = $self->schema->get_resource_root;

    my $route = $self->schema->get_route_by_name($router_simple_route->name);

    my $default_code = $route->default_responsible_code;
    my $response_resource = $route->canonical_response_resource($root, [
        $default_code
    ]);

t/Plack-App-APISchema-MockServer.t  view on Meta::CPAN

             is $res->code, 200;
             is $res->header('content-type'), 'application/json; charset=utf-8';
             is $res->content, q!{"value":19.5}!;
         }
     };

     subtest 'when invalid request' => sub {
         test_psgi $app => sub {
             my $server = shift;
             my $res = $server->(POST '/notfound');
             is $res->code, 404;
             is $res->header('content-type'), 'text/plain; charset=utf-8';
             is $res->content, q!not found!;
         }
     };
}

sub when_encoding_is_specified : Tests {
    my $schema = t::test::fixtures::prepare_bmi;
    $schema->register_route(
        method => 'POST',



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