Skip to content

Commit

Permalink
Item13897: Made JsonrpcTests working.
Browse files Browse the repository at this point in the history
- Fixed a bug where JsonRpcContrib::Server was resetting web/topic
despite they're been handled by Request::JSON now.

- Trigger wasn't called for Foswiki::Request::method attribute in build
code.

- Fixed JsonRpc code being treated as a string by JSON::encode()
sometimes.
  • Loading branch information
vrurg committed Oct 31, 2016
1 parent 2ecbed8 commit 43b778c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
6 changes: 2 additions & 4 deletions JsonRpcContrib/lib/Foswiki/Contrib/JsonRpcContrib/Response.pm
Expand Up @@ -22,10 +22,8 @@ use Assert;
use Compress::Zlib ();
use JSON ();

use Moo;
use namespace::clean;
use Foswiki::Class qw(app);
extends qw(Foswiki::Object);
with qw(Foswiki::AppObject);

use constant TRACE => 0; # toggle me

Expand Down Expand Up @@ -107,7 +105,7 @@ sub encode {
$message = {
jsonrpc => "2.0",
error => {
code => $code,
code => $code + 0, # Sometimes code comes as a string, not int.
message => $message,
},
};
Expand Down
20 changes: 12 additions & 8 deletions JsonRpcContrib/lib/Foswiki/Contrib/JsonRpcContrib/Server.pm
Expand Up @@ -82,16 +82,20 @@ sub dispatch {
return;
}

# ---------- OBSOLETE BEGIN ----------
# XXX This code is obsolete since the request object is handling this logic
# now and provides valid defined web.topic for the application.
# get topic parameter and set the location overriding any
# other value derived from the namespace param
my $topic = $request->param('topic')
|| $Foswiki::cfg{HomeTopicName};
my ( $jsrpcWeb, $jsrpcTopic ) =
Foswiki::Func::normalizeWebTopicName( $Foswiki::cfg{UsersWebName},
$topic );
$app->request->web($jsrpcWeb);
$app->request->topic($jsrpcTopic);
$this->writeDebug("topic=$topic") if TRACE;
#my $topic = $request->param('topic')
# || $Foswiki::cfg{HomeTopicName};
#my ( $jsrpcWeb, $jsrpcTopic ) =
# Foswiki::Func::normalizeWebTopicName( $Foswiki::cfg{UsersWebName},
# $topic );
#$app->request->web($jsrpcWeb);
#$app->request->topic($jsrpcTopic);
#$this->writeDebug("topic=$topic") if TRACE;
# ---------- OBSOLETE END ---------

# get handler for this namespace
my $handler = $this->getHandler($request);
Expand Down
4 changes: 2 additions & 2 deletions JsonRpcContrib/lib/Foswiki/Request/JSON.pm
Expand Up @@ -412,11 +412,11 @@ around _trigger_method => sub {
my $this = shift;
my ($value) = @_;

if ( defined $value && $this->_has_jsondata && lc($value) ne 'post' ) {
if ( defined $value && $this->has_jsondata && lc($value) ne 'post' ) {
$this->jsonerror(
new Foswiki::Contrib::JsonRpcContrib::Error(
code => -32600,
text => "Method must be POST, not " . $value
text => "Method must be POST, not " . uc($value)
)
);
}
Expand Down
5 changes: 2 additions & 3 deletions JsonRpcContrib/test/unit/JsonRpcContrib/JsonrpcTests.pm
Expand Up @@ -11,8 +11,7 @@ use Foswiki::EngineException();
use Carp();
use Try::Tiny;

use Moo;
use namespace::clean;
use Foswiki::Class;
extends qw( FoswikiFnTestCase );

use Unit::Request::JSON;
Expand Down Expand Up @@ -294,7 +293,7 @@ sub test_post_required {
};

$this->assert_matches( qr/"code" : -32600/, $response );
$this->assert_matches( qr/"message" : "Method must be POST"/, $response );
$this->assert_matches( qr/"message" : "Method must be POST,/, $response );

return;
}
Expand Down
9 changes: 7 additions & 2 deletions core/lib/Foswiki/Request.pm
Expand Up @@ -1268,8 +1268,13 @@ sub _establishTopic {
}

sub _establishMethod {
my $this = shift;
return $this->app->engine->connectionData->{method};
my $this = shift;
my $method = $this->app->engine->connectionData->{method};

# Call it manually because triggers are not called for default/build.
$this->_trigger_method($method);

return $method;
}

sub _establishUploads {
Expand Down

0 comments on commit 43b778c

Please sign in to comment.