diff --git a/Changes b/Changes index 82b19fc5..25c78927 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,8 @@ Revision history for Perl module Mojo::WebSocketProxy {{$NEXT}} Add warning with transaction details failure, if available. + Add optional localization callback + Use 'Mojo::JSON' instead of 3rd-party JSON-handling modules Don't bother removing args from req storage after SanityCheckFailed. 0.07 2017-08-29 03:04:42 UTC 2017 diff --git a/cpanfile b/cpanfile index 17f00d50..d883fa9c 100644 --- a/cpanfile +++ b/cpanfile @@ -2,8 +2,6 @@ requires 'Class::Method::Modifiers'; requires 'Data::UUID'; requires 'Future'; requires 'Guard'; -requires 'JSON'; -requires 'JSON::XS'; requires 'MojoX::JSON::RPC'; requires 'Mojolicious'; requires 'Scalar::Util'; diff --git a/dist.ini b/dist.ini index 42a3da6c..1fbe5ae7 100644 --- a/dist.ini +++ b/dist.ini @@ -44,6 +44,8 @@ perltidyrc = t/rc/.perltidyrc [Test::Compile] [Test::Synopsis] [Test::EOL] +finder = :InstallModules +finder = :ExecFiles [Test::Version] [Test::Pod::LinkCheck] [PodCoverageTests] diff --git a/lib/Mojo/WebSocketProxy/CallingEngine.pm b/lib/Mojo/WebSocketProxy/CallingEngine.pm index 44ffb2e5..36c3f848 100644 --- a/lib/Mojo/WebSocketProxy/CallingEngine.pm +++ b/lib/Mojo/WebSocketProxy/CallingEngine.pm @@ -3,9 +3,9 @@ package Mojo::WebSocketProxy::CallingEngine; use strict; use warnings; +use Mojo::JSON qw(encode_json); use MojoX::JSON::RPC::Client; use Guard; -use JSON; ## VERSION diff --git a/lib/Mojo/WebSocketProxy/Dispatcher.pm b/lib/Mojo/WebSocketProxy/Dispatcher.pm index 49b998d8..963ea426 100644 --- a/lib/Mojo/WebSocketProxy/Dispatcher.pm +++ b/lib/Mojo/WebSocketProxy/Dispatcher.pm @@ -4,13 +4,13 @@ use strict; use warnings; use Mojo::Base 'Mojolicious::Controller'; +use Mojo::JSON qw(encode_json); use Mojo::WebSocketProxy::Parser; use Mojo::WebSocketProxy::Config; use Mojo::WebSocketProxy::CallingEngine; use Class::Method::Modifiers; -use Mojo::JSON qw(encode_json); use Future::Utils qw/fmap/; use Scalar::Util qw(blessed); use Variable::Disposition qw(dispose retain retain_future); diff --git a/lib/Mojolicious/Plugin/WebSocketProxy.pm b/lib/Mojolicious/Plugin/WebSocketProxy.pm index a0a00300..fbd8f075 100644 --- a/lib/Mojolicious/Plugin/WebSocketProxy.pm +++ b/lib/Mojolicious/Plugin/WebSocketProxy.pm @@ -14,6 +14,8 @@ sub register { die 'No base path found!' unless $config->{base_path}; + my $localizer = $config->{localizer} || sub { $_[1] }; + my $url_setter; $url_setter = delete $config->{url} if $config->{url} and ref($config->{url}) eq 'CODE'; $app->helper( @@ -24,12 +26,11 @@ sub register { }); $app->helper( wsp_error => sub { - shift; # $c - my ($msg_type, $code, $message, $details) = @_; + my ($c, $msg_type, $code, $message, $details) = @_; my $error = { code => $code, - message => $message + message => $localizer->($c, $message), }; $error->{details} = $details if ref($details) eq 'HASH' && keys %$details; @@ -84,6 +85,10 @@ Mojolicious::Plugin::WebSocketProxy my $self = shift; $self->plugin( 'web_socket_proxy' => { + localizer => sub { + my ($c, $error) = @_; + return $translate_somehow->($error) // $error; + }, actions => [ ['json_key', {some_param => 'some_value'}] ], diff --git a/t/05_basic.t b/t/05_basic.t index 6185e628..1db107f9 100644 --- a/t/05_basic.t +++ b/t/05_basic.t @@ -3,8 +3,6 @@ use strict; use warnings; -use JSON; - # Mojolicious app for testing package WebsocketProxy; @@ -48,6 +46,7 @@ use Test::More; use Test::Mojo; use Test::MockModule; use Test::MockObject; +use Mojo::JSON qw(decode_json encode_json); # Mocking RPC client to catch RPC calls my ($url, $call_params, $fake_rpc_response, $fake_rpc_client, $rpc_client_mock, $rpc_response); diff --git a/t/10-before_forward.t b/t/10-before_forward.t index 2dd9c76e..a7082fdf 100644 --- a/t/10-before_forward.t +++ b/t/10-before_forward.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/11-timeout.t b/t/11-timeout.t index 355f617c..125fddc5 100644 --- a/t/11-timeout.t +++ b/t/11-timeout.t @@ -6,7 +6,7 @@ local $ENV{MOJO_WEBSOCKETPROXY_TIMEOUT} = 1; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/12-before_send_api_response.t b/t/12-before_send_api_response.t index 0e97e6ab..a9762a8a 100644 --- a/t/12-before_send_api_response.t +++ b/t/12-before_send_api_response.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/13-action-not-found.t b/t/13-action-not-found.t index cff0f441..00154924 100644 --- a/t/13-action-not-found.t +++ b/t/13-action-not-found.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/14-bad-request.t b/t/14-bad-request.t index 6c94eafc..9dbbd27a 100644 --- a/t/14-bad-request.t +++ b/t/14-bad-request.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/15-simple-success.t b/t/15-simple-success.t index eea9c1f2..c3e28901 100644 --- a/t/15-simple-success.t +++ b/t/15-simple-success.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/16-auth-error-sample.t b/t/16-auth-error-sample.t index ae2649b8..95a58ff1 100644 --- a/t/16-auth-error-sample.t +++ b/t/16-auth-error-sample.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/17-instead_of_forward.t b/t/17-instead_of_forward.t index 30784fc6..edf7dea0 100644 --- a/t/17-instead_of_forward.t +++ b/t/17-instead_of_forward.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/18-per-action_before-forward.t b/t/18-per-action_before-forward.t index 805e508d..098a36c7 100644 --- a/t/18-per-action_before-forward.t +++ b/t/18-per-action_before-forward.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/19-rpc_response_cb.t b/t/19-rpc_response_cb.t index 0e98292a..1eb3a965 100644 --- a/t/19-rpc_response_cb.t +++ b/t/19-rpc_response_cb.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/20-after_got_rpc_response.t b/t/20-after_got_rpc_response.t index 32ecf08b..dfa9a31e 100644 --- a/t/20-after_got_rpc_response.t +++ b/t/20-after_got_rpc_response.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/21-mojo-stash.t b/t/21-mojo-stash.t index 24920ce2..feced418 100644 --- a/t/21-mojo-stash.t +++ b/t/21-mojo-stash.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/22-override-response.t b/t/22-override-response.t index 6537312d..0739f20b 100644 --- a/t/22-override-response.t +++ b/t/22-override-response.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; diff --git a/t/23-binary-frame.t b/t/23-binary-frame.t index f1ad1a25..fae06f4b 100644 --- a/t/23-binary-frame.t +++ b/t/23-binary-frame.t @@ -4,7 +4,7 @@ use warnings; use t::TestWSP qw/test_wsp/; use Test::More; use Test::Mojo; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); use Mojo::IOLoop; use Future; use Path::Tiny; diff --git a/t/24-localize.t b/t/24-localize.t new file mode 100644 index 00000000..7ca223e2 --- /dev/null +++ b/t/24-localize.t @@ -0,0 +1,41 @@ +use strict; +use warnings; + +use t::TestWSP qw/test_wsp/; +use Test::More; +use Test::Mojo; +use Mojo::JSON qw(decode_json encode_json); +use Mojo::IOLoop; +use Future; + +package t::FrontEnd { + use base 'Mojolicious'; + # does not matter + sub startup { + my $self = shift; + $self->helper('l' => sub { + my ($c, $message) = @_; + return "[localized] : $message"; + }); + $self->plugin( + 'web_socket_proxy' => { + localizer => sub { shift->l(shift) }, + actions => [ + ['success', {stash_params => [qw/lang/]}], + ], + base_path => '/api', + url => $ENV{T_TestWSP_RPC_URL} // die("T_TestWSP_RPC_URL is not defined"), + } + ); + } +}; + +test_wsp { + my ($t) = @_; + $t->websocket_ok('/api' => {}); + $t->send_ok({json => 'invalid', lang => 'ru'})->message_ok; + is decode_json($t->message->[1])->{error}->{code}, 'BadRequest'; + is decode_json($t->message->[1])->{error}->{message}, '[localized] : The application sent an invalid request.'; +} 't::FrontEnd'; + +done_testing; diff --git a/t/SampleRPC.pm b/t/SampleRPC.pm index b1f676b0..30b65f49 100644 --- a/t/SampleRPC.pm +++ b/t/SampleRPC.pm @@ -5,7 +5,7 @@ use warnings; use MojoX::JSON::RPC::Service; use Mojo::Base 'Mojolicious'; -use JSON::XS; +use Mojo::JSON qw(decode_json encode_json); sub startup { my $self = shift;