123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536 |
- <?php
- /**
- * Unit Tests for KO7_HTTP_Header
- *
- * @group ko7
- * @group ko7.core
- * @group ko7.core.http
- * @group ko7.core.http.header
- * @group ko7.core.http.header
- *
- * @package KO7
- * @category Tests
- *
- * @copyright (c) 2007-2016 Kohana Team
- * @copyright (c) since 2016 Koseven Team
- * @license https://koseven.dev/LICENSE
- */
- class KO7_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->assertIsFloat($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->assertIsArray($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: KO7 Baby \r\n"
- ],
- [
- 'content-type' => 'application/x-www-form-urlencoded',
- 'accept' => 'text/html, text/plain; q=.5, application/json, */* ',
- 'x-powered-by' => 'KO7 Baby '
- ]
- ],
- [
- [
- "Content-Type: application/x-www-form-urlencoded\r\n",
- "Accept: text/html, text/plain; q=.5, application/json, */* \r\n",
- "X-Powered-By: KO7 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' => 'KO7 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 = KO7::$content_type.'; charset='.KO7::$charset;
- return [
- [
- [],
- [
- 'HTTP/1.1 200 OK',
- 'Content-Type: '.$content_type,
- ],
- FALSE,
- ],
- [
- [],
- [
- 'HTTP/1.1 200 OK',
- 'Content-Type: '.$content_type,
- 'X-Powered-By: '.KO7::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)
- {
- KO7::$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 KO7_HTTP_HeaderTest
|