From f0dd303d7b6d1f122591d4a5ff0ea12825716e78 Mon Sep 17 00:00:00 2001 From: Greg Beaver Date: Wed, 27 Jul 2011 21:19:10 -0500 Subject: [PATCH] new URI tests, fix resolve --- src/Pyrus/JsonSchema/URI.php | 3 ++- tests/Pyrus/JsonSchema/URI/uri_resolve1.phpt | 17 ++++++++++++ .../URI/uri_resolve2_queryfragment.phpt | 20 ++++++++++++++ .../JsonSchema/URI/uri_resolve3_dot.phpt | 20 ++++++++++++++ .../URI/uri_resolve4_dot_abnormal.phpt | 27 +++++++++++++++++++ .../uri_resolve5_queryfragment_abnormal.phpt | 16 +++++++++++ .../JsonSchema/URI/uri_resolve6_tolerant.phpt | 16 +++++++++++ 7 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 tests/Pyrus/JsonSchema/URI/uri_resolve1.phpt create mode 100644 tests/Pyrus/JsonSchema/URI/uri_resolve2_queryfragment.phpt create mode 100644 tests/Pyrus/JsonSchema/URI/uri_resolve3_dot.phpt create mode 100644 tests/Pyrus/JsonSchema/URI/uri_resolve4_dot_abnormal.phpt create mode 100644 tests/Pyrus/JsonSchema/URI/uri_resolve5_queryfragment_abnormal.phpt create mode 100644 tests/Pyrus/JsonSchema/URI/uri_resolve6_tolerant.phpt diff --git a/src/Pyrus/JsonSchema/URI.php b/src/Pyrus/JsonSchema/URI.php index 0ff1a37..0e2af52 100644 --- a/src/Pyrus/JsonSchema/URI.php +++ b/src/Pyrus/JsonSchema/URI.php @@ -241,6 +241,7 @@ function pctDecUnreserved($str) { $newStr = ""; $i = 0; + $str = $str[0]; while ($i < strlen($str)) { $c = hexdec(substr($str, $i + 1, 2)); @@ -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; diff --git a/tests/Pyrus/JsonSchema/URI/uri_resolve1.phpt b/tests/Pyrus/JsonSchema/URI/uri_resolve1.phpt new file mode 100644 index 0000000..4d6a21e --- /dev/null +++ b/tests/Pyrus/JsonSchema/URI/uri_resolve1.phpt @@ -0,0 +1,17 @@ +--TEST-- +JsonSchema: URI - resolve (1) with scheme +--FILE-- +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=== \ No newline at end of file diff --git a/tests/Pyrus/JsonSchema/URI/uri_resolve2_queryfragment.phpt b/tests/Pyrus/JsonSchema/URI/uri_resolve2_queryfragment.phpt new file mode 100644 index 0000000..4529825 --- /dev/null +++ b/tests/Pyrus/JsonSchema/URI/uri_resolve2_queryfragment.phpt @@ -0,0 +1,20 @@ +--TEST-- +JsonSchema: URI - resolve (2) with query and fragment +--FILE-- +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=== \ No newline at end of file diff --git a/tests/Pyrus/JsonSchema/URI/uri_resolve3_dot.phpt b/tests/Pyrus/JsonSchema/URI/uri_resolve3_dot.phpt new file mode 100644 index 0000000..24ca048 --- /dev/null +++ b/tests/Pyrus/JsonSchema/URI/uri_resolve3_dot.phpt @@ -0,0 +1,20 @@ +--TEST-- +JsonSchema: URI - resolve (3) with dot segments +--FILE-- +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=== \ No newline at end of file diff --git a/tests/Pyrus/JsonSchema/URI/uri_resolve4_dot_abnormal.phpt b/tests/Pyrus/JsonSchema/URI/uri_resolve4_dot_abnormal.phpt new file mode 100644 index 0000000..1c9f9f2 --- /dev/null +++ b/tests/Pyrus/JsonSchema/URI/uri_resolve4_dot_abnormal.phpt @@ -0,0 +1,27 @@ +--TEST-- +JsonSchema: URI - resolve (4) with dot segments, abnormal examples +--FILE-- +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=== \ No newline at end of file diff --git a/tests/Pyrus/JsonSchema/URI/uri_resolve5_queryfragment_abnormal.phpt b/tests/Pyrus/JsonSchema/URI/uri_resolve5_queryfragment_abnormal.phpt new file mode 100644 index 0000000..daa7eb2 --- /dev/null +++ b/tests/Pyrus/JsonSchema/URI/uri_resolve5_queryfragment_abnormal.phpt @@ -0,0 +1,16 @@ +--TEST-- +JsonSchema: URI - resolve (5) with abnormal query/fragment examples +--FILE-- +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=== \ No newline at end of file diff --git a/tests/Pyrus/JsonSchema/URI/uri_resolve6_tolerant.phpt b/tests/Pyrus/JsonSchema/URI/uri_resolve6_tolerant.phpt new file mode 100644 index 0000000..e720668 --- /dev/null +++ b/tests/Pyrus/JsonSchema/URI/uri_resolve6_tolerant.phpt @@ -0,0 +1,16 @@ +--TEST-- +JsonSchema: URI - resolve (6) tolerant +--FILE-- +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=== \ No newline at end of file