Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1583 from CPAN-API/json-view-errors
Json view errors
- Loading branch information
Showing
3 changed files
with
81 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package MetaCPAN::Web::Controller::Test; | ||
|
||
use Moose; | ||
BEGIN { extends 'MetaCPAN::Web::Controller' } | ||
|
||
sub _json_body { | ||
my ( $self, $c ) = @_; | ||
JSON::MaybeXS->new->utf8->decode( | ||
do { local $/; $c->req->body->getline } | ||
); | ||
} | ||
|
||
sub json_echo : Local { | ||
my ( $self, $c ) = @_; | ||
|
||
$c->stash->{echo} = $self->_json_body($c); | ||
|
||
$c->detach( $c->view('JSON') ); | ||
} | ||
|
||
__PACKAGE__->meta->make_immutable; | ||
|
||
1; |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use strict; | ||
use warnings; | ||
use lib 't/lib'; | ||
use Test::More; | ||
use HTTP::Request::Common 'POST'; | ||
use MetaCPAN::Web::Test; | ||
use JSON::MaybeXS; | ||
|
||
sub post_json { | ||
POST( | ||
shift(), | ||
Content => shift(), | ||
Content_type => 'application/json', | ||
@_ | ||
); | ||
} | ||
|
||
my $cb; | ||
|
||
sub echo_json_ok { | ||
my ( $json, $desc ) = @_; | ||
|
||
subtest $desc => sub { | ||
my $exp = decode_json($json); | ||
|
||
ok( my $res = $cb->( post_json '/test/json_echo', $json ), | ||
'http post' ); | ||
is( $res->code, 200, '200 OK' ); | ||
|
||
my $res_json = $res->content; | ||
ok( my $obj = eval { decode_json($res_json); }, 'decode json' ); | ||
|
||
note "received: $res_json"; | ||
|
||
is_deeply $obj, { echo => $exp }, "json passed through unchanged"; | ||
}; | ||
} | ||
|
||
test_psgi app, sub { | ||
$cb = shift; # global | ||
}; | ||
|
||
{ | ||
|
||
echo_json_ok( q!{"test": 1}!, 'minimal json' ); | ||
|
||
echo_json_ok( q!{"test": [1, 2, {"hash": {}}] }!, 'arrays and hashes' ); | ||
|
||
echo_json_ok( q!{"test": true}!, 'booleans' ); | ||
} | ||
|
||
done_testing; |