Skip to content

Commit

Permalink
Item13897: Experimenting with JSON fixes
Browse files Browse the repository at this point in the history
 - One unit test now appears to submit POSTDATA, but it's ignored.
 - Removed leading _ from jsondata .. the clearer didn't seem to like it
 - Added clearer for jsondata
 - Dropped leading _ from a call to jsonerror

The test doesn't work, but it gets a bit closer.  Ended the test with a
__END__ to avoid lots of try/catch syntax errors for now.   Configure
still works.
  • Loading branch information
gac410 committed Oct 19, 2016
1 parent 9cbca2f commit dd0cf5d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 36 deletions.
35 changes: 18 additions & 17 deletions JsonRpcContrib/lib/Foswiki/Request/JSON.pm
Expand Up @@ -81,11 +81,12 @@ has jsonerror => (
'jsonerror', 'Foswiki::Contrib::JsonRpcContrib::Error'
),
);
has _jsondata => (
has jsondata => (
is => 'rw',
lazy => 1,
predicate => 1,
isa => Foswiki::Object::isaHASH( '_jsondata', noUndef => 1, ),
isa => Foswiki::Object::isaHASH( 'jsondata', noUndef => 1, ),
clearer => 1,
builder => '_establishJSON',
);

Expand Down Expand Up @@ -129,12 +130,12 @@ around param => sub {

# Intercept POSTDATA assignment, and process the JSON data
if ( $key eq 'POSTDATA' && scalar @value ) {
$this->_clear_jsondata;
$this->clear_jsondata;
}

# If key doesn't exist in json data, the fall back to CGI.
return $this->_jsondata->{params}{$key}
if ( defined $this->_jsondata->{params}{$key} );
return $this->jsondata->{params}{$key}
if ( defined $this->jsondata->{params}{$key} );

# Process
return $orig->( $this, @p );
Expand Down Expand Up @@ -206,7 +207,7 @@ sub parseJSON {
=begin TML
---++ private objectMethod initFromString() -> %jsondata
Initializes the _jsondata hash by processing the POSTDATA from the request.
Initializes the jsondata hash by processing the POSTDATA from the request.
=cut

Expand All @@ -223,7 +224,7 @@ sub initFromString {
my $error = Foswiki::Exception::errorStr(
Foswiki::Exception::Fatal->transmute( $_, 0 ) );
$error =~ s/,? +at.*$//s;
$this->_jsonerror(
$this->jsonerror(
new Foswiki::Contrib::JsonRpcContrib::Error(
code => -32700,
text => "Parse error - invalid json-rpc request: $error"
Expand Down Expand Up @@ -280,8 +281,8 @@ sub jsonparam {
my ( $this, $key, $value ) = @_;

return unless defined $key;
$this->_jsondata->{params}{$key} = $value if defined $value;
return $this->_jsondata->{params}{$key};
$this->jsondata->{params}{$key} = $value if defined $value;
return $this->jsondata->{params}{$key};
}

=begin TML
Expand All @@ -295,8 +296,8 @@ the version string is replaced.
sub version {
my ( $this, $value ) = @_;

$this->_jsondata->{jsonrpc} = $value if defined $value;
return $this->_jsondata->{jsonrpc} || '';
$this->jsondata->{jsonrpc} = $value if defined $value;
return $this->jsondata->{jsonrpc} || '';
}

=begin TML
Expand All @@ -310,8 +311,8 @@ the id is replaced.
sub id {
my ( $this, $value ) = @_;

$this->_jsondata->{id} = $value if defined $value;
return $this->_jsondata->{id} || '';
$this->jsondata->{id} = $value if defined $value;
return $this->jsondata->{id} || '';
}

=begin TML
Expand All @@ -321,7 +322,7 @@ Returns the parameters hash from the parsed JSON data.
=cut

sub params {
return $_[0]->_jsondata->{params};
return $_[0]->jsondata->{params};
}

=begin TML
Expand Down Expand Up @@ -395,7 +396,7 @@ around _trigger_method => sub {
my $this = shift;
my ($value) = @_;

if ( defined $value && $this->_has_jsondata && lc($value) ne 'post' ) {
if ( defined $value && $this->_hasjsondata && lc($value) ne 'post' ) {
$this->jsonerror(
new Foswiki::Contrib::JsonRpcContrib::Error(
code => -32600,
Expand All @@ -408,15 +409,15 @@ around _trigger_method => sub {
sub _trigger_jsonmethod {
my $this = shift;
my ($value) = @_;
$this->_jsondata->{method} = $value;
$this->jsondata->{method} = $value;

# SMELL method and jsonmethod must not be mixed up!
#$this->method($value);
}

sub _establishJSONMethod {
my $this = shift;
return $this->_jsondata->{method};
return $this->jsondata->{method};
}

sub _establishNamespace {
Expand Down
75 changes: 56 additions & 19 deletions JsonRpcContrib/test/unit/JsonRpcContrib/JsonrpcTests.pm
Expand Up @@ -2,29 +2,33 @@
# Author: Crawford Currie

package JsonrpcTests;
use strict;
use warnings;
use FoswikiFnTestCase();
our @ISA = qw( FoswikiFnTestCase );
use v5.14;

use Assert;
use Foswiki();
use Foswiki::Func();
use Foswiki::EngineException();
use Carp();
use Error ':try';
use Try::Tiny;

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

use Unit::Request::JSON;

our $UI_FN;

sub set_up {
around set_up => sub {
my $orig = shift;
my $this = shift;
$this->SUPER::set_up();
$UI_FN ||= $this->getUIFn('jsonrpc');
$orig->($this);

Foswiki::Contrib::JsonRpcContrib::registerMethod( __PACKAGE__, 'trial',
\&json_handler );

return;
}
};

# A simple REST handler
sub json_handler {
Expand Down Expand Up @@ -77,22 +81,55 @@ sub json_authtest {
# Simple jsonrpc, using posted data
sub test_simple_postdata {
my $this = shift;
Foswiki::Contrib::JsonRpcContrib::registerMethod( __PACKAGE__, 'trial',
\&json_handler );

my $query = Unit::Request::JSON->new( { action => ['jsonrpc'], } );
$query->path_info( '/' . __PACKAGE__ . '/trial' );
$query->method('post');
$query->param( 'POSTDATA',
my $response;

try {
($response) = $this->capture(
sub {

$this->createNewFoswikiApp(
requestParams => {
initializer => {
action => ['jsonrpc'],
uri => '/' . __PACKAGE__ . '/trial',
path_info => '/' . __PACKAGE__ . '/trial',
},
},
engineParams => {
initialAttributes => {
uri => '/' . __PACKAGE__ . "/trial",
path_info => '/' . __PACKAGE__ . "/trial",
method => 'post',
action => 'jsonrpc',
},
},
);
$this->app->request->param(
-name => 'POSTDATA',
-value =>
'{"jsonrpc":"2.0","method":"trial","params":{"wizard":"ScriptHash","method":"verify","keys":"{ScriptUrlPaths}{view}","set":{},"topic":"System.WebChanges","cfgpassword":"xxxxxxx"},"id":"iCall-verify_6"}'
);
$this->createNewFoswikiSession( $this->{test_user_login}, $query );
my ( $response, $result, $out, $err ) =
$this->capture( $UI_FN, $this->{session} );
);

return $this->app->handleRequest;
},
);
}
catch {
my $e = $_;
if ( ref($e) && $e->isa('Foswiki::EngineException') ) {
$this->assert_equals( 401, $e->status, $e->stringify );
}
else {
$e->rethrow;
}
};

$this->assert_matches( qr/"result" : "SUCCESS"/, $response );
return;
}
1;
__END__
# Simple jsonrpc, using query params
sub test_simple_query_params {
Expand Down

0 comments on commit dd0cf5d

Please sign in to comment.