Skip to content

Commit

Permalink
fixed RFC 6901 compliance of Mojo::JSON::Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 3, 2013
1 parent 4ad06cc commit 8bad5a2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -7,6 +7,7 @@
- Improved WebSocket send method to stringify objects. (jberger)
- Improved documentation.
- Improved tests.
- Fixed RFC 6901 compliance of Mojo::JSON::Pointer.

3.91 2013-03-17
- Improved bad charset handling in Mojo::DOM::HTML.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/JSON/Pointer.pm
Expand Up @@ -12,7 +12,7 @@ sub _pointer {

$pointer = decode('UTF-8', url_unescape $pointer);
return $data unless $pointer =~ s!^/!!;
for my $p (split '/', $pointer) {
for my $p ($pointer eq '' ? ($pointer) : (split '/', $pointer)) {
$p =~ s/~0/~/g;
$p =~ s!~1!/!g;

Expand Down
2 changes: 1 addition & 1 deletion lib/Test/Mojo.pm
Expand Up @@ -674,7 +674,7 @@ Opposite of C<json_has>.
=head2 json_is
$t = $t->json_is('/' => {foo => [1, 2, 3]});
$t = $t->json_is('' => {foo => [1, 2, 3]});
$t = $t->json_is('/foo' => [1, 2, 3]);
$t = $t->json_is('/foo/1' => 2, 'right value');
Expand Down
32 changes: 32 additions & 0 deletions t/mojo/json_pointer.t
Expand Up @@ -61,4 +61,36 @@ is $pointer->get(
[{'f~o~o~/b~' => {'a~' => {'r' => 'baz'}}}] => '/0/f~0o~0o~0~1b~0/a~0/r'),
'baz', '"/0/f~0o~0o~0~1b~0/a~0/r" is "baz"';

# RFC 6901
my $hash = {
foo => ['bar', 'baz'],
'' => 0,
'a/b' => 1,
'c%d' => 2,
'e^f' => 3,
'g|h' => 4,
'i\\j' => 5,
'k"l' => 6,
' ' => 7,
'm~n' => 8
};
is_deeply $pointer->get($hash, ''), $hash, 'empty pointer is whole document';
is_deeply $pointer->get($hash, '/foo'), ['bar', 'baz'],
'"/foo" is "["bar", "baz"]"';
is $pointer->get($hash, '/foo/0'), 'bar', '"/foo/0" is "bar"';
is $pointer->get($hash, '/'), 0, '"/" is 0';
is $pointer->get($hash, '/a~1b'), 1, '"/a~1b" is 1';
is $pointer->get($hash, '/c%25d'), 2, '"/c%25d" is 2';
is $pointer->get($hash, '/e^f'), 3, '"/e^f" is 3';
is $pointer->get($hash, '/e%5Ef'), 3, '"/e%5Ef" is 3';
is $pointer->get($hash, '/g|h'), 4, '"/g|h" is 4';
is $pointer->get($hash, '/g%7Ch'), 4, '"/g%7Ch" is 4';
is $pointer->get($hash, '/i\\j'), 5, '"/i\\\\j" is 5';
is $pointer->get($hash, '/i%5Cj'), 5, '"/i%5Cj" is 5';
is $pointer->get($hash, '/k"l'), 6, '"/k\\"l" is 6';
is $pointer->get($hash, '/k%22l'), 6, '"/k%22l" is 6';
is $pointer->get($hash, '/ '), 7, '"/ " is 7';
is $pointer->get($hash, '/%20'), 7, '"/%20" is 7';
is $pointer->get($hash, '/m~0n'), 8, '"/m~0n" is 8';

done_testing();
4 changes: 2 additions & 2 deletions t/mojolicious/websocket_lite_app.t
Expand Up @@ -133,10 +133,10 @@ $t->get_ok('/echo')->status_is(200)->content_is('plain echo!');
# JSON roundtrips
$t->websocket_ok('/json')
->send_ok({text => j({test => 23, snowman => ''})})
->message_ok->json_message_is('/' => {test => 24, snowman => ''})
->message_ok->json_message_is('' => {test => 24, snowman => ''})
->json_message_has('/test')->json_message_hasnt('/test/2')
->send_ok({binary => j([1, 2, 3])})
->message_ok->json_message_is('/' => [1, 2, 3, 4], 'right content')
->message_ok->json_message_is('' => [1, 2, 3, 4], 'right content')
->send_ok({binary => j([1, 2, 3])})
->message_ok->json_message_has('/2', 'has two elements')
->json_message_is('/2' => 3)->json_message_hasnt('/5', 'not five elements')
Expand Down

0 comments on commit 8bad5a2

Please sign in to comment.