UriResoverTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace GuzzleHttp\Tests\Psr7;
  3. use GuzzleHttp\Psr7\Uri;
  4. use GuzzleHttp\Psr7\UriResolver;
  5. /**
  6. * @covers GuzzleHttp\Psr7\UriResolver
  7. */
  8. class UriResolverTest extends BaseTest
  9. {
  10. const RFC3986_BASE = 'http://a/b/c/d;p?q';
  11. /**
  12. * @dataProvider getResolveTestCases
  13. */
  14. public function testResolveUri($base, $rel, $expectedTarget)
  15. {
  16. $baseUri = new Uri($base);
  17. $targetUri = UriResolver::resolve($baseUri, new Uri($rel));
  18. $this->assertInstanceOf('Psr\Http\Message\UriInterface', $targetUri);
  19. $this->assertSame($expectedTarget, (string) $targetUri);
  20. // This ensures there are no test cases that only work in the resolve() direction but not the
  21. // opposite via relativize(). This can happen when both base and rel URI are relative-path
  22. // references resulting in another relative-path URI.
  23. $this->assertSame($expectedTarget, (string) UriResolver::resolve($baseUri, $targetUri));
  24. }
  25. /**
  26. * @dataProvider getResolveTestCases
  27. */
  28. public function testRelativizeUri($base, $expectedRelativeReference, $target)
  29. {
  30. $baseUri = new Uri($base);
  31. $relativeUri = UriResolver::relativize($baseUri, new Uri($target));
  32. $this->assertInstanceOf('Psr\Http\Message\UriInterface', $relativeUri);
  33. // There are test-cases with too many dot-segments and relative references that are equal like "." == "./".
  34. // So apart from the same-as condition, this alternative success condition is necessary.
  35. $this->assertTrue(
  36. $expectedRelativeReference === (string) $relativeUri
  37. || $target === (string) UriResolver::resolve($baseUri, $relativeUri),
  38. sprintf(
  39. '"%s" is not the correct relative reference as it does not resolve to the target URI from the base URI',
  40. (string) $relativeUri
  41. )
  42. );
  43. }
  44. /**
  45. * @dataProvider getRelativizeTestCases
  46. */
  47. public function testRelativizeUriWithUniqueTests($base, $target, $expectedRelativeReference)
  48. {
  49. $baseUri = new Uri($base);
  50. $targetUri = new Uri($target);
  51. $relativeUri = UriResolver::relativize($baseUri, $targetUri);
  52. $this->assertInstanceOf('Psr\Http\Message\UriInterface', $relativeUri);
  53. $this->assertSame($expectedRelativeReference, (string) $relativeUri);
  54. $this->assertSame((string) UriResolver::resolve($baseUri, $targetUri), (string) UriResolver::resolve($baseUri, $relativeUri));
  55. }
  56. public function getResolveTestCases()
  57. {
  58. return [
  59. [self::RFC3986_BASE, 'g:h', 'g:h'],
  60. [self::RFC3986_BASE, 'g', 'http://a/b/c/g'],
  61. [self::RFC3986_BASE, './g', 'http://a/b/c/g'],
  62. [self::RFC3986_BASE, 'g/', 'http://a/b/c/g/'],
  63. [self::RFC3986_BASE, '/g', 'http://a/g'],
  64. [self::RFC3986_BASE, '//g', 'http://g'],
  65. [self::RFC3986_BASE, '?y', 'http://a/b/c/d;p?y'],
  66. [self::RFC3986_BASE, 'g?y', 'http://a/b/c/g?y'],
  67. [self::RFC3986_BASE, '#s', 'http://a/b/c/d;p?q#s'],
  68. [self::RFC3986_BASE, 'g#s', 'http://a/b/c/g#s'],
  69. [self::RFC3986_BASE, 'g?y#s', 'http://a/b/c/g?y#s'],
  70. [self::RFC3986_BASE, ';x', 'http://a/b/c/;x'],
  71. [self::RFC3986_BASE, 'g;x', 'http://a/b/c/g;x'],
  72. [self::RFC3986_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s'],
  73. [self::RFC3986_BASE, '', self::RFC3986_BASE],
  74. [self::RFC3986_BASE, '.', 'http://a/b/c/'],
  75. [self::RFC3986_BASE, './', 'http://a/b/c/'],
  76. [self::RFC3986_BASE, '..', 'http://a/b/'],
  77. [self::RFC3986_BASE, '../', 'http://a/b/'],
  78. [self::RFC3986_BASE, '../g', 'http://a/b/g'],
  79. [self::RFC3986_BASE, '../..', 'http://a/'],
  80. [self::RFC3986_BASE, '../../', 'http://a/'],
  81. [self::RFC3986_BASE, '../../g', 'http://a/g'],
  82. [self::RFC3986_BASE, '../../../g', 'http://a/g'],
  83. [self::RFC3986_BASE, '../../../../g', 'http://a/g'],
  84. [self::RFC3986_BASE, '/./g', 'http://a/g'],
  85. [self::RFC3986_BASE, '/../g', 'http://a/g'],
  86. [self::RFC3986_BASE, 'g.', 'http://a/b/c/g.'],
  87. [self::RFC3986_BASE, '.g', 'http://a/b/c/.g'],
  88. [self::RFC3986_BASE, 'g..', 'http://a/b/c/g..'],
  89. [self::RFC3986_BASE, '..g', 'http://a/b/c/..g'],
  90. [self::RFC3986_BASE, './../g', 'http://a/b/g'],
  91. [self::RFC3986_BASE, 'foo////g', 'http://a/b/c/foo////g'],
  92. [self::RFC3986_BASE, './g/.', 'http://a/b/c/g/'],
  93. [self::RFC3986_BASE, 'g/./h', 'http://a/b/c/g/h'],
  94. [self::RFC3986_BASE, 'g/../h', 'http://a/b/c/h'],
  95. [self::RFC3986_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y'],
  96. [self::RFC3986_BASE, 'g;x=1/../y', 'http://a/b/c/y'],
  97. // dot-segments in the query or fragment
  98. [self::RFC3986_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x'],
  99. [self::RFC3986_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x'],
  100. [self::RFC3986_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x'],
  101. [self::RFC3986_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x'],
  102. [self::RFC3986_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x'],
  103. [self::RFC3986_BASE, '?y#s', 'http://a/b/c/d;p?y#s'],
  104. // base with fragment
  105. ['http://a/b/c?q#s', '?y', 'http://a/b/c?y'],
  106. // base with user info
  107. ['http://u@a/b/c/d;p?q', '.', 'http://u@a/b/c/'],
  108. ['http://u:p@a/b/c/d;p?q', '.', 'http://u:p@a/b/c/'],
  109. // path ending with slash or no slash at all
  110. ['http://a/b/c/d/', 'e', 'http://a/b/c/d/e'],
  111. ['urn:no-slash', 'e', 'urn:e'],
  112. // falsey relative parts
  113. [self::RFC3986_BASE, '//0', 'http://0'],
  114. [self::RFC3986_BASE, '0', 'http://a/b/c/0'],
  115. [self::RFC3986_BASE, '?0', 'http://a/b/c/d;p?0'],
  116. [self::RFC3986_BASE, '#0', 'http://a/b/c/d;p?q#0'],
  117. // absolute path base URI
  118. ['/a/b/', '', '/a/b/'],
  119. ['/a/b', '', '/a/b'],
  120. ['/', 'a', '/a'],
  121. ['/', 'a/b', '/a/b'],
  122. ['/a/b', 'g', '/a/g'],
  123. ['/a/b/c', './', '/a/b/'],
  124. ['/a/b/', '../', '/a/'],
  125. ['/a/b/c', '../', '/a/'],
  126. ['/a/b/', '../../x/y/z/', '/x/y/z/'],
  127. ['/a/b/c/d/e', '../../../c/d', '/a/c/d'],
  128. ['/a/b/c//', '../', '/a/b/c/'],
  129. ['/a/b/c/', './/', '/a/b/c//'],
  130. ['/a/b/c', '../../../../a', '/a'],
  131. ['/a/b/c', '../../../..', '/'],
  132. // not actually a dot-segment
  133. ['/a/b/c', '..a/b..', '/a/b/..a/b..'],
  134. // '' cannot be used as relative reference as it would inherit the base query component
  135. ['/a/b?q', 'b', '/a/b'],
  136. ['/a/b/?q', './', '/a/b/'],
  137. // path with colon: "with:colon" would be the wrong relative reference
  138. ['/a/', './with:colon', '/a/with:colon'],
  139. ['/a/', 'b/with:colon', '/a/b/with:colon'],
  140. ['/a/', './:b/', '/a/:b/'],
  141. // relative path references
  142. ['a', 'a/b', 'a/b'],
  143. ['', '', ''],
  144. ['', '..', ''],
  145. ['/', '..', '/'],
  146. ['urn:a/b', '..//a/b', 'urn:/a/b'],
  147. // network path references
  148. // empty base path and relative-path reference
  149. ['//example.com', 'a', '//example.com/a'],
  150. // path starting with two slashes
  151. ['//example.com//two-slashes', './', '//example.com//'],
  152. ['//example.com', './/', '//example.com//'],
  153. ['//example.com/', './/', '//example.com//'],
  154. // base URI has less components than relative URI
  155. ['/', '//a/b?q#h', '//a/b?q#h'],
  156. ['/', 'urn:/', 'urn:/'],
  157. ];
  158. }
  159. /**
  160. * Some additional tests to getResolveTestCases() that only make sense for relativize.
  161. */
  162. public function getRelativizeTestCases()
  163. {
  164. return [
  165. // targets that are relative-path references are returned as-is
  166. ['a/b', 'b/c', 'b/c'],
  167. ['a/b/c', '../b/c', '../b/c'],
  168. ['a', '', ''],
  169. ['a', './', './'],
  170. ['a', 'a/..', 'a/..'],
  171. ['/a/b/?q', '?q#h', '?q#h'],
  172. ['/a/b/?q', '#h', '#h'],
  173. ['/a/b/?q', 'c#h', 'c#h'],
  174. // If the base URI has a query but the target has none, we cannot return an empty path reference as it would
  175. // inherit the base query component when resolving.
  176. ['/a/b/?q', '/a/b/#h', './#h'],
  177. ['/', '/#h', '#h'],
  178. ['/', '/', ''],
  179. ['http://a', 'http://a/', './'],
  180. ['urn:a/b?q', 'urn:x/y?q', '../x/y?q'],
  181. ['urn:', 'urn:/', './/'],
  182. ['urn:a/b?q', 'urn:', '../'],
  183. // target URI has less components than base URI
  184. ['http://a/b/', '//a/b/c', 'c'],
  185. ['http://a/b/', '/b/c', 'c'],
  186. ['http://a/b/', '/x/y', '../x/y'],
  187. ['http://a/b/', '/', '../'],
  188. // absolute target URI without authority but base URI has one
  189. ['urn://a/b/', 'urn:/b/', 'urn:/b/'],
  190. ];
  191. }
  192. }