http_request.test 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * @file
  4. * Tests for http_request.inc.
  5. */
  6. /**
  7. * Tests for the http library.
  8. */
  9. class FeedsHTTPRequestTestCase extends FeedsUnitTestHelper {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'HTTP library',
  13. 'description' => 'Tests for Feeds HTTP library.',
  14. 'group' => 'Feeds',
  15. );
  16. }
  17. public function setUp() {
  18. parent::setUp();
  19. feeds_include_library('http_request.inc', 'http_request');
  20. }
  21. /**
  22. * Tests http_request_find_feeds().
  23. */
  24. public function testHTTPRequestFindFeeds() {
  25. $html = <<<EOF
  26. <html>
  27. <head>
  28. <title>Welcome to Example.com</title>
  29. <link rel="stylesheet" type="text/css" media="screen, projection" href="/stuff.css" >
  30. <link rel="search" title="Something" href="//example.com/search">
  31. <link rel="alternate" title="Something RSS" href="http://example.com/rss.xml" type="application/rss+xml">
  32. <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
  33. </head>
  34. <body>
  35. This is a body.
  36. </body>
  37. </html
  38. EOF;
  39. $links = http_request_find_feeds($html);
  40. $this->assertEqual(count($links), 1);
  41. $this->assertEqual($links[0], 'http://example.com/rss.xml');
  42. // Test single quoted HTML.
  43. $links = http_request_find_feeds(str_replace('"', "'", $html));
  44. $this->assertEqual(count($links), 1);
  45. $this->assertEqual($links[0], 'http://example.com/rss.xml');
  46. }
  47. /**
  48. * Tests http_request_create_absolute_url().
  49. */
  50. public function testHTTPRequestCreateAbsoluteUrl() {
  51. $test_urls = array(
  52. // Rels that do not start with "/".
  53. array(
  54. 'rel' => 'h',
  55. 'base' => 'http://www',
  56. 'expected' => 'http://www/h',
  57. ),
  58. array(
  59. 'rel' => 'h',
  60. 'base' => 'http://www/',
  61. 'expected' => 'http://www/h',
  62. ),
  63. array(
  64. 'rel' => 'h',
  65. 'base' => 'http://www/?c;d=e#f',
  66. 'expected' => 'http://www/h',
  67. ),
  68. array(
  69. 'rel' => 'h',
  70. 'base' => 'http://www/a/b',
  71. 'expected' => 'http://www/a/h',
  72. ),
  73. array(
  74. 'rel' => 'h/j',
  75. 'base' => 'http://www',
  76. 'expected' => 'http://www/h/j',
  77. ),
  78. array(
  79. 'rel' => 'h/j',
  80. 'base' => 'http://www/',
  81. 'expected' => 'http://www/h/j',
  82. ),
  83. array(
  84. 'rel' => 'h/j',
  85. 'base' => 'http://www/?c;d=e#f',
  86. 'expected' => 'http://www/h/j',
  87. ),
  88. array(
  89. 'rel' => 'h/j',
  90. 'base' => 'http://www/a/b',
  91. 'expected' => 'http://www/a/h/j',
  92. ),
  93. array(
  94. 'rel' => 'h/j',
  95. 'base' => 'http://www/a/b/',
  96. 'expected' => 'http://www/a/b/h/j',
  97. ),
  98. // Rels that start with "/".
  99. array(
  100. 'rel' => '/h',
  101. 'base' => 'http://www',
  102. 'expected' => 'http://www/h',
  103. ),
  104. array(
  105. 'rel' => '/h',
  106. 'base' => 'http://www/',
  107. 'expected' => 'http://www/h',
  108. ),
  109. array(
  110. 'rel' => '/h',
  111. 'base' => 'http://www/?c;d=e#f',
  112. 'expected' => 'http://www/h',
  113. ),
  114. array(
  115. 'rel' => '/h',
  116. 'base' => 'http://www/a/b',
  117. 'expected' => 'http://www/h',
  118. ),
  119. array(
  120. 'rel' => '/h/j',
  121. 'base' => 'http://www',
  122. 'expected' => 'http://www/h/j',
  123. ),
  124. array(
  125. 'rel' => '/h/j',
  126. 'base' => 'http://www/',
  127. 'expected' => 'http://www/h/j',
  128. ),
  129. array(
  130. 'rel' => '/h/j',
  131. 'base' => 'http://www/?c;d=e#f',
  132. 'expected' => 'http://www/h/j',
  133. ),
  134. array(
  135. 'rel' => '/h/j',
  136. 'base' => 'http://www/a/b',
  137. 'expected' => 'http://www/h/j',
  138. ),
  139. array(
  140. 'rel' => '/h/j',
  141. 'base' => 'http://www/a/b/',
  142. 'expected' => 'http://www/h/j',
  143. ),
  144. // Rels that contain ".".
  145. array(
  146. 'rel' => './h',
  147. 'base' => 'http://www',
  148. 'expected' => 'http://www/h',
  149. ),
  150. array(
  151. 'rel' => './h',
  152. 'base' => 'http://www/',
  153. 'expected' => 'http://www/h',
  154. ),
  155. array(
  156. 'rel' => './h',
  157. 'base' => 'http://www/?c;d=e#f',
  158. 'expected' => 'http://www/h',
  159. ),
  160. array(
  161. 'rel' => './h',
  162. 'base' => 'http://www/a/b',
  163. 'expected' => 'http://www/a/h',
  164. ),
  165. array(
  166. 'rel' => './h/j',
  167. 'base' => 'http://www',
  168. 'expected' => 'http://www/h/j',
  169. ),
  170. array(
  171. 'rel' => './h/j',
  172. 'base' => 'http://www/',
  173. 'expected' => 'http://www/h/j',
  174. ),
  175. array(
  176. 'rel' => './h/j',
  177. 'base' => 'http://www/?c;d=e#f',
  178. 'expected' => 'http://www/h/j',
  179. ),
  180. array(
  181. 'rel' => './h/j',
  182. 'base' => 'http://www/a/b',
  183. 'expected' => 'http://www/a/h/j',
  184. ),
  185. array(
  186. 'rel' => './h/j',
  187. 'base' => 'http://www/a/b/',
  188. 'expected' => 'http://www/a/b/h/j',
  189. ),
  190. array(
  191. 'rel' => 'h/./j',
  192. 'base' => 'http://www',
  193. 'expected' => 'http://www/h/j',
  194. ),
  195. array(
  196. 'rel' => 'h/./j',
  197. 'base' => 'http://www/',
  198. 'expected' => 'http://www/h/j',
  199. ),
  200. array(
  201. 'rel' => 'h/./j',
  202. 'base' => 'http://www/?c;d=e#f',
  203. 'expected' => 'http://www/h/j',
  204. ),
  205. array(
  206. 'rel' => 'h/./j',
  207. 'base' => 'http://www/a/b',
  208. 'expected' => 'http://www/a/h/j',
  209. ),
  210. array(
  211. 'rel' => 'h/./j',
  212. 'base' => 'http://www/a/b/',
  213. 'expected' => 'http://www/a/b/h/j',
  214. ),
  215. array(
  216. 'rel' => '/h/./j',
  217. 'base' => 'http://www',
  218. 'expected' => 'http://www/h/j',
  219. ),
  220. array(
  221. 'rel' => '/h/./j',
  222. 'base' => 'http://www/',
  223. 'expected' => 'http://www/h/j',
  224. ),
  225. array(
  226. 'rel' => '/h/./j',
  227. 'base' => 'http://www/?c;d=e#f',
  228. 'expected' => 'http://www/h/j',
  229. ),
  230. array(
  231. 'rel' => '/h/./j',
  232. 'base' => 'http://www/a/b',
  233. 'expected' => 'http://www/h/j',
  234. ),
  235. array(
  236. 'rel' => '/h/./j',
  237. 'base' => 'http://www/a/b/',
  238. 'expected' => 'http://www/h/j',
  239. ),
  240. // Rels that starts with "../".
  241. array(
  242. 'rel' => '../h/j',
  243. 'base' => 'http://www',
  244. 'expected' => 'http://www/h/j',
  245. ),
  246. array(
  247. 'rel' => '../h/j',
  248. 'base' => 'http://www/',
  249. 'expected' => 'http://www/h/j',
  250. ),
  251. array(
  252. 'rel' => '../h/j',
  253. 'base' => 'http://www/?c;d=e#f',
  254. 'expected' => 'http://www/h/j',
  255. ),
  256. array(
  257. 'rel' => '../h/j',
  258. 'base' => 'http://www/a/b',
  259. 'expected' => 'http://www/h/j',
  260. ),
  261. array(
  262. 'rel' => '../h/j',
  263. 'base' => 'http://www/a/b/',
  264. 'expected' => 'http://www/a/h/j',
  265. ),
  266. array(
  267. 'rel' => '../h/j',
  268. 'base' => 'http://www/a/b/c/',
  269. 'expected' => 'http://www/a/b/h/j',
  270. ),
  271. // Rels that start with "../../".
  272. array(
  273. 'rel' => '../../h/j',
  274. 'base' => 'http://www',
  275. 'expected' => 'http://www/h/j',
  276. ),
  277. array(
  278. 'rel' => '../../h/j',
  279. 'base' => 'http://www/',
  280. 'expected' => 'http://www/h/j',
  281. ),
  282. array(
  283. 'rel' => '../../h/j',
  284. 'base' => 'http://www/?c;d=e#f',
  285. 'expected' => 'http://www/h/j',
  286. ),
  287. array(
  288. 'rel' => '../../h/j',
  289. 'base' => 'http://www/a/b',
  290. 'expected' => 'http://www/h/j',
  291. ),
  292. array(
  293. 'rel' => '../../h/j',
  294. 'base' => 'http://www/a/b/',
  295. 'expected' => 'http://www/h/j',
  296. ),
  297. array(
  298. 'rel' => '../../h/j',
  299. 'base' => 'http://www/a/b/c/',
  300. 'expected' => 'http://www/a/h/j',
  301. ),
  302. array(
  303. 'rel' => '../../h/j',
  304. 'base' => 'http://www/a/b/c/d',
  305. 'expected' => 'http://www/a/h/j',
  306. ),
  307. // Crazy rels.
  308. array(
  309. 'rel' => 'h/../../j/./k',
  310. 'base' => 'http://www/a/b/c/',
  311. 'expected' => 'http://www/a/b/j/k',
  312. ),
  313. array(
  314. 'rel' => 'h/../../j/./k',
  315. 'base' => 'http://www/a/b/c/d',
  316. 'expected' => 'http://www/a/b/j/k',
  317. ),
  318. array(
  319. 'rel' => '../../../',
  320. 'base' => 'http://www/a/b/c/',
  321. 'expected' => 'http://www/',
  322. ),
  323. array(
  324. 'rel' => 'h/j/k/../../',
  325. 'base' => 'http://www/a/b/c/',
  326. 'expected' => 'http://www/a/b/c/h',
  327. ),
  328. );
  329. foreach ($test_urls as $test_url) {
  330. $result_url = http_request_create_absolute_url($test_url['rel'], $test_url['base']);
  331. $this->assertEqual($test_url['expected'], $result_url, format_string('Creating an absolute URL from base @base and rel @rel resulted into @expected (actual: @actual).', array(
  332. '@actual' => var_export($result_url, TRUE),
  333. '@expected' => var_export($test_url['expected'], TRUE),
  334. '@rel' => var_export($test_url['rel'], TRUE),
  335. '@base' => var_export($test_url['base'], TRUE),
  336. )));
  337. }
  338. }
  339. }