PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/phpunit/tests/http/http.php

https://gitlab.com/morganestes/wordpress-develop
PHP | 374 lines | 261 code | 46 blank | 67 comment | 2 complexity | 4a6f9ed865cb9c5364afd08cf3a758ed MD5 | raw file
  1. <?php
  2. /**
  3. * Non-transport-specific WP_HTTP Tests
  4. *
  5. * @group http
  6. */
  7. class Tests_HTTP_HTTP extends WP_UnitTestCase {
  8. const FULL_TEST_URL = 'http://username:password@host.name:9090/path?arg1=value1&arg2=value2#anchor';
  9. /**
  10. * @dataProvider make_absolute_url_testcases
  11. */
  12. function test_make_absolute_url( $relative_url, $absolute_url, $expected ) {
  13. $actual = WP_Http::make_absolute_url( $relative_url, $absolute_url );
  14. $this->assertEquals( $expected, $actual );
  15. }
  16. function make_absolute_url_testcases() {
  17. // 0: The Location header, 1: The current url, 3: The expected url
  18. return array(
  19. array( 'http://site.com/', 'http://example.com/', 'http://site.com/' ), // Absolute URL provided
  20. array( '/location', '', '/location' ), // No current url provided
  21. array( '', 'http://example.com', 'http://example.com/' ), // No location provided
  22. // Location provided relative to site root
  23. array( '/root-relative-link.ext', 'http://example.com/', 'http://example.com/root-relative-link.ext' ),
  24. array( '/root-relative-link.ext?with=query', 'http://example.com/index.ext?query', 'http://example.com/root-relative-link.ext?with=query' ),
  25. // Location provided relative to current file/directory
  26. array( 'relative-file.ext', 'http://example.com/', 'http://example.com/relative-file.ext' ),
  27. array( 'relative-file.ext', 'http://example.com/filename', 'http://example.com/relative-file.ext' ),
  28. array( 'relative-file.ext', 'http://example.com/directory/', 'http://example.com/directory/relative-file.ext' ),
  29. // Location provided relative to current file/directory but in a parent directory
  30. array( '../file-in-parent.ext', 'http://example.com', 'http://example.com/file-in-parent.ext' ),
  31. array( '../file-in-parent.ext', 'http://example.com/filename', 'http://example.com/file-in-parent.ext' ),
  32. array( '../file-in-parent.ext', 'http://example.com/directory/', 'http://example.com/file-in-parent.ext' ),
  33. array( '../file-in-parent.ext', 'http://example.com/directory/filename', 'http://example.com/file-in-parent.ext' ),
  34. // Location provided in muliple levels higher, including impossible to reach (../ below DOCROOT)
  35. array( '../../file-in-grand-parent.ext', 'http://example.com', 'http://example.com/file-in-grand-parent.ext' ),
  36. array( '../../file-in-grand-parent.ext', 'http://example.com/filename', 'http://example.com/file-in-grand-parent.ext' ),
  37. array( '../../file-in-grand-parent.ext', 'http://example.com/directory/', 'http://example.com/file-in-grand-parent.ext' ),
  38. array( '../../file-in-grand-parent.ext', 'http://example.com/directory/filename/', 'http://example.com/file-in-grand-parent.ext' ),
  39. array( '../../file-in-grand-parent.ext', 'http://example.com/directory1/directory2/filename', 'http://example.com/file-in-grand-parent.ext' ),
  40. // Query strings should attach, or replace existing query string.
  41. array( '?query=string', 'http://example.com', 'http://example.com/?query=string' ),
  42. array( '?query=string', 'http://example.com/file.ext', 'http://example.com/file.ext?query=string' ),
  43. array( '?query=string', 'http://example.com/file.ext?existing=query-string', 'http://example.com/file.ext?query=string' ),
  44. array( 'otherfile.ext?query=string', 'http://example.com/file.ext?existing=query-string', 'http://example.com/otherfile.ext?query=string' ),
  45. // A file with a leading dot
  46. array( '.ext', 'http://example.com/', 'http://example.com/.ext' ),
  47. // URls within URLs
  48. array( '/expected', 'http://example.com/sub/http://site.com/sub/', 'http://example.com/expected' ),
  49. array( '/expected/http://site.com/sub/', 'http://example.com/', 'http://example.com/expected/http://site.com/sub/' ),
  50. // Schemeless URL's (Not valid in HTTP Headers, but may be used elsewhere)
  51. array( '//example.com/sub/', 'https://example.net', 'https://example.com/sub/' ),
  52. );
  53. }
  54. /**
  55. * @dataProvider parse_url_testcases
  56. */
  57. function test_wp_parse_url( $url, $expected ) {
  58. $actual = wp_parse_url( $url );
  59. $this->assertEquals( $expected, $actual );
  60. }
  61. function parse_url_testcases() {
  62. // 0: The URL, 1: The expected resulting structure
  63. return array(
  64. array(
  65. self::FULL_TEST_URL,
  66. array(
  67. 'scheme' => 'http',
  68. 'host' => 'host.name',
  69. 'port' => 9090,
  70. 'user' => 'username',
  71. 'pass' => 'password',
  72. 'path' => '/path',
  73. 'query' => 'arg1=value1&arg2=value2',
  74. 'fragment' => 'anchor',
  75. ),
  76. ),
  77. array(
  78. 'http://example.com/',
  79. array(
  80. 'scheme' => 'http',
  81. 'host' => 'example.com',
  82. 'path' => '/',
  83. ),
  84. ),
  85. // < PHP 5.4.7: Schemeless URL
  86. array(
  87. '//example.com/path/',
  88. array(
  89. 'host' => 'example.com',
  90. 'path' => '/path/',
  91. ),
  92. ),
  93. array(
  94. '//example.com/',
  95. array(
  96. 'host' => 'example.com',
  97. 'path' => '/',
  98. ),
  99. ),
  100. array(
  101. 'http://example.com//path/',
  102. array(
  103. 'scheme' => 'http',
  104. 'host' => 'example.com',
  105. 'path' => '//path/',
  106. ),
  107. ),
  108. // < PHP 5.4.7: Scheme separator in the URL.
  109. array(
  110. 'http://example.com/http://example.net/',
  111. array(
  112. 'scheme' => 'http',
  113. 'host' => 'example.com',
  114. 'path' => '/http://example.net/',
  115. ),
  116. ),
  117. array( '/path/http://example.net/', array( 'path' => '/path/http://example.net/' ) ),
  118. // < PHP 5.4.7: IPv6 literals in schemeless URLs are handled incorrectly.
  119. array(
  120. '//[::FFFF::127.0.0.1]/',
  121. array(
  122. 'host' => '[::FFFF::127.0.0.1]',
  123. 'path' => '/',
  124. ),
  125. ),
  126. // PHP's parse_url() calls this an invalid url, we handle it as a path
  127. array( '/://example.com/', array( 'path' => '/://example.com/' ) ),
  128. // Schemeless URL containing colons cause parse errors in PHP 7+.
  129. array(
  130. '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin',
  131. array(
  132. 'host' => 'fonts.googleapis.com',
  133. 'path' => '/css',
  134. 'query' => 'family=Open+Sans:400&subset=latin',
  135. ),
  136. ),
  137. array(
  138. '//fonts.googleapis.com/css?family=Open+Sans:400',
  139. array(
  140. 'host' => 'fonts.googleapis.com',
  141. 'path' => '/css',
  142. 'query' => 'family=Open+Sans:400',
  143. ),
  144. ),
  145. array( 'filenamefound', array( 'path' => 'filenamefound' ) ),
  146. // Empty string or non-string passed in.
  147. array( '', array( 'path' => '' ) ),
  148. array( 123, array( 'path' => '123' ) ),
  149. );
  150. /*
  151. * Untestable edge cases in various PHP:
  152. * - ///example.com - Fails in PHP >= 5.4.7, assumed path in <5.4.7
  153. * - ://example.com - assumed path in PHP >= 5.4.7, fails in <5.4.7
  154. */
  155. }
  156. /**
  157. * @ticket 36356
  158. */
  159. function test_wp_parse_url_with_default_component() {
  160. $actual = wp_parse_url( self::FULL_TEST_URL, -1 );
  161. $this->assertEquals(
  162. array(
  163. 'scheme' => 'http',
  164. 'host' => 'host.name',
  165. 'port' => 9090,
  166. 'user' => 'username',
  167. 'pass' => 'password',
  168. 'path' => '/path',
  169. 'query' => 'arg1=value1&arg2=value2',
  170. 'fragment' => 'anchor',
  171. ), $actual
  172. );
  173. }
  174. /**
  175. * @ticket 36356
  176. *
  177. * @dataProvider parse_url_component_testcases
  178. */
  179. function test_wp_parse_url_with_component( $url, $component, $expected ) {
  180. $actual = wp_parse_url( $url, $component );
  181. $this->assertSame( $expected, $actual );
  182. }
  183. function parse_url_component_testcases() {
  184. // 0: The URL, 1: The requested component, 2: The expected resulting structure.
  185. return array(
  186. array( self::FULL_TEST_URL, PHP_URL_SCHEME, 'http' ),
  187. array( self::FULL_TEST_URL, PHP_URL_USER, 'username' ),
  188. array( self::FULL_TEST_URL, PHP_URL_PASS, 'password' ),
  189. array( self::FULL_TEST_URL, PHP_URL_HOST, 'host.name' ),
  190. array( self::FULL_TEST_URL, PHP_URL_PORT, 9090 ),
  191. array( self::FULL_TEST_URL, PHP_URL_PATH, '/path' ),
  192. array( self::FULL_TEST_URL, PHP_URL_QUERY, 'arg1=value1&arg2=value2' ),
  193. array( self::FULL_TEST_URL, PHP_URL_FRAGMENT, 'anchor' ),
  194. // < PHP 5.4.7: Schemeless URL.
  195. array( '//example.com/path/', PHP_URL_HOST, 'example.com' ),
  196. array( '//example.com/path/', PHP_URL_PATH, '/path/' ),
  197. array( '//example.com/', PHP_URL_HOST, 'example.com' ),
  198. array( '//example.com/', PHP_URL_PATH, '/' ),
  199. array( 'http://example.com//path/', PHP_URL_HOST, 'example.com' ),
  200. array( 'http://example.com//path/', PHP_URL_PATH, '//path/' ),
  201. // < PHP 5.4.7: Scheme separator in the URL.
  202. array( 'http://example.com/http://example.net/', PHP_URL_HOST, 'example.com' ),
  203. array( 'http://example.com/http://example.net/', PHP_URL_PATH, '/http://example.net/' ),
  204. array( '/path/http://example.net/', PHP_URL_HOST, null ),
  205. array( '/path/http://example.net/', PHP_URL_PATH, '/path/http://example.net/' ),
  206. // < PHP 5.4.7: IPv6 literals in schemeless URLs are handled incorrectly.
  207. array( '//[::FFFF::127.0.0.1]/', PHP_URL_HOST, '[::FFFF::127.0.0.1]' ),
  208. array( '//[::FFFF::127.0.0.1]/', PHP_URL_PATH, '/' ),
  209. // PHP's parse_url() calls this an invalid URL, we handle it as a path.
  210. array( '/://example.com/', PHP_URL_PATH, '/://example.com/' ),
  211. // Schemeless URL containing colons cause parse errors in PHP 7+.
  212. array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_HOST, 'fonts.googleapis.com' ),
  213. array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_PORT, null ),
  214. array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_PATH, '/css' ),
  215. array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_QUERY, 'family=Open+Sans:400&subset=latin' ),
  216. array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_HOST, 'fonts.googleapis.com' ), // 25
  217. array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_PORT, null ),
  218. array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_PATH, '/css' ), //27
  219. array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_QUERY, 'family=Open+Sans:400' ), //28
  220. // Empty string or non-string passed in.
  221. array( '', PHP_URL_PATH, '' ),
  222. array( '', PHP_URL_QUERY, null ),
  223. array( 123, PHP_URL_PORT, null ),
  224. array( 123, PHP_URL_PATH, '123' ),
  225. );
  226. }
  227. /**
  228. * @ticket 35426
  229. */
  230. public function test_http_response_code_constants() {
  231. global $wp_header_to_desc;
  232. $ref = new ReflectionClass( 'WP_Http' );
  233. $constants = $ref->getConstants();
  234. // This primes the `$wp_header_to_desc` global:
  235. get_status_header_desc( 200 );
  236. $this->assertEquals( array_keys( $wp_header_to_desc ), array_values( $constants ) );
  237. }
  238. /**
  239. * @ticket 37768
  240. */
  241. public function test_normalize_cookies_scalar_values() {
  242. $http = _wp_http_get_object();
  243. $cookies = array(
  244. 'x' => 'foo',
  245. 'y' => 2,
  246. 'z' => 0.45,
  247. 'foo' => array( 'bar' ),
  248. );
  249. $cookie_jar = $http->normalize_cookies(
  250. array(
  251. 'x' => 'foo',
  252. 'y' => 2,
  253. 'z' => 0.45,
  254. 'foo' => array( 'bar' ),
  255. )
  256. );
  257. $this->assertInstanceOf( 'Requests_Cookie_Jar', $cookie_jar );
  258. foreach ( array_keys( $cookies ) as $cookie ) {
  259. if ( 'foo' === $cookie ) {
  260. $this->assertFalse( isset( $cookie_jar[ $cookie ] ) );
  261. } else {
  262. $this->assertInstanceOf( 'Requests_Cookie', $cookie_jar[ $cookie ] );
  263. }
  264. }
  265. }
  266. /**
  267. * @ticket 36356
  268. *
  269. * @dataProvider get_component_from_parsed_url_array_testcases
  270. */
  271. function test_get_component_from_parsed_url_array( $url, $component, $expected ) {
  272. $parts = wp_parse_url( $url );
  273. $actual = _get_component_from_parsed_url_array( $parts, $component );
  274. $this->assertSame( $expected, $actual );
  275. }
  276. function get_component_from_parsed_url_array_testcases() {
  277. // 0: A URL, 1: PHP URL constant, 2: The expected result.
  278. return array(
  279. array(
  280. 'http://example.com/',
  281. -1,
  282. array(
  283. 'scheme' => 'http',
  284. 'host' => 'example.com',
  285. 'path' => '/',
  286. ),
  287. ),
  288. array(
  289. 'http://example.com/',
  290. -1,
  291. array(
  292. 'scheme' => 'http',
  293. 'host' => 'example.com',
  294. 'path' => '/',
  295. ),
  296. ),
  297. array( 'http://example.com/', PHP_URL_HOST, 'example.com' ),
  298. array( 'http://example.com/', PHP_URL_USER, null ),
  299. array( 'http:///example.com', -1, false ), // Malformed.
  300. array( 'http:///example.com', PHP_URL_HOST, null ), // Malformed.
  301. );
  302. }
  303. /**
  304. * @ticket 36356
  305. *
  306. * @dataProvider wp_translate_php_url_constant_to_key_testcases
  307. */
  308. function test_wp_translate_php_url_constant_to_key( $input, $expected ) {
  309. $actual = _wp_translate_php_url_constant_to_key( $input );
  310. $this->assertSame( $expected, $actual );
  311. }
  312. function wp_translate_php_url_constant_to_key_testcases() {
  313. // 0: PHP URL constant, 1: The expected result.
  314. return array(
  315. array( PHP_URL_SCHEME, 'scheme' ),
  316. array( PHP_URL_HOST, 'host' ),
  317. array( PHP_URL_PORT, 'port' ),
  318. array( PHP_URL_USER, 'user' ),
  319. array( PHP_URL_PASS, 'pass' ),
  320. array( PHP_URL_PATH, 'path' ),
  321. array( PHP_URL_QUERY, 'query' ),
  322. array( PHP_URL_FRAGMENT, 'fragment' ),
  323. // Test with non-PHP_URL_CONSTANT parameter.
  324. array( 'something', false ),
  325. array( ABSPATH, false ),
  326. );
  327. }
  328. }