Skip to content

Commit

Permalink
new URI tests, fix resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Beaver authored and Greg Beaver committed Jul 28, 2011
1 parent 3eb5cdd commit f0dd303
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Pyrus/JsonSchema/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ function pctDecUnreserved($str)
{
$newStr = "";
$i = 0;
$str = $str[0];

while ($i < strlen($str)) {
$c = hexdec(substr($str, $i + 1, 2));
Expand Down Expand Up @@ -569,7 +570,7 @@ function resolveComponents(Components $base, Components $relative, Options $opti
} else if (!$base->path) {
$target->path = $relative->path;
} else {
if (strpos($base->path, '/')) {
if (strpos($base->path, '/') !== false) {
$target->path = substr($base->path, 0, strrpos($base->path, "/") + 1) . $relative->path;
} else {
$target->path = $relative->path;
Expand Down
17 changes: 17 additions & 0 deletions tests/Pyrus/JsonSchema/URI/uri_resolve1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
JsonSchema: URI - resolve (1) with scheme
--FILE--
<?php
require dirname(__FILE__) . '/setup.php.inc';
$base = "http://a/b/c/d;p?q";

$test->assertEquals('g:h', $uri->resolve($base, 'g:h'), 'g:h');
$test->assertEquals('http://a/b/c/g', $uri->resolve($base, 'g'), 'g');
$test->assertEquals('http://a/b/c/g', $uri->resolve($base, './g'), './g');
$test->assertEquals('http://a/b/c/g/', $uri->resolve($base, 'g/'), 'g/');
$test->assertEquals('http://a/g', $uri->resolve($base, '/g'), '/g');
$test->assertEquals('http://g', $uri->resolve($base, '//g'), '//g');
?>
===DONE===
--EXPECT--
===DONE===
20 changes: 20 additions & 0 deletions tests/Pyrus/JsonSchema/URI/uri_resolve2_queryfragment.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
JsonSchema: URI - resolve (2) with query and fragment
--FILE--
<?php
require dirname(__FILE__) . '/setup.php.inc';
$base = "http://a/b/c/d;p?q";

$test->assertEquals('http://a/b/c/d;p?y', $uri->resolve($base, '?y'), '?y');
$test->assertEquals('http://a/b/c/g?y', $uri->resolve($base, 'g?y'), 'g?y');
$test->assertEquals('http://a/b/c/d;p?q#s', $uri->resolve($base, '#s'), '#s');
$test->assertEquals('http://a/b/c/g#s', $uri->resolve($base, 'g#s'), 'g#s');
$test->assertEquals('http://a/b/c/g?y#s', $uri->resolve($base, 'g?y#s'), 'g?y#s');
$test->assertEquals('http://a/b/c/;x', $uri->resolve($base, ';x'), ';x');
$test->assertEquals('http://a/b/c/g;x', $uri->resolve($base, 'g;x'), 'g;x');
$test->assertEquals('http://a/b/c/g;x?y#s', $uri->resolve($base, 'g;x?y#s'), 'g;x?y#s');
$test->assertEquals('http://a/b/c/d;p?q', $uri->resolve($base, ''), '[empty]');
?>
===DONE===
--EXPECT--
===DONE===
20 changes: 20 additions & 0 deletions tests/Pyrus/JsonSchema/URI/uri_resolve3_dot.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
JsonSchema: URI - resolve (3) with dot segments
--FILE--
<?php
require dirname(__FILE__) . '/setup.php.inc';
$base = "http://a/b/c/d;p?q";

$test->assertEquals('http://a/b/c/', $uri->resolve($base, '.'), '.');
$test->assertEquals('http://a/b/c/', $uri->resolve($base, './'), './');
$test->assertEquals('http://a/b/', $uri->resolve($base, '..'), '..');
$test->assertEquals('http://a/b/', $uri->resolve($base, '../'), '../');
$test->assertEquals('http://a/b/g', $uri->resolve($base, '../g'), '../g');
$test->assertEquals('http://a/', $uri->resolve($base, '../..'), '../..');
$test->assertEquals('http://a/', $uri->resolve($base, '../../'), '../../');
$test->assertEquals('http://a/g', $uri->resolve($base, '../../g'), '../../g');

?>
===DONE===
--EXPECT--
===DONE===
27 changes: 27 additions & 0 deletions tests/Pyrus/JsonSchema/URI/uri_resolve4_dot_abnormal.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
JsonSchema: URI - resolve (4) with dot segments, abnormal examples
--FILE--
<?php
require dirname(__FILE__) . '/setup.php.inc';
$base = "http://a/b/c/d;p?q";

//abnormal examples from RFC 3986
$test->assertEquals('http://a/g', $uri->resolve($base, '../../../g'), '../../../g');
$test->assertEquals('http://a/g', $uri->resolve($base, '../../../../g'), '../../../../g');

$test->assertEquals('http://a/g', $uri->resolve($base, '/./g'), '/./g');
$test->assertEquals('http://a/g', $uri->resolve($base, '/../g'), '/../g');
$test->assertEquals('http://a/b/c/g.', $uri->resolve($base, 'g.'), 'g.');
$test->assertEquals('http://a/b/c/.g', $uri->resolve($base, '.g'), '.g');
$test->assertEquals('http://a/b/c/g..', $uri->resolve($base, 'g..'), 'g..');
$test->assertEquals('http://a/b/c/..g', $uri->resolve($base, '..g'), '..g');

$test->assertEquals('http://a/b/g', $uri->resolve($base, './../g'), './../g');
$test->assertEquals('http://a/b/c/g/', $uri->resolve($base, './g/.'), './g/.');
$test->assertEquals('http://a/b/c/h', $uri->resolve($base, 'g/../h'), 'g/../h');
$test->assertEquals('http://a/b/c/g;x=1/y', $uri->resolve($base, 'g;x=1/./y'), 'g;x=1/./y');
$test->assertEquals('http://a/b/c/y', $uri->resolve($base, 'g;x=1/../y'), 'g;x=1/../y');
?>
===DONE===
--EXPECT--
===DONE===
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
JsonSchema: URI - resolve (5) with abnormal query/fragment examples
--FILE--
<?php
require dirname(__FILE__) . '/setup.php.inc';
$base = "http://a/b/c/d;p?q";

//abnormal examples from RFC 3986
$test->assertEquals('http://a/b/c/g?y/./x', $uri->resolve($base, 'g?y/./x'), 'g?y/./x');
$test->assertEquals('http://a/b/c/g?y/../x', $uri->resolve($base, 'g?y/../x'), 'g?y/../x');
$test->assertEquals('http://a/b/c/g#y/./x', $uri->resolve($base, 'g#y/./x'), 'g#y/./x');
$test->assertEquals('http://a/b/c/g#y/../x', $uri->resolve($base, 'g#y/../x'), 'g#y/../x');
?>
===DONE===
--EXPECT--
===DONE===
16 changes: 16 additions & 0 deletions tests/Pyrus/JsonSchema/URI/uri_resolve6_tolerant.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
JsonSchema: URI - resolve (6) tolerant
--FILE--
<?php
require dirname(__FILE__) . '/setup.php.inc';
$base = "http://a/b/c/d;p?q";
$options = new Pyrus\JsonSchema\URI\Options;
$options->tolerant = true;

//abnormal examples from RFC 3986
$test->assertEquals('http:g', $uri->resolve($base, 'http:g'), 'http:g, non-tolerant');
$test->assertEquals('http://a/b/c/g', $uri->resolve($base, 'http:g', $options), 'http:g, tolerant');
?>
===DONE===
--EXPECT--
===DONE===

0 comments on commit f0dd303

Please sign in to comment.