HeaderTest.php 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. <?php
  2. /**
  3. * Unit Tests for Kohana_HTTP_Header
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.http
  8. * @group kohana.core.http.header
  9. * @group kohana.core.http.header
  10. *
  11. * @package Kohana
  12. * @category Tests
  13. * @author Kohana Team
  14. * @copyright (c) Kohana Team
  15. * @license https://koseven.ga/LICENSE.md
  16. */
  17. class Kohana_HTTP_HeaderTest extends Unittest_TestCase {
  18. /**
  19. * Provides data for test_accept_quality
  20. *
  21. * @return array
  22. */
  23. public function provider_accept_quality()
  24. {
  25. return [
  26. [
  27. [
  28. 'text/html; q=1',
  29. 'text/plain; q=.5',
  30. 'application/json; q=.1',
  31. 'text/*'
  32. ],
  33. [
  34. 'text/html' => (float) 1,
  35. 'text/plain' => 0.5,
  36. 'application/json' => 0.1,
  37. 'text/*' => (float) 1
  38. ]
  39. ],
  40. [
  41. [
  42. 'text/*',
  43. 'text/html; level=1; q=0.4',
  44. 'application/xml+rss; q=0.5; level=4'
  45. ],
  46. [
  47. 'text/*' => (float) 1,
  48. 'text/html; level=1' => 0.4,
  49. 'application/xml+rss; level=4' => 0.5
  50. ]
  51. ]
  52. ];
  53. }
  54. /**
  55. * Tests the `accept_quality` method parses the quality values
  56. * correctly out of header parts
  57. *
  58. * @dataProvider provider_accept_quality
  59. *
  60. * @param array $parts input
  61. * @param array $expected expected output
  62. * @return void
  63. */
  64. public function test_accept_quality(array $parts, array $expected)
  65. {
  66. $out = HTTP_Header::accept_quality($parts);
  67. foreach ($out as $key => $value)
  68. {
  69. $this->assertInternalType('float', $value);
  70. }
  71. $this->assertSame($expected, $out);
  72. }
  73. /**
  74. * Data provider for test_parse_accept_header
  75. *
  76. * @return array
  77. */
  78. public function provider_parse_accept_header()
  79. {
  80. return [
  81. [
  82. 'text/html, text/plain, text/*, */*',
  83. [
  84. 'text' => [
  85. 'html' => (float) 1,
  86. 'plain' => (float) 1,
  87. '*' => (float) 1
  88. ],
  89. '*' => [
  90. '*' => (float) 1
  91. ]
  92. ]
  93. ],
  94. [
  95. 'text/html; q=.5, application/json, application/xml+rss; level=1; q=.7, text/*, */*',
  96. [
  97. 'text' => [
  98. 'html' => 0.5,
  99. '*' => (float) 1
  100. ],
  101. 'application' => [
  102. 'json' => (float) 1,
  103. 'xml+rss; level=1' => 0.7
  104. ],
  105. '*' => [
  106. '*' => (float) 1
  107. ]
  108. ]
  109. ]
  110. ];
  111. }
  112. /**
  113. * Tests the `parse_accept_header` method parses the Accept: header
  114. * correctly and returns expected output
  115. *
  116. * @dataProvider provider_parse_accept_header
  117. *
  118. * @param string $accept accept in
  119. * @param array $expected expected out
  120. * @return void
  121. */
  122. public function test_parse_accept_header($accept, array $expected)
  123. {
  124. $this->assertSame($expected, HTTP_Header::parse_accept_header($accept));
  125. }
  126. /**
  127. * Provides data for test_parse_charset_header
  128. *
  129. * @return array
  130. */
  131. public function provider_parse_charset_header()
  132. {
  133. return [
  134. [
  135. 'utf-8, utf-10, utf-16, iso-8859-1',
  136. [
  137. 'utf-8' => (float) 1,
  138. 'utf-10' => (float) 1,
  139. 'utf-16' => (float) 1,
  140. 'iso-8859-1'=> (float) 1
  141. ]
  142. ],
  143. [
  144. 'utf-8, utf-10; q=.9, utf-16; q=.5, iso-8859-1; q=.75',
  145. [
  146. 'utf-8' => (float) 1,
  147. 'utf-10' => 0.9,
  148. 'utf-16' => 0.5,
  149. 'iso-8859-1'=> 0.75
  150. ]
  151. ],
  152. [
  153. NULL,
  154. [
  155. '*' => (float) 1
  156. ]
  157. ]
  158. ];
  159. }
  160. /**
  161. * Tests the `parse_charset_header` method parsed the Accept-Charset header
  162. * correctly
  163. *
  164. * @dataProvider provider_parse_charset_header
  165. *
  166. * @param string $accept accept
  167. * @param array $expected expected
  168. * @return void
  169. */
  170. public function test_parse_charset_header($accept, array $expected)
  171. {
  172. $this->assertSame($expected, HTTP_Header::parse_charset_header($accept));
  173. }
  174. /**
  175. * Provides data for test_parse_charset_header
  176. *
  177. * @return array
  178. */
  179. public function provider_parse_encoding_header()
  180. {
  181. return [
  182. [
  183. 'compress, gzip, blowfish',
  184. [
  185. 'compress' => (float) 1,
  186. 'gzip' => (float) 1,
  187. 'blowfish' => (float) 1
  188. ]
  189. ],
  190. [
  191. 'compress, gzip; q=0.12345, blowfish; q=1.0',
  192. [
  193. 'compress' => (float) 1,
  194. 'gzip' => 0.12345,
  195. 'blowfish' => (float) 1
  196. ]
  197. ],
  198. [
  199. NULL,
  200. [
  201. '*' => (float) 1
  202. ]
  203. ],
  204. [
  205. '',
  206. [
  207. 'identity' => (float) 1
  208. ]
  209. ]
  210. ];
  211. }
  212. /**
  213. * Tests the `parse_encoding_header` method parses the Accept-Encoding header
  214. * correctly
  215. *
  216. * @dataProvider provider_parse_encoding_header
  217. *
  218. * @param string $accept accept
  219. * @param array $expected expected
  220. * @return void
  221. */
  222. public function test_parse_encoding_header($accept, array $expected)
  223. {
  224. $this->assertSame($expected, HTTP_Header::parse_encoding_header($accept));
  225. }
  226. /**
  227. * Provides data for test_parse_charset_header
  228. *
  229. * @return array
  230. */
  231. public function provider_parse_language_header()
  232. {
  233. return [
  234. [
  235. 'en, en-us, en-gb, fr, fr-fr, es-es',
  236. [
  237. 'en' => [
  238. '*' => (float) 1,
  239. 'us' => (float) 1,
  240. 'gb' => (float) 1
  241. ],
  242. 'fr' => [
  243. '*' => (float) 1,
  244. 'fr' => (float) 1
  245. ],
  246. 'es' => [
  247. 'es' => (float) 1
  248. ]
  249. ]
  250. ],
  251. [
  252. 'en; q=.9, en-us, en-gb, fr; q=.5, fr-fr; q=0.4, es-es; q=0.9, en-gb-gb; q=.45',
  253. [
  254. 'en' => [
  255. '*' => 0.9,
  256. 'us' => (float) 1,
  257. 'gb' => (float) 1,
  258. 'gb-gb' => 0.45
  259. ],
  260. 'fr' => [
  261. '*' => 0.5,
  262. 'fr' => 0.4
  263. ],
  264. 'es' => [
  265. 'es' => 0.9
  266. ]
  267. ]
  268. ],
  269. [
  270. NULL,
  271. [
  272. '*' => [
  273. '*' => (float) 1
  274. ]
  275. ]
  276. ]
  277. ];
  278. }
  279. /**
  280. * Tests the `parse_language_header` method parses the Accept-Language header
  281. * correctly
  282. *
  283. * @dataProvider provider_parse_language_header
  284. *
  285. * @param string $accept accept
  286. * @param array $expected expected
  287. * @return void
  288. */
  289. public function test_parse_language_header($accept, array $expected)
  290. {
  291. $this->assertSame($expected, HTTP_Header::parse_language_header($accept));
  292. }
  293. /**
  294. * Data provider for test_create_cache_control
  295. *
  296. * @return array
  297. */
  298. public function provider_create_cache_control()
  299. {
  300. return [
  301. [
  302. [
  303. 'public',
  304. 'max-age' => 1800,
  305. 'must-revalidate',
  306. 's-max-age' => 3600
  307. ],
  308. 'public, max-age=1800, must-revalidate, s-max-age=3600'
  309. ],
  310. [
  311. [
  312. 'max-age' => 1800,
  313. 's-max-age' => 1800,
  314. 'public',
  315. 'must-revalidate',
  316. ],
  317. 'max-age=1800, s-max-age=1800, public, must-revalidate'
  318. ],
  319. [
  320. [
  321. 'private',
  322. 'no-cache',
  323. 'max-age' => 0,
  324. 'must-revalidate'
  325. ],
  326. 'private, no-cache, max-age=0, must-revalidate'
  327. ]
  328. ];
  329. }
  330. /**
  331. * Tests that `create_cache_control()` outputs the correct cache control
  332. * string from the supplied input
  333. *
  334. * @dataProvider provider_create_cache_control
  335. *
  336. * @param array $input input
  337. * @param string $expected expected
  338. * @return void
  339. */
  340. public function test_create_cache_control(array $input, $expected)
  341. {
  342. $this->assertSame($expected, HTTP_Header::create_cache_control($input));
  343. }
  344. /**
  345. * Data provider for parse_cache_control
  346. *
  347. * @return array
  348. */
  349. public function provider_parse_cache_control()
  350. {
  351. return [
  352. [
  353. 'public, max-age=1800, must-revalidate, s-max-age=3600',
  354. [
  355. 'public',
  356. 'max-age' => 1800,
  357. 'must-revalidate',
  358. 's-max-age' => 3600
  359. ]
  360. ],
  361. [
  362. 'max-age=1800, s-max-age=1800, public, must-revalidate',
  363. [
  364. 'max-age' => 1800,
  365. 's-max-age' => 1800,
  366. 'public',
  367. 'must-revalidate',
  368. ]
  369. ],
  370. [
  371. 'private, no-cache, max-age=0, must-revalidate',
  372. [
  373. 'private',
  374. 'no-cache',
  375. 'max-age' => 0,
  376. 'must-revalidate'
  377. ]
  378. ]
  379. ];
  380. }
  381. /**
  382. * Tests that `parse_cache_control()` outputs the correct cache control
  383. * parsed data from the input string
  384. *
  385. * @dataProvider provider_parse_cache_control
  386. *
  387. * @param string $input input
  388. * @param array $expected expected
  389. * @return void
  390. */
  391. public function test_parse_cache_control($input, array $expected)
  392. {
  393. $parsed = HTTP_Header::parse_cache_control($input);
  394. $this->assertInternalType('array', $parsed);
  395. foreach ($expected as $key => $value)
  396. {
  397. if (is_int($key))
  398. {
  399. $this->assertTrue(in_array($value, $parsed));
  400. }
  401. else
  402. {
  403. $this->assertTrue(array_key_exists($key, $parsed));
  404. $this->assertSame($value, $parsed[$key]);
  405. }
  406. }
  407. }
  408. /**
  409. * Data provider for test_offsetSet
  410. *
  411. * @return array
  412. */
  413. // @codingStandardsIgnoreStart
  414. public function provider_offsetSet()
  415. // @codingStandardsIgnoreEnd
  416. {
  417. return [
  418. [
  419. [
  420. 'Content-Type' => 'application/x-www-form-urlencoded',
  421. 'Accept' => 'text/html, text/plain; q=.1, */*',
  422. 'Accept-Language' => 'en-gb, en-us, en; q=.1'
  423. ],
  424. [
  425. [
  426. 'Accept-Encoding',
  427. 'compress, gzip',
  428. FALSE
  429. ]
  430. ],
  431. [
  432. 'content-type' => 'application/x-www-form-urlencoded',
  433. 'accept' => 'text/html, text/plain; q=.1, */*',
  434. 'accept-language' => 'en-gb, en-us, en; q=.1',
  435. 'accept-encoding' => 'compress, gzip'
  436. ]
  437. ],
  438. [
  439. [
  440. 'Content-Type' => 'application/x-www-form-urlencoded',
  441. 'Accept' => 'text/html, text/plain; q=.1, */*',
  442. 'Accept-Language' => 'en-gb, en-us, en; q=.1'
  443. ],
  444. [
  445. [
  446. 'Accept-Encoding',
  447. 'compress, gzip',
  448. FALSE
  449. ],
  450. [
  451. 'Accept-Encoding',
  452. 'bzip',
  453. FALSE
  454. ]
  455. ],
  456. [
  457. 'content-type' => 'application/x-www-form-urlencoded',
  458. 'accept' => 'text/html, text/plain; q=.1, */*',
  459. 'accept-language' => 'en-gb, en-us, en; q=.1',
  460. 'accept-encoding' => [
  461. 'compress, gzip',
  462. 'bzip'
  463. ]
  464. ]
  465. ],
  466. [
  467. [
  468. 'Content-Type' => 'application/x-www-form-urlencoded',
  469. 'Accept' => 'text/html, text/plain; q=.1, */*',
  470. 'Accept-Language' => 'en-gb, en-us, en; q=.1'
  471. ],
  472. [
  473. [
  474. 'Accept-Encoding',
  475. 'compress, gzip',
  476. FALSE
  477. ],
  478. [
  479. 'Accept-Encoding',
  480. 'bzip',
  481. TRUE
  482. ],
  483. [
  484. 'Accept',
  485. 'text/*',
  486. FALSE
  487. ]
  488. ],
  489. [
  490. 'content-type' => 'application/x-www-form-urlencoded',
  491. 'accept' => [
  492. 'text/html, text/plain; q=.1, */*',
  493. 'text/*'
  494. ],
  495. 'accept-language' => 'en-gb, en-us, en; q=.1',
  496. 'accept-encoding' => 'bzip'
  497. ]
  498. ],
  499. ];
  500. }
  501. /**
  502. * Ensures that offsetSet normalizes the array keys
  503. *
  504. * @dataProvider provider_offsetSet
  505. *
  506. * @param array $constructor constructor
  507. * @param array $to_set to_set
  508. * @param array $expected expected
  509. * @return void
  510. */
  511. // @codingStandardsIgnoreStart
  512. public function test_offsetSet(array $constructor, array $to_set, array $expected)
  513. // @codingStandardsIgnoreEnd
  514. {
  515. $http_header = new HTTP_Header($constructor);
  516. $reflection = new ReflectionClass($http_header);
  517. $method = $reflection->getMethod('offsetSet');
  518. foreach ($to_set as $args)
  519. {
  520. $method->invokeArgs($http_header, $args);
  521. }
  522. $this->assertSame($expected, $http_header->getArrayCopy());
  523. }
  524. /**
  525. * Data provider for test_offsetGet
  526. *
  527. * @return array
  528. */
  529. // @codingStandardsIgnoreStart
  530. public function provider_offsetGet()
  531. // @codingStandardsIgnoreEnd
  532. {
  533. return [
  534. [
  535. [
  536. 'FoO' => 'bar',
  537. 'START' => 'end',
  538. 'true' => TRUE
  539. ],
  540. 'FOO',
  541. 'bar'
  542. ],
  543. [
  544. [
  545. 'FoO' => 'bar',
  546. 'START' => 'end',
  547. 'true' => TRUE
  548. ],
  549. 'true',
  550. TRUE
  551. ],
  552. [
  553. [
  554. 'FoO' => 'bar',
  555. 'START' => 'end',
  556. 'true' => TRUE
  557. ],
  558. 'True',
  559. TRUE
  560. ],
  561. [
  562. [
  563. 'FoO' => 'bar',
  564. 'START' => 'end',
  565. 'true' => TRUE
  566. ],
  567. 'Start',
  568. 'end'
  569. ],
  570. [
  571. [
  572. 'content-type' => 'bar',
  573. 'Content-Type' => 'end',
  574. 'Accept' => '*/*'
  575. ],
  576. 'content-type',
  577. 'end'
  578. ]
  579. ];
  580. }
  581. /**
  582. * Ensures that offsetGet normalizes the array keys
  583. *
  584. * @dataProvider provider_offsetGet
  585. *
  586. * @param array start state
  587. * @param string key to retrieve
  588. * @param mixed expected
  589. * @return void
  590. */
  591. // @codingStandardsIgnoreStart
  592. public function test_offsetGet(array $state, $key, $expected)
  593. // @codingStandardsIgnoreEnd
  594. {
  595. $header = new HTTP_Header($state);
  596. $this->assertSame($expected, $header->offsetGet($key));
  597. }
  598. /**
  599. * Data provider for test_offsetExists
  600. *
  601. * @return array
  602. */
  603. // @codingStandardsIgnoreStart
  604. public function provider_offsetExists()
  605. // @codingStandardsIgnoreEnd
  606. {
  607. return [
  608. [
  609. [
  610. 'Accept' => 'text/html, application/json',
  611. 'Accept-Language' => 'en, en-GB',
  612. 'Content-Type' => 'application/x-www-form-urlencoded'
  613. ],
  614. 'Content-Type',
  615. TRUE
  616. ],
  617. [
  618. [
  619. 'Accept' => 'text/html, application/json',
  620. 'Accept-Language' => 'en, en-GB',
  621. 'Content-Type' => 'application/x-www-form-urlencoded'
  622. ],
  623. 'CONTENT-TYPE',
  624. TRUE
  625. ],
  626. [
  627. [
  628. 'Accept' => 'text/html, application/json',
  629. 'Accept-Language' => 'en, en-GB',
  630. 'Content-Type' => 'application/x-www-form-urlencoded'
  631. ],
  632. 'accept-language',
  633. TRUE
  634. ],
  635. [
  636. [
  637. 'Accept' => 'text/html, application/json',
  638. 'Accept-Language' => 'en, en-GB',
  639. 'Content-Type' => 'application/x-www-form-urlencoded'
  640. ],
  641. 'x-powered-by',
  642. FALSE
  643. ]
  644. ];
  645. }
  646. /**
  647. * Ensures that offsetExists normalizes the array key
  648. *
  649. * @dataProvider provider_offsetExists
  650. *
  651. * @param array $state state
  652. * @param string $key key
  653. * @param boolean $expected expected
  654. * @return void
  655. */
  656. // @codingStandardsIgnoreStart
  657. public function test_offsetExists(array $state, $key, $expected)
  658. // @codingStandardsIgnoreEnd
  659. {
  660. $header = new HTTP_Header($state);
  661. $this->assertSame($expected, $header->offsetExists($key));
  662. }
  663. /**
  664. * Data provider for test_offsetUnset
  665. *
  666. * @return array
  667. */
  668. // @codingStandardsIgnoreStart
  669. public function provider_offsetUnset()
  670. // @codingStandardsIgnoreEnd
  671. {
  672. return [
  673. [
  674. [
  675. 'Accept' => 'text/html, application/json',
  676. 'Accept-Language' => 'en, en-GB',
  677. 'Content-Type' => 'application/x-www-form-urlencoded'
  678. ],
  679. 'Accept-Language',
  680. [
  681. 'accept' => 'text/html, application/json',
  682. 'content-type' => 'application/x-www-form-urlencoded'
  683. ]
  684. ],
  685. [
  686. [
  687. 'Accept' => 'text/html, application/json',
  688. 'Accept-Language' => 'en, en-GB',
  689. 'Content-Type' => 'application/x-www-form-urlencoded'
  690. ],
  691. 'ACCEPT',
  692. [
  693. 'accept-language' => 'en, en-GB',
  694. 'content-type' => 'application/x-www-form-urlencoded'
  695. ]
  696. ],
  697. [
  698. [
  699. 'Accept' => 'text/html, application/json',
  700. 'Accept-Language' => 'en, en-GB',
  701. 'Content-Type' => 'application/x-www-form-urlencoded'
  702. ],
  703. 'content-type',
  704. [
  705. 'accept' => 'text/html, application/json',
  706. 'accept-language' => 'en, en-GB',
  707. ]
  708. ]
  709. ];
  710. }
  711. /**
  712. * Tests that `offsetUnset` normalizes the key names properly
  713. *
  714. * @dataProvider provider_offsetUnset
  715. *
  716. * @param array $state state
  717. * @param string $remove remove
  718. * @param array $expected expected
  719. * @return void
  720. */
  721. // @codingStandardsIgnoreStart
  722. public function test_offsetUnset(array $state, $remove, array $expected)
  723. // @codingStandardsIgnoreEnd
  724. {
  725. $header = new HTTP_Header($state);
  726. $header->offsetUnset($remove);
  727. $this->assertSame($expected, $header->getArrayCopy());
  728. }
  729. /**
  730. * Provides data for test_parse_header_string
  731. *
  732. * @return array
  733. */
  734. public function provider_parse_header_string()
  735. {
  736. return [
  737. [
  738. [
  739. "Content-Type: application/x-www-form-urlencoded\r\n",
  740. "Accept: text/html, text/plain; q=.5, application/json, */* \r\n",
  741. "X-Powered-By: Kohana Baby \r\n"
  742. ],
  743. [
  744. 'content-type' => 'application/x-www-form-urlencoded',
  745. 'accept' => 'text/html, text/plain; q=.5, application/json, */* ',
  746. 'x-powered-by' => 'Kohana Baby '
  747. ]
  748. ],
  749. [
  750. [
  751. "Content-Type: application/x-www-form-urlencoded\r\n",
  752. "Accept: text/html, text/plain; q=.5, application/json, */* \r\n",
  753. "X-Powered-By: Kohana Baby \r\n",
  754. "Content-Type: application/json\r\n"
  755. ],
  756. [
  757. 'content-type' => [
  758. 'application/x-www-form-urlencoded',
  759. 'application/json'
  760. ],
  761. 'accept' => 'text/html, text/plain; q=.5, application/json, */* ',
  762. 'x-powered-by' => 'Kohana Baby '
  763. ]
  764. ]
  765. ];
  766. }
  767. /**
  768. * Tests that `parse_header_string` performs as expected
  769. *
  770. * @dataProvider provider_parse_header_string
  771. *
  772. * @param array headers
  773. * @param array expected
  774. * @return void
  775. */
  776. public function test_parse_header_string(array $headers, array $expected)
  777. {
  778. $http_header = new HTTP_Header([]);
  779. foreach ($headers as $header)
  780. {
  781. $this->assertEquals(strlen($header), $http_header->parse_header_string(NULL, $header));
  782. }
  783. $this->assertSame($expected, $http_header->getArrayCopy());
  784. }
  785. /**
  786. * Data Provider for test_accepts_at_quality
  787. *
  788. * @return array
  789. */
  790. public function provider_accepts_at_quality()
  791. {
  792. return [
  793. [
  794. [
  795. 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
  796. ],
  797. 'application/json',
  798. FALSE,
  799. 1.0
  800. ],
  801. [
  802. [
  803. 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
  804. ],
  805. 'text/html',
  806. FALSE,
  807. 0.5
  808. ],
  809. [
  810. [
  811. 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
  812. ],
  813. 'text/plain',
  814. FALSE,
  815. 0.1
  816. ],
  817. [
  818. [
  819. 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
  820. ],
  821. 'text/plain',
  822. TRUE,
  823. FALSE
  824. ],
  825. [
  826. [
  827. 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
  828. ],
  829. 'application/xml',
  830. FALSE,
  831. 1.0
  832. ],
  833. [
  834. [
  835. 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
  836. ],
  837. 'application/xml',
  838. TRUE,
  839. FALSE
  840. ],
  841. [
  842. [],
  843. 'application/xml',
  844. FALSE,
  845. 1.0
  846. ],
  847. [
  848. [],
  849. 'application/xml',
  850. TRUE,
  851. FALSE
  852. ]
  853. ];
  854. }
  855. /**
  856. * Tests `accepts_at_quality` parsed the Accept: header as expected
  857. *
  858. * @dataProvider provider_accepts_at_quality
  859. *
  860. * @param array starting state
  861. * @param string accept header to test
  862. * @param boolean explicitly check
  863. * @param mixed expected output
  864. * @return void
  865. */
  866. public function test_accepts_at_quality(array $state, $accept, $explicit, $expected)
  867. {
  868. $header = new HTTP_Header($state);
  869. $this->assertSame($expected, $header->accepts_at_quality($accept, $explicit));
  870. }
  871. /**
  872. * Data provider for test_preferred_accept
  873. *
  874. * @return array
  875. */
  876. public function provider_preferred_accept()
  877. {
  878. return [
  879. [
  880. [
  881. 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
  882. ],
  883. [
  884. 'text/html',
  885. 'application/json',
  886. 'text/plain'
  887. ],
  888. FALSE,
  889. 'application/json'
  890. ],
  891. [
  892. [
  893. 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
  894. ],
  895. [
  896. 'text/plain',
  897. 'application/xml',
  898. 'image/jpeg'
  899. ],
  900. FALSE,
  901. 'application/xml'
  902. ],
  903. [
  904. [
  905. 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1'
  906. ],
  907. [
  908. 'text/plain',
  909. 'application/xml',
  910. 'image/jpeg'
  911. ],
  912. FALSE,
  913. 'text/plain'
  914. ],
  915. [
  916. [
  917. 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
  918. ],
  919. [
  920. 'text/plain',
  921. 'application/xml',
  922. 'image/jpeg'
  923. ],
  924. TRUE,
  925. FALSE
  926. ],
  927. ];
  928. }
  929. /**
  930. * Tests `preferred_accept` returns the correct preferred type
  931. *
  932. * @dataProvider provider_preferred_accept
  933. *
  934. * @param array state
  935. * @param array accepts
  936. * @param string explicit
  937. * @param string expected
  938. * @return void
  939. */
  940. public function test_preferred_accept(array $state, array $accepts, $explicit, $expected)
  941. {
  942. $header = new HTTP_Header($state);
  943. $this->assertSame($expected, $header->preferred_accept($accepts, $explicit));
  944. }
  945. /**
  946. * Data provider for test_accepts_charset_at_quality
  947. *
  948. * @return array
  949. */
  950. public function provider_accepts_charset_at_quality()
  951. {
  952. return [
  953. [
  954. [
  955. 'Accept-Charset' => 'utf-8, utf-10, utf-16, iso-8859-1'
  956. ],
  957. 'utf-8',
  958. 1.0
  959. ],
  960. [
  961. [
  962. 'Accept-Charset' => 'utf-8, utf-10, utf-16, iso-8859-1'
  963. ],
  964. 'utf-16',
  965. 1.0
  966. ],
  967. [
  968. [
  969. 'Accept-Charset' => 'utf-8; q=.1, utf-10, utf-16; q=.2, iso-8859-1'
  970. ],
  971. 'utf-8',
  972. 0.1
  973. ],
  974. [
  975. [
  976. 'Accept-Charset' => 'utf-8; q=.1, utf-10, utf-16; q=.2, iso-8859-1; q=.5'
  977. ],
  978. 'iso-8859-1',
  979. 0.5
  980. ]
  981. ];
  982. }
  983. /**
  984. * Tests `accepts_charset_at_quality` works as expected, returning the correct
  985. * quality value
  986. *
  987. * @dataProvider provider_accepts_charset_at_quality
  988. *
  989. * @param array state
  990. * @param string charset
  991. * @param string expected
  992. * @return void
  993. */
  994. public function test_accepts_charset_at_quality(array $state, $charset, $expected)
  995. {
  996. $header = new HTTP_Header($state);
  997. $this->assertSame($expected, $header->accepts_charset_at_quality($charset));
  998. }
  999. /**
  1000. * Data provider for test_preferred_charset
  1001. *
  1002. * @return array
  1003. */
  1004. public function provider_preferred_charset()
  1005. {
  1006. return [
  1007. [
  1008. [
  1009. 'Accept-Charset' => 'utf-8, utf-10, utf-16, iso-8859-1'
  1010. ],
  1011. [
  1012. 'utf-8',
  1013. 'utf-16'
  1014. ],
  1015. 'utf-8'
  1016. ],
  1017. [
  1018. [
  1019. 'Accept-Charset' => 'utf-8, utf-10, utf-16, iso-8859-1'
  1020. ],
  1021. [
  1022. 'UTF-10'
  1023. ],
  1024. 'UTF-10'
  1025. ],
  1026. ];
  1027. }
  1028. /**
  1029. * Tests `preferred_charset` works as expected, returning the correct charset
  1030. * from the list supplied
  1031. *
  1032. * @dataProvider provider_preferred_charset
  1033. *
  1034. * @param array state
  1035. * @param array charsets
  1036. * @param string expected
  1037. * @return void
  1038. */
  1039. public function test_preferred_charset(array $state, array $charsets, $expected)
  1040. {
  1041. $header = new HTTP_Header($state);
  1042. $this->assertSame($expected, $header->preferred_charset($charsets));
  1043. }
  1044. /**
  1045. * Data provider for test_accepts_encoding_at_quality
  1046. *
  1047. * @return array
  1048. */
  1049. public function provider_accepts_encoding_at_quality()
  1050. {
  1051. return [
  1052. [
  1053. [
  1054. 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
  1055. ],
  1056. 'gzip',
  1057. FALSE,
  1058. 1.0
  1059. ],
  1060. [
  1061. [
  1062. 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
  1063. ],
  1064. 'gzip',
  1065. TRUE,
  1066. 1.0
  1067. ],
  1068. [
  1069. [
  1070. 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
  1071. ],
  1072. 'blowfish',
  1073. FALSE,
  1074. 0.7
  1075. ],
  1076. [
  1077. [
  1078. 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
  1079. ],
  1080. 'bzip',
  1081. FALSE,
  1082. 0.5
  1083. ],
  1084. [
  1085. [
  1086. 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
  1087. ],
  1088. 'bzip',
  1089. TRUE,
  1090. (float) 0
  1091. ]
  1092. ];
  1093. }
  1094. /**
  1095. * Tests `accepts_encoding_at_quality` parses and returns the correct
  1096. * quality value for Accept-Encoding headers
  1097. *
  1098. * @dataProvider provider_accepts_encoding_at_quality
  1099. *
  1100. * @param array state
  1101. * @param string encoding
  1102. * @param boolean explicit
  1103. * @param float expected
  1104. * @return void
  1105. */
  1106. public function test_accepts_encoding_at_quality(array $state, $encoding, $explicit, $expected)
  1107. {
  1108. $header = new HTTP_Header($state);
  1109. $this->assertSame($expected, $header->accepts_encoding_at_quality($encoding, $explicit));
  1110. }
  1111. /**
  1112. * Data provider for test_preferred_encoding
  1113. *
  1114. * @return array
  1115. */
  1116. public function provider_preferred_encoding()
  1117. {
  1118. return [
  1119. [
  1120. [
  1121. 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
  1122. ],
  1123. ['gzip', 'blowfish', 'bzip'],
  1124. FALSE,
  1125. 'gzip'
  1126. ],
  1127. [
  1128. [
  1129. 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
  1130. ],
  1131. ['bzip', 'ROT-13'],
  1132. FALSE,
  1133. 'bzip'
  1134. ],
  1135. [
  1136. [
  1137. 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
  1138. ],
  1139. ['bzip', 'ROT-13'],
  1140. TRUE,
  1141. FALSE
  1142. ],
  1143. [
  1144. [
  1145. 'accept-encoding' => 'compress, gzip, blowfish; q=.2, *; q=.5'
  1146. ],
  1147. ['ROT-13', 'blowfish'],
  1148. FALSE,
  1149. 'ROT-13'
  1150. ],
  1151. ];
  1152. }
  1153. /**
  1154. * Tests that `preferred_encoding` parses and returns the correct
  1155. * encoding type
  1156. *
  1157. * @dataProvider provider_preferred_encoding
  1158. *
  1159. * @param array state in
  1160. * @param array encodings to interrogate
  1161. * @param boolean explicit check
  1162. * @param string expected output
  1163. * @return void
  1164. */
  1165. public function test_preferred_encoding(array $state, array $encodings, $explicit, $expected)
  1166. {
  1167. $header = new HTTP_Header($state);
  1168. $this->assertSame($expected, $header->preferred_encoding($encodings, $explicit));
  1169. }
  1170. /**
  1171. * Data provider for test_accepts_language_at_quality
  1172. *
  1173. * @return array
  1174. */
  1175. public function provider_accepts_language_at_quality()
  1176. {
  1177. return [
  1178. [
  1179. [
  1180. 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
  1181. ],
  1182. 'en',
  1183. FALSE,
  1184. 0.5
  1185. ],
  1186. [
  1187. [
  1188. 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
  1189. ],
  1190. 'en-gb',
  1191. FALSE,
  1192. 0.7
  1193. ],
  1194. [
  1195. [
  1196. 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
  1197. ],
  1198. 'en',
  1199. TRUE,
  1200. 0.5
  1201. ],
  1202. [
  1203. [
  1204. 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
  1205. ],
  1206. 'fr-ni',
  1207. FALSE,
  1208. 0.8
  1209. ],
  1210. [
  1211. [
  1212. 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
  1213. ],
  1214. 'fr-ni',
  1215. TRUE,
  1216. (float) 0
  1217. ],
  1218. [
  1219. [
  1220. 'accept-language' => 'en-US'
  1221. ],
  1222. 'en-us',
  1223. TRUE,
  1224. (float) 1
  1225. ],
  1226. ];
  1227. }
  1228. /**
  1229. * Tests `accepts_language_at_quality` parses the Accept-Language header
  1230. * correctly and identifies the correct quality supplied, explicit or not
  1231. *
  1232. * @dataProvider provider_accepts_language_at_quality
  1233. *
  1234. * @param array state in
  1235. * @param string language to interrogate
  1236. * @param boolean explicit check
  1237. * @param float expected output
  1238. * @return void
  1239. */
  1240. public function test_accepts_language_at_quality(array $state, $language, $explicit, $expected)
  1241. {
  1242. $header = new HTTP_Header($state);
  1243. $this->assertSame($expected, $header->accepts_language_at_quality($language, $explicit));
  1244. }
  1245. /**
  1246. * Data provider for test_preferred_language
  1247. *
  1248. * @return array
  1249. */
  1250. public function provider_preferred_language()
  1251. {
  1252. return [
  1253. [
  1254. [
  1255. 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
  1256. ],
  1257. [
  1258. 'en',
  1259. 'fr',
  1260. 'en-gb'
  1261. ],
  1262. FALSE,
  1263. 'fr'
  1264. ],
  1265. [
  1266. [
  1267. 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
  1268. ],
  1269. [
  1270. 'en',
  1271. 'fr',
  1272. 'en-gb'
  1273. ],
  1274. TRUE,
  1275. 'fr'
  1276. ],
  1277. [
  1278. [
  1279. 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
  1280. ],
  1281. [
  1282. 'en-au',
  1283. 'fr-ni',
  1284. 'fr'
  1285. ],
  1286. FALSE,
  1287. 'fr-ni'
  1288. ],
  1289. [
  1290. [
  1291. 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
  1292. ],
  1293. [
  1294. 'en-au',
  1295. 'fr-ni',
  1296. 'fr'
  1297. ],
  1298. TRUE,
  1299. 'fr'
  1300. ],
  1301. [
  1302. [
  1303. 'accept-language' => 'en-US'
  1304. ],
  1305. [
  1306. 'en-us'
  1307. ],
  1308. TRUE,
  1309. 'en-us'
  1310. ],
  1311. [
  1312. [
  1313. 'accept-language' => 'fr,en-US,en'
  1314. ],
  1315. [
  1316. 'en-us',
  1317. 'fr-fr'
  1318. ],
  1319. FALSE,
  1320. 'fr-fr'
  1321. ],
  1322. [
  1323. [
  1324. 'accept-language' => 'fr,en-US,en'
  1325. ],
  1326. [
  1327. 'fr-fr',
  1328. 'en-us'
  1329. ],
  1330. FALSE,
  1331. 'fr-fr'
  1332. ],
  1333. [
  1334. [
  1335. 'accept-language' => 'fr,en,en-US'
  1336. ],
  1337. [
  1338. 'en-us',
  1339. 'fr-fr'
  1340. ],
  1341. TRUE,
  1342. 'en-us'
  1343. ],
  1344. [
  1345. [
  1346. 'accept-language' => 'fr,en,en-US'
  1347. ],
  1348. [
  1349. 'fr-fr',
  1350. 'en-us'
  1351. ],
  1352. TRUE,
  1353. 'en-us'
  1354. ],
  1355. ];
  1356. }
  1357. /**
  1358. * Tests that `preferred_language` correctly identifies the right
  1359. * language based on the Accept-Language header and `$explicit` setting
  1360. *
  1361. * @dataProvider provider_preferred_language
  1362. *
  1363. * @param array state in
  1364. * @param array languages to interrogate
  1365. * @param boolean explicit check
  1366. * @param string expected output
  1367. * @return void
  1368. */
  1369. public function test_preferred_language(array $state, array $languages, $explicit, $expected)
  1370. {
  1371. $header = new HTTP_Header($state);
  1372. $this->assertSame($expected, $header->preferred_language($languages, $explicit));
  1373. }
  1374. /**
  1375. * Data provider for test_send_headers
  1376. *
  1377. * @return array
  1378. */
  1379. public function provider_send_headers()
  1380. {
  1381. $content_type = Kohana::$content_type.'; charset='.Kohana::$charset;
  1382. return [
  1383. [
  1384. [],
  1385. [
  1386. 'HTTP/1.1 200 OK',
  1387. 'Content-Type: '.$content_type,
  1388. ],
  1389. FALSE,
  1390. ],
  1391. [
  1392. [],
  1393. [
  1394. 'HTTP/1.1 200 OK',
  1395. 'Content-Type: '.$content_type,
  1396. 'X-Powered-By: '.Kohana::version(),
  1397. ],
  1398. TRUE,
  1399. ],
  1400. [
  1401. [
  1402. 'accept' => 'text/html, text/plain, text/*, */*',
  1403. 'accept-charset' => 'utf-8, utf-10, iso-8859-1',
  1404. 'accept-encoding' => 'compress, gzip',
  1405. 'accept-language' => 'en, en-gb, en-us'
  1406. ],
  1407. [
  1408. 'HTTP/1.1 200 OK',
  1409. 'Accept: text/html, text/plain, text/*, */*',
  1410. 'Accept-Charset: utf-8, utf-10, iso-8859-1',
  1411. 'Accept-Encoding: compress, gzip',
  1412. 'Accept-Language: en, en-gb, en-us',
  1413. 'Content-Type: '.$content_type,
  1414. ],
  1415. FALSE
  1416. ],
  1417. [
  1418. [
  1419. 'accept' => 'text/html, text/plain, text/*, */*',
  1420. 'accept-charset' => 'utf-8, utf-10, iso-8859-1',
  1421. 'accept-encoding' => 'compress, gzip',
  1422. 'accept-language' => 'en, en-gb, en-us',
  1423. 'content-type' => 'application/json',
  1424. 'x-powered-by' => 'Mohana',
  1425. 'x-ssl-enabled' => 'TRUE'
  1426. ],
  1427. [
  1428. 'HTTP/1.1 200 OK',
  1429. 'Accept: text/html, text/plain, text/*, */*',
  1430. 'Accept-Charset: utf-8, utf-10, iso-8859-1',
  1431. 'Accept-Encoding: compress, gzip',
  1432. 'Accept-Language: en, en-gb, en-us',
  1433. 'Content-Type: application/json',
  1434. 'X-Powered-By: Mohana',
  1435. 'X-Ssl-Enabled: TRUE'
  1436. ],
  1437. TRUE
  1438. ]
  1439. ];
  1440. }
  1441. /**
  1442. * Tests that send headers processes the headers sent to PHP correctly
  1443. *
  1444. * @dataProvider provider_send_headers
  1445. *
  1446. * @param array state in
  1447. * @param array expected out
  1448. * @return void
  1449. */
  1450. public function test_send_headers(array $state, array $expected, $expose)
  1451. {
  1452. Kohana::$expose = $expose;
  1453. $response = new Response;
  1454. $response->headers($state);
  1455. $this->assertSame(
  1456. $expected,
  1457. $response->send_headers(FALSE, [$this, 'send_headers_handler'])
  1458. );
  1459. }
  1460. /**
  1461. * Callback handler for send headers
  1462. *
  1463. * @param array headers
  1464. * @param boolean replace
  1465. * @return array
  1466. */
  1467. public function send_headers_handler($response, $headers, $replace)
  1468. {
  1469. return $headers;
  1470. }
  1471. } // End Kohana_HTTP_HeaderTest