http.php 13 KB

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