File: marshal.t

package info (click to toggle)
libjson-rpc-common-perl 0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 244 kB
  • sloc: perl: 1,481; makefile: 2
file content (68 lines) | stat: -rw-r--r-- 1,532 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/perl

use strict;
use warnings;

use Test::More 'no_plan';

use Test::Exception;

use JSON;

use JSON::RPC::Common::Marshal::Text;

{
	my $m_json = JSON::RPC::Common::Marshal::Text->new;

	isa_ok( $m_json, "JSON::RPC::Common::Marshal::Text" );

	my $call = $m_json->json_to_message(<<JSON);
{	"jsonrpc": "2.0"
,	"method":  "oink"
,	"params":  { "foo": 3 }
,	"id":      "dancing"
}
JSON

	ok( $call, "JSON text parsed into Procedure Call" );
	isa_ok( $call, "JSON::RPC::Common::Procedure::Call" );

	is( $call->version, "2.0", "version" );
	is( $call->id, "dancing", "id" );

	my $res = $call->return_result("bah");

	my $text = $m_json->message_to_json($res);

	is_deeply(
		from_json($text),
		{
			jsonrpc => "2.0",
			id      => "dancing",
			result  => "bah",
		},
		"result encoding",
	);

	ok( $m_json->json_to_call('{"jsonrpc":"2.0","params":{"foo":3},"method":"hello"}'), "notification (2.0)" );

	my $no_ver = $m_json->json_to_call('{"id":"moo","params":[],"method":"hello"}');
	ok( $no_ver, "missing version parsed" );
	is( $no_ver->version, "1.0", "version 1.0" );

	my %required = (
		method => '{"jsonrpc":"2.0","params":{},"id":3}', # missing method
		params => '{"method":"hello","id":3}', # missing params
		id => '{"params":[],"method":"hello"}', # missing id (version 1.0)
	);

	foreach my $req ( keys %required ) {
		throws_ok {
			$m_json->json_to_call($required{$req});
		} qr/\Q$req\E.*required/, "required param $req";
	}

	dies_ok { $m_json->json_to_call("adtjhat3!!!!") } "JSON parse error";
}