12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535 |
- <?php
- /**
- * Unit Tests for Kohana_HTTP_Header
- *
- * @group kohana
- * @group kohana.core
- * @group kohana.core.http
- * @group kohana.core.http.header
- * @group kohana.core.http.header
- *
- * @package Kohana
- * @category Tests
- * @author Kohana Team
- * @copyright (c) Kohana Team
- * @license https://koseven.ga/LICENSE.md
- */
- class Kohana_HTTP_HeaderTest extends Unittest_TestCase {
- /**
- * Provides data for test_accept_quality
- *
- * @return array
- */
- public function provider_accept_quality()
- {
- return [
- [
- [
- 'text/html; q=1',
- 'text/plain; q=.5',
- 'application/json; q=.1',
- 'text/*'
- ],
- [
- 'text/html' => (float) 1,
- 'text/plain' => 0.5,
- 'application/json' => 0.1,
- 'text/*' => (float) 1
- ]
- ],
- [
- [
- 'text/*',
- 'text/html; level=1; q=0.4',
- 'application/xml+rss; q=0.5; level=4'
- ],
- [
- 'text/*' => (float) 1,
- 'text/html; level=1' => 0.4,
- 'application/xml+rss; level=4' => 0.5
- ]
- ]
- ];
- }
- /**
- * Tests the `accept_quality` method parses the quality values
- * correctly out of header parts
- *
- * @dataProvider provider_accept_quality
- *
- * @param array $parts input
- * @param array $expected expected output
- * @return void
- */
- public function test_accept_quality(array $parts, array $expected)
- {
- $out = HTTP_Header::accept_quality($parts);
- foreach ($out as $key => $value)
- {
- $this->assertInternalType('float', $value);
- }
- $this->assertSame($expected, $out);
- }
- /**
- * Data provider for test_parse_accept_header
- *
- * @return array
- */
- public function provider_parse_accept_header()
- {
- return [
- [
- 'text/html, text/plain, text/*, */*',
- [
- 'text' => [
- 'html' => (float) 1,
- 'plain' => (float) 1,
- '*' => (float) 1
- ],
- '*' => [
- '*' => (float) 1
- ]
- ]
- ],
- [
- 'text/html; q=.5, application/json, application/xml+rss; level=1; q=.7, text/*, */*',
- [
- 'text' => [
- 'html' => 0.5,
- '*' => (float) 1
- ],
- 'application' => [
- 'json' => (float) 1,
- 'xml+rss; level=1' => 0.7
- ],
- '*' => [
- '*' => (float) 1
- ]
- ]
- ]
- ];
- }
- /**
- * Tests the `parse_accept_header` method parses the Accept: header
- * correctly and returns expected output
- *
- * @dataProvider provider_parse_accept_header
- *
- * @param string $accept accept in
- * @param array $expected expected out
- * @return void
- */
- public function test_parse_accept_header($accept, array $expected)
- {
- $this->assertSame($expected, HTTP_Header::parse_accept_header($accept));
- }
- /**
- * Provides data for test_parse_charset_header
- *
- * @return array
- */
- public function provider_parse_charset_header()
- {
- return [
- [
- 'utf-8, utf-10, utf-16, iso-8859-1',
- [
- 'utf-8' => (float) 1,
- 'utf-10' => (float) 1,
- 'utf-16' => (float) 1,
- 'iso-8859-1'=> (float) 1
- ]
- ],
- [
- 'utf-8, utf-10; q=.9, utf-16; q=.5, iso-8859-1; q=.75',
- [
- 'utf-8' => (float) 1,
- 'utf-10' => 0.9,
- 'utf-16' => 0.5,
- 'iso-8859-1'=> 0.75
- ]
- ],
- [
- NULL,
- [
- '*' => (float) 1
- ]
- ]
- ];
- }
- /**
- * Tests the `parse_charset_header` method parsed the Accept-Charset header
- * correctly
- *
- * @dataProvider provider_parse_charset_header
- *
- * @param string $accept accept
- * @param array $expected expected
- * @return void
- */
- public function test_parse_charset_header($accept, array $expected)
- {
- $this->assertSame($expected, HTTP_Header::parse_charset_header($accept));
- }
- /**
- * Provides data for test_parse_charset_header
- *
- * @return array
- */
- public function provider_parse_encoding_header()
- {
- return [
- [
- 'compress, gzip, blowfish',
- [
- 'compress' => (float) 1,
- 'gzip' => (float) 1,
- 'blowfish' => (float) 1
- ]
- ],
- [
- 'compress, gzip; q=0.12345, blowfish; q=1.0',
- [
- 'compress' => (float) 1,
- 'gzip' => 0.12345,
- 'blowfish' => (float) 1
- ]
- ],
- [
- NULL,
- [
- '*' => (float) 1
- ]
- ],
- [
- '',
- [
- 'identity' => (float) 1
- ]
- ]
- ];
- }
- /**
- * Tests the `parse_encoding_header` method parses the Accept-Encoding header
- * correctly
- *
- * @dataProvider provider_parse_encoding_header
- *
- * @param string $accept accept
- * @param array $expected expected
- * @return void
- */
- public function test_parse_encoding_header($accept, array $expected)
- {
- $this->assertSame($expected, HTTP_Header::parse_encoding_header($accept));
- }
- /**
- * Provides data for test_parse_charset_header
- *
- * @return array
- */
- public function provider_parse_language_header()
- {
- return [
- [
- 'en, en-us, en-gb, fr, fr-fr, es-es',
- [
- 'en' => [
- '*' => (float) 1,
- 'us' => (float) 1,
- 'gb' => (float) 1
- ],
- 'fr' => [
- '*' => (float) 1,
- 'fr' => (float) 1
- ],
- 'es' => [
- 'es' => (float) 1
- ]
- ]
- ],
- [
- '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',
- [
- 'en' => [
- '*' => 0.9,
- 'us' => (float) 1,
- 'gb' => (float) 1,
- 'gb-gb' => 0.45
- ],
- 'fr' => [
- '*' => 0.5,
- 'fr' => 0.4
- ],
- 'es' => [
- 'es' => 0.9
- ]
- ]
- ],
- [
- NULL,
- [
- '*' => [
- '*' => (float) 1
- ]
- ]
- ]
- ];
- }
- /**
- * Tests the `parse_language_header` method parses the Accept-Language header
- * correctly
- *
- * @dataProvider provider_parse_language_header
- *
- * @param string $accept accept
- * @param array $expected expected
- * @return void
- */
- public function test_parse_language_header($accept, array $expected)
- {
- $this->assertSame($expected, HTTP_Header::parse_language_header($accept));
- }
- /**
- * Data provider for test_create_cache_control
- *
- * @return array
- */
- public function provider_create_cache_control()
- {
- return [
- [
- [
- 'public',
- 'max-age' => 1800,
- 'must-revalidate',
- 's-max-age' => 3600
- ],
- 'public, max-age=1800, must-revalidate, s-max-age=3600'
- ],
- [
- [
- 'max-age' => 1800,
- 's-max-age' => 1800,
- 'public',
- 'must-revalidate',
- ],
- 'max-age=1800, s-max-age=1800, public, must-revalidate'
- ],
- [
- [
- 'private',
- 'no-cache',
- 'max-age' => 0,
- 'must-revalidate'
- ],
- 'private, no-cache, max-age=0, must-revalidate'
- ]
- ];
- }
- /**
- * Tests that `create_cache_control()` outputs the correct cache control
- * string from the supplied input
- *
- * @dataProvider provider_create_cache_control
- *
- * @param array $input input
- * @param string $expected expected
- * @return void
- */
- public function test_create_cache_control(array $input, $expected)
- {
- $this->assertSame($expected, HTTP_Header::create_cache_control($input));
- }
- /**
- * Data provider for parse_cache_control
- *
- * @return array
- */
- public function provider_parse_cache_control()
- {
- return [
- [
- 'public, max-age=1800, must-revalidate, s-max-age=3600',
- [
- 'public',
- 'max-age' => 1800,
- 'must-revalidate',
- 's-max-age' => 3600
- ]
- ],
- [
- 'max-age=1800, s-max-age=1800, public, must-revalidate',
- [
- 'max-age' => 1800,
- 's-max-age' => 1800,
- 'public',
- 'must-revalidate',
- ]
- ],
- [
- 'private, no-cache, max-age=0, must-revalidate',
- [
- 'private',
- 'no-cache',
- 'max-age' => 0,
- 'must-revalidate'
- ]
- ]
- ];
- }
- /**
- * Tests that `parse_cache_control()` outputs the correct cache control
- * parsed data from the input string
- *
- * @dataProvider provider_parse_cache_control
- *
- * @param string $input input
- * @param array $expected expected
- * @return void
- */
- public function test_parse_cache_control($input, array $expected)
- {
- $parsed = HTTP_Header::parse_cache_control($input);
- $this->assertInternalType('array', $parsed);
- foreach ($expected as $key => $value)
- {
- if (is_int($key))
- {
- $this->assertTrue(in_array($value, $parsed));
- }
- else
- {
- $this->assertTrue(array_key_exists($key, $parsed));
- $this->assertSame($value, $parsed[$key]);
- }
- }
- }
- /**
- * Data provider for test_offsetSet
- *
- * @return array
- */
- // @codingStandardsIgnoreStart
- public function provider_offsetSet()
- // @codingStandardsIgnoreEnd
- {
- return [
- [
- [
- 'Content-Type' => 'application/x-www-form-urlencoded',
- 'Accept' => 'text/html, text/plain; q=.1, */*',
- 'Accept-Language' => 'en-gb, en-us, en; q=.1'
- ],
- [
- [
- 'Accept-Encoding',
- 'compress, gzip',
- FALSE
- ]
- ],
- [
- 'content-type' => 'application/x-www-form-urlencoded',
- 'accept' => 'text/html, text/plain; q=.1, */*',
- 'accept-language' => 'en-gb, en-us, en; q=.1',
- 'accept-encoding' => 'compress, gzip'
- ]
- ],
- [
- [
- 'Content-Type' => 'application/x-www-form-urlencoded',
- 'Accept' => 'text/html, text/plain; q=.1, */*',
- 'Accept-Language' => 'en-gb, en-us, en; q=.1'
- ],
- [
- [
- 'Accept-Encoding',
- 'compress, gzip',
- FALSE
- ],
- [
- 'Accept-Encoding',
- 'bzip',
- FALSE
- ]
- ],
- [
- 'content-type' => 'application/x-www-form-urlencoded',
- 'accept' => 'text/html, text/plain; q=.1, */*',
- 'accept-language' => 'en-gb, en-us, en; q=.1',
- 'accept-encoding' => [
- 'compress, gzip',
- 'bzip'
- ]
- ]
- ],
- [
- [
- 'Content-Type' => 'application/x-www-form-urlencoded',
- 'Accept' => 'text/html, text/plain; q=.1, */*',
- 'Accept-Language' => 'en-gb, en-us, en; q=.1'
- ],
- [
- [
- 'Accept-Encoding',
- 'compress, gzip',
- FALSE
- ],
- [
- 'Accept-Encoding',
- 'bzip',
- TRUE
- ],
- [
- 'Accept',
- 'text/*',
- FALSE
- ]
- ],
- [
- 'content-type' => 'application/x-www-form-urlencoded',
- 'accept' => [
- 'text/html, text/plain; q=.1, */*',
- 'text/*'
- ],
- 'accept-language' => 'en-gb, en-us, en; q=.1',
- 'accept-encoding' => 'bzip'
- ]
- ],
- ];
- }
- /**
- * Ensures that offsetSet normalizes the array keys
- *
- * @dataProvider provider_offsetSet
- *
- * @param array $constructor constructor
- * @param array $to_set to_set
- * @param array $expected expected
- * @return void
- */
- // @codingStandardsIgnoreStart
- public function test_offsetSet(array $constructor, array $to_set, array $expected)
- // @codingStandardsIgnoreEnd
- {
- $http_header = new HTTP_Header($constructor);
- $reflection = new ReflectionClass($http_header);
- $method = $reflection->getMethod('offsetSet');
- foreach ($to_set as $args)
- {
- $method->invokeArgs($http_header, $args);
- }
- $this->assertSame($expected, $http_header->getArrayCopy());
- }
- /**
- * Data provider for test_offsetGet
- *
- * @return array
- */
- // @codingStandardsIgnoreStart
- public function provider_offsetGet()
- // @codingStandardsIgnoreEnd
- {
- return [
- [
- [
- 'FoO' => 'bar',
- 'START' => 'end',
- 'true' => TRUE
- ],
- 'FOO',
- 'bar'
- ],
- [
- [
- 'FoO' => 'bar',
- 'START' => 'end',
- 'true' => TRUE
- ],
- 'true',
- TRUE
- ],
- [
- [
- 'FoO' => 'bar',
- 'START' => 'end',
- 'true' => TRUE
- ],
- 'True',
- TRUE
- ],
- [
- [
- 'FoO' => 'bar',
- 'START' => 'end',
- 'true' => TRUE
- ],
- 'Start',
- 'end'
- ],
- [
- [
- 'content-type' => 'bar',
- 'Content-Type' => 'end',
- 'Accept' => '*/*'
- ],
- 'content-type',
- 'end'
- ]
- ];
- }
- /**
- * Ensures that offsetGet normalizes the array keys
- *
- * @dataProvider provider_offsetGet
- *
- * @param array start state
- * @param string key to retrieve
- * @param mixed expected
- * @return void
- */
- // @codingStandardsIgnoreStart
- public function test_offsetGet(array $state, $key, $expected)
- // @codingStandardsIgnoreEnd
- {
- $header = new HTTP_Header($state);
- $this->assertSame($expected, $header->offsetGet($key));
- }
- /**
- * Data provider for test_offsetExists
- *
- * @return array
- */
- // @codingStandardsIgnoreStart
- public function provider_offsetExists()
- // @codingStandardsIgnoreEnd
- {
- return [
- [
- [
- 'Accept' => 'text/html, application/json',
- 'Accept-Language' => 'en, en-GB',
- 'Content-Type' => 'application/x-www-form-urlencoded'
- ],
- 'Content-Type',
- TRUE
- ],
- [
- [
- 'Accept' => 'text/html, application/json',
- 'Accept-Language' => 'en, en-GB',
- 'Content-Type' => 'application/x-www-form-urlencoded'
- ],
- 'CONTENT-TYPE',
- TRUE
- ],
- [
- [
- 'Accept' => 'text/html, application/json',
- 'Accept-Language' => 'en, en-GB',
- 'Content-Type' => 'application/x-www-form-urlencoded'
- ],
- 'accept-language',
- TRUE
- ],
- [
- [
- 'Accept' => 'text/html, application/json',
- 'Accept-Language' => 'en, en-GB',
- 'Content-Type' => 'application/x-www-form-urlencoded'
- ],
- 'x-powered-by',
- FALSE
- ]
- ];
- }
- /**
- * Ensures that offsetExists normalizes the array key
- *
- * @dataProvider provider_offsetExists
- *
- * @param array $state state
- * @param string $key key
- * @param boolean $expected expected
- * @return void
- */
- // @codingStandardsIgnoreStart
- public function test_offsetExists(array $state, $key, $expected)
- // @codingStandardsIgnoreEnd
- {
- $header = new HTTP_Header($state);
- $this->assertSame($expected, $header->offsetExists($key));
- }
- /**
- * Data provider for test_offsetUnset
- *
- * @return array
- */
- // @codingStandardsIgnoreStart
- public function provider_offsetUnset()
- // @codingStandardsIgnoreEnd
- {
- return [
- [
- [
- 'Accept' => 'text/html, application/json',
- 'Accept-Language' => 'en, en-GB',
- 'Content-Type' => 'application/x-www-form-urlencoded'
- ],
- 'Accept-Language',
- [
- 'accept' => 'text/html, application/json',
- 'content-type' => 'application/x-www-form-urlencoded'
- ]
- ],
- [
- [
- 'Accept' => 'text/html, application/json',
- 'Accept-Language' => 'en, en-GB',
- 'Content-Type' => 'application/x-www-form-urlencoded'
- ],
- 'ACCEPT',
- [
- 'accept-language' => 'en, en-GB',
- 'content-type' => 'application/x-www-form-urlencoded'
- ]
- ],
- [
- [
- 'Accept' => 'text/html, application/json',
- 'Accept-Language' => 'en, en-GB',
- 'Content-Type' => 'application/x-www-form-urlencoded'
- ],
- 'content-type',
- [
- 'accept' => 'text/html, application/json',
- 'accept-language' => 'en, en-GB',
- ]
- ]
- ];
- }
- /**
- * Tests that `offsetUnset` normalizes the key names properly
- *
- * @dataProvider provider_offsetUnset
- *
- * @param array $state state
- * @param string $remove remove
- * @param array $expected expected
- * @return void
- */
- // @codingStandardsIgnoreStart
- public function test_offsetUnset(array $state, $remove, array $expected)
- // @codingStandardsIgnoreEnd
- {
- $header = new HTTP_Header($state);
- $header->offsetUnset($remove);
- $this->assertSame($expected, $header->getArrayCopy());
- }
- /**
- * Provides data for test_parse_header_string
- *
- * @return array
- */
- public function provider_parse_header_string()
- {
- return [
- [
- [
- "Content-Type: application/x-www-form-urlencoded\r\n",
- "Accept: text/html, text/plain; q=.5, application/json, */* \r\n",
- "X-Powered-By: Kohana Baby \r\n"
- ],
- [
- 'content-type' => 'application/x-www-form-urlencoded',
- 'accept' => 'text/html, text/plain; q=.5, application/json, */* ',
- 'x-powered-by' => 'Kohana Baby '
- ]
- ],
- [
- [
- "Content-Type: application/x-www-form-urlencoded\r\n",
- "Accept: text/html, text/plain; q=.5, application/json, */* \r\n",
- "X-Powered-By: Kohana Baby \r\n",
- "Content-Type: application/json\r\n"
- ],
- [
- 'content-type' => [
- 'application/x-www-form-urlencoded',
- 'application/json'
- ],
- 'accept' => 'text/html, text/plain; q=.5, application/json, */* ',
- 'x-powered-by' => 'Kohana Baby '
- ]
- ]
- ];
- }
- /**
- * Tests that `parse_header_string` performs as expected
- *
- * @dataProvider provider_parse_header_string
- *
- * @param array headers
- * @param array expected
- * @return void
- */
- public function test_parse_header_string(array $headers, array $expected)
- {
- $http_header = new HTTP_Header([]);
- foreach ($headers as $header)
- {
-
- $this->assertEquals(strlen($header), $http_header->parse_header_string(NULL, $header));
- }
- $this->assertSame($expected, $http_header->getArrayCopy());
- }
- /**
- * Data Provider for test_accepts_at_quality
- *
- * @return array
- */
- public function provider_accepts_at_quality()
- {
- return [
- [
- [
- 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
- ],
- 'application/json',
- FALSE,
- 1.0
- ],
- [
- [
- 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
- ],
- 'text/html',
- FALSE,
- 0.5
- ],
- [
- [
- 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
- ],
- 'text/plain',
- FALSE,
- 0.1
- ],
- [
- [
- 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
- ],
- 'text/plain',
- TRUE,
- FALSE
- ],
- [
- [
- 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
- ],
- 'application/xml',
- FALSE,
- 1.0
- ],
- [
- [
- 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
- ],
- 'application/xml',
- TRUE,
- FALSE
- ],
- [
- [],
- 'application/xml',
- FALSE,
- 1.0
- ],
- [
- [],
- 'application/xml',
- TRUE,
- FALSE
- ]
- ];
- }
- /**
- * Tests `accepts_at_quality` parsed the Accept: header as expected
- *
- * @dataProvider provider_accepts_at_quality
- *
- * @param array starting state
- * @param string accept header to test
- * @param boolean explicitly check
- * @param mixed expected output
- * @return void
- */
- public function test_accepts_at_quality(array $state, $accept, $explicit, $expected)
- {
- $header = new HTTP_Header($state);
- $this->assertSame($expected, $header->accepts_at_quality($accept, $explicit));
- }
- /**
- * Data provider for test_preferred_accept
- *
- * @return array
- */
- public function provider_preferred_accept()
- {
- return [
- [
- [
- 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
- ],
- [
- 'text/html',
- 'application/json',
- 'text/plain'
- ],
- FALSE,
- 'application/json'
- ],
- [
- [
- 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
- ],
- [
- 'text/plain',
- 'application/xml',
- 'image/jpeg'
- ],
- FALSE,
- 'application/xml'
- ],
- [
- [
- 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1'
- ],
- [
- 'text/plain',
- 'application/xml',
- 'image/jpeg'
- ],
- FALSE,
- 'text/plain'
- ],
- [
- [
- 'Accept' => 'application/json, text/html; q=.5, text/*; q=.1, */*'
- ],
- [
- 'text/plain',
- 'application/xml',
- 'image/jpeg'
- ],
- TRUE,
- FALSE
- ],
-
- ];
- }
- /**
- * Tests `preferred_accept` returns the correct preferred type
- *
- * @dataProvider provider_preferred_accept
- *
- * @param array state
- * @param array accepts
- * @param string explicit
- * @param string expected
- * @return void
- */
- public function test_preferred_accept(array $state, array $accepts, $explicit, $expected)
- {
- $header = new HTTP_Header($state);
- $this->assertSame($expected, $header->preferred_accept($accepts, $explicit));
- }
- /**
- * Data provider for test_accepts_charset_at_quality
- *
- * @return array
- */
- public function provider_accepts_charset_at_quality()
- {
- return [
- [
- [
- 'Accept-Charset' => 'utf-8, utf-10, utf-16, iso-8859-1'
- ],
- 'utf-8',
- 1.0
- ],
- [
- [
- 'Accept-Charset' => 'utf-8, utf-10, utf-16, iso-8859-1'
- ],
- 'utf-16',
- 1.0
- ],
- [
- [
- 'Accept-Charset' => 'utf-8; q=.1, utf-10, utf-16; q=.2, iso-8859-1'
- ],
- 'utf-8',
- 0.1
- ],
- [
- [
- 'Accept-Charset' => 'utf-8; q=.1, utf-10, utf-16; q=.2, iso-8859-1; q=.5'
- ],
- 'iso-8859-1',
- 0.5
- ]
- ];
- }
- /**
- * Tests `accepts_charset_at_quality` works as expected, returning the correct
- * quality value
- *
- * @dataProvider provider_accepts_charset_at_quality
- *
- * @param array state
- * @param string charset
- * @param string expected
- * @return void
- */
- public function test_accepts_charset_at_quality(array $state, $charset, $expected)
- {
- $header = new HTTP_Header($state);
- $this->assertSame($expected, $header->accepts_charset_at_quality($charset));
- }
- /**
- * Data provider for test_preferred_charset
- *
- * @return array
- */
- public function provider_preferred_charset()
- {
- return [
- [
- [
- 'Accept-Charset' => 'utf-8, utf-10, utf-16, iso-8859-1'
- ],
- [
- 'utf-8',
- 'utf-16'
- ],
- 'utf-8'
- ],
- [
- [
- 'Accept-Charset' => 'utf-8, utf-10, utf-16, iso-8859-1'
- ],
- [
- 'UTF-10'
- ],
- 'UTF-10'
- ],
- ];
- }
- /**
- * Tests `preferred_charset` works as expected, returning the correct charset
- * from the list supplied
- *
- * @dataProvider provider_preferred_charset
- *
- * @param array state
- * @param array charsets
- * @param string expected
- * @return void
- */
- public function test_preferred_charset(array $state, array $charsets, $expected)
- {
- $header = new HTTP_Header($state);
- $this->assertSame($expected, $header->preferred_charset($charsets));
- }
- /**
- * Data provider for test_accepts_encoding_at_quality
- *
- * @return array
- */
- public function provider_accepts_encoding_at_quality()
- {
- return [
- [
- [
- 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
- ],
- 'gzip',
- FALSE,
- 1.0
- ],
- [
- [
- 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
- ],
- 'gzip',
- TRUE,
- 1.0
- ],
- [
- [
- 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
- ],
- 'blowfish',
- FALSE,
- 0.7
- ],
- [
- [
- 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
- ],
- 'bzip',
- FALSE,
- 0.5
- ],
- [
- [
- 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
- ],
- 'bzip',
- TRUE,
- (float) 0
- ]
- ];
- }
- /**
- * Tests `accepts_encoding_at_quality` parses and returns the correct
- * quality value for Accept-Encoding headers
- *
- * @dataProvider provider_accepts_encoding_at_quality
- *
- * @param array state
- * @param string encoding
- * @param boolean explicit
- * @param float expected
- * @return void
- */
- public function test_accepts_encoding_at_quality(array $state, $encoding, $explicit, $expected)
- {
- $header = new HTTP_Header($state);
- $this->assertSame($expected, $header->accepts_encoding_at_quality($encoding, $explicit));
- }
- /**
- * Data provider for test_preferred_encoding
- *
- * @return array
- */
- public function provider_preferred_encoding()
- {
- return [
- [
- [
- 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
- ],
- ['gzip', 'blowfish', 'bzip'],
- FALSE,
- 'gzip'
- ],
- [
- [
- 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
- ],
- ['bzip', 'ROT-13'],
- FALSE,
- 'bzip'
- ],
- [
- [
- 'accept-encoding' => 'compress, gzip, blowfish; q=.7, *; q=.5'
- ],
- ['bzip', 'ROT-13'],
- TRUE,
- FALSE
- ],
- [
- [
- 'accept-encoding' => 'compress, gzip, blowfish; q=.2, *; q=.5'
- ],
- ['ROT-13', 'blowfish'],
- FALSE,
- 'ROT-13'
- ],
- ];
- }
- /**
- * Tests that `preferred_encoding` parses and returns the correct
- * encoding type
- *
- * @dataProvider provider_preferred_encoding
- *
- * @param array state in
- * @param array encodings to interrogate
- * @param boolean explicit check
- * @param string expected output
- * @return void
- */
- public function test_preferred_encoding(array $state, array $encodings, $explicit, $expected)
- {
- $header = new HTTP_Header($state);
- $this->assertSame($expected, $header->preferred_encoding($encodings, $explicit));
- }
- /**
- * Data provider for test_accepts_language_at_quality
- *
- * @return array
- */
- public function provider_accepts_language_at_quality()
- {
- return [
- [
- [
- 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
- ],
- 'en',
- FALSE,
- 0.5
- ],
- [
- [
- 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
- ],
- 'en-gb',
- FALSE,
- 0.7
- ],
- [
- [
- 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
- ],
- 'en',
- TRUE,
- 0.5
- ],
- [
- [
- 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
- ],
- 'fr-ni',
- FALSE,
- 0.8
- ],
- [
- [
- 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
- ],
- 'fr-ni',
- TRUE,
- (float) 0
- ],
- [
- [
- 'accept-language' => 'en-US'
- ],
- 'en-us',
- TRUE,
- (float) 1
- ],
- ];
- }
- /**
- * Tests `accepts_language_at_quality` parses the Accept-Language header
- * correctly and identifies the correct quality supplied, explicit or not
- *
- * @dataProvider provider_accepts_language_at_quality
- *
- * @param array state in
- * @param string language to interrogate
- * @param boolean explicit check
- * @param float expected output
- * @return void
- */
- public function test_accepts_language_at_quality(array $state, $language, $explicit, $expected)
- {
- $header = new HTTP_Header($state);
- $this->assertSame($expected, $header->accepts_language_at_quality($language, $explicit));
- }
- /**
- * Data provider for test_preferred_language
- *
- * @return array
- */
- public function provider_preferred_language()
- {
- return [
- [
- [
- 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
- ],
- [
- 'en',
- 'fr',
- 'en-gb'
- ],
- FALSE,
- 'fr'
- ],
- [
- [
- 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
- ],
- [
- 'en',
- 'fr',
- 'en-gb'
- ],
- TRUE,
- 'fr'
- ],
- [
- [
- 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
- ],
- [
- 'en-au',
- 'fr-ni',
- 'fr'
- ],
- FALSE,
- 'fr-ni'
- ],
- [
- [
- 'accept-language' => 'en-us; q=.9, en-gb; q=.7, en; q=.5, fr-fr; q=.9, fr; q=.8'
- ],
- [
- 'en-au',
- 'fr-ni',
- 'fr'
- ],
- TRUE,
- 'fr'
- ],
- [
- [
- 'accept-language' => 'en-US'
- ],
- [
- 'en-us'
- ],
- TRUE,
- 'en-us'
- ],
- [
- [
- 'accept-language' => 'fr,en-US,en'
- ],
- [
- 'en-us',
- 'fr-fr'
- ],
- FALSE,
- 'fr-fr'
- ],
- [
- [
- 'accept-language' => 'fr,en-US,en'
- ],
- [
- 'fr-fr',
- 'en-us'
- ],
- FALSE,
- 'fr-fr'
- ],
- [
- [
- 'accept-language' => 'fr,en,en-US'
- ],
- [
- 'en-us',
- 'fr-fr'
- ],
- TRUE,
- 'en-us'
- ],
- [
- [
- 'accept-language' => 'fr,en,en-US'
- ],
- [
- 'fr-fr',
- 'en-us'
- ],
- TRUE,
- 'en-us'
- ],
- ];
- }
- /**
- * Tests that `preferred_language` correctly identifies the right
- * language based on the Accept-Language header and `$explicit` setting
- *
- * @dataProvider provider_preferred_language
- *
- * @param array state in
- * @param array languages to interrogate
- * @param boolean explicit check
- * @param string expected output
- * @return void
- */
- public function test_preferred_language(array $state, array $languages, $explicit, $expected)
- {
- $header = new HTTP_Header($state);
- $this->assertSame($expected, $header->preferred_language($languages, $explicit));
- }
- /**
- * Data provider for test_send_headers
- *
- * @return array
- */
- public function provider_send_headers()
- {
- $content_type = Kohana::$content_type.'; charset='.Kohana::$charset;
- return [
- [
- [],
- [
- 'HTTP/1.1 200 OK',
- 'Content-Type: '.$content_type,
- ],
- FALSE,
- ],
- [
- [],
- [
- 'HTTP/1.1 200 OK',
- 'Content-Type: '.$content_type,
- 'X-Powered-By: '.Kohana::version(),
- ],
- TRUE,
- ],
- [
- [
- 'accept' => 'text/html, text/plain, text/*, */*',
- 'accept-charset' => 'utf-8, utf-10, iso-8859-1',
- 'accept-encoding' => 'compress, gzip',
- 'accept-language' => 'en, en-gb, en-us'
- ],
- [
- 'HTTP/1.1 200 OK',
- 'Accept: text/html, text/plain, text/*, */*',
- 'Accept-Charset: utf-8, utf-10, iso-8859-1',
- 'Accept-Encoding: compress, gzip',
- 'Accept-Language: en, en-gb, en-us',
- 'Content-Type: '.$content_type,
- ],
- FALSE
- ],
- [
- [
- 'accept' => 'text/html, text/plain, text/*, */*',
- 'accept-charset' => 'utf-8, utf-10, iso-8859-1',
- 'accept-encoding' => 'compress, gzip',
- 'accept-language' => 'en, en-gb, en-us',
- 'content-type' => 'application/json',
- 'x-powered-by' => 'Mohana',
- 'x-ssl-enabled' => 'TRUE'
- ],
- [
- 'HTTP/1.1 200 OK',
- 'Accept: text/html, text/plain, text/*, */*',
- 'Accept-Charset: utf-8, utf-10, iso-8859-1',
- 'Accept-Encoding: compress, gzip',
- 'Accept-Language: en, en-gb, en-us',
- 'Content-Type: application/json',
- 'X-Powered-By: Mohana',
- 'X-Ssl-Enabled: TRUE'
- ],
- TRUE
- ]
- ];
- }
- /**
- * Tests that send headers processes the headers sent to PHP correctly
- *
- * @dataProvider provider_send_headers
- *
- * @param array state in
- * @param array expected out
- * @return void
- */
- public function test_send_headers(array $state, array $expected, $expose)
- {
- Kohana::$expose = $expose;
- $response = new Response;
- $response->headers($state);
- $this->assertSame(
- $expected,
- $response->send_headers(FALSE, [$this, 'send_headers_handler'])
- );
- }
- /**
- * Callback handler for send headers
- *
- * @param array headers
- * @param boolean replace
- * @return array
- */
- public function send_headers_handler($response, $headers, $replace)
- {
- return $headers;
- }
- } // End Kohana_HTTP_HeaderTest
|