Amon2-Plugin-Web-CpanelJSON

 view release on metacpan or  search on metacpan

t/01_render_json.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;
use Cpanel::JSON::XS::Type;

{
    package MyApp::Web;
    use parent qw(Amon2 Amon2::Web);
    __PACKAGE__->load_plugins('Web::CpanelJSON');
    sub encoding { 'utf-8' }
}

my $app = MyApp::Web->to_app;

test_psgi $app, sub {
    my $cb  = shift;

    no warnings qw(once);

    subtest 'no arguments' => sub {
        local *MyApp::Web::dispatch = sub {
            my $c = shift;
            $c->render_json()
        };

        my $res = $cb->(GET "/");
        is $res->code, 500;
    };

    subtest 'empty data' => sub {
        local *MyApp::Web::dispatch = sub {
            my $c = shift;
            $c->render_json({})
        };

        my $res = $cb->(GET "/");
        is $res->code, 200;
        is $res->content, '{}';
    };

    subtest 'simple data' => sub {
        local *MyApp::Web::dispatch = sub {
            my $c = shift;
            $c->render_json({foo => '123'})
        };

        my $res = $cb->(GET "/");
        is $res->code, 200;
        is $res->content, '{"foo":"123"}';
    };

    subtest 'with JSON_TYPE_INT' => sub {
        local *MyApp::Web::dispatch = sub {
            my $c = shift;
            $c->render_json({foo => '123'}, {foo => JSON_TYPE_INT})
        };

        my $res = $cb->(GET "/");
        is $res->code, 200;
        is $res->content, '{"foo":123}';
    };

    subtest 'with JSON_TYPE_BOOL' => sub {
        local *MyApp::Web::dispatch = sub {



( run in 1.287 second using v1.01-cache-2.11-cpan-140bd7fdf52 )