Skip to content

Add localizer; use JSON::MaybeXS; avoid EOL-check for tests #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 2 additions & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ perltidyrc = t/rc/.perltidyrc
[Test::Compile]
[Test::Synopsis]
[Test::EOL]
finder = :InstallModules
finder = :ExecFiles
[Test::Version]
[Test::Pod::LinkCheck]
[PodCoverageTests]
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/WebSocketProxy/CallingEngine.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/WebSocketProxy/Dispatcher.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions lib/Mojolicious/Plugin/WebSocketProxy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;

Expand Down Expand Up @@ -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'}]
],
Expand Down
3 changes: 1 addition & 2 deletions t/05_basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
use strict;
use warnings;

use JSON;

# Mojolicious app for testing
package WebsocketProxy;

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion t/10-before_forward.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/11-timeout.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/12-before_send_api_response.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/13-action-not-found.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/14-bad-request.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/15-simple-success.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/16-auth-error-sample.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/17-instead_of_forward.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/18-per-action_before-forward.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/19-rpc_response_cb.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/20-after_got_rpc_response.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/21-mojo-stash.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/22-override-response.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion t/23-binary-frame.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions t/24-localize.t
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion t/SampleRPC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down