ResponseTest.php 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Network;
  16. use Cake\Network\Exception\NotFoundException;
  17. use Cake\Network\Request;
  18. use Cake\Network\Response;
  19. use Cake\TestSuite\TestCase;
  20. use Zend\Diactoros\Stream;
  21. /**
  22. * ResponseTest
  23. */
  24. class ResponseTest extends TestCase
  25. {
  26. /**
  27. * Setup for tests
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. include_once __DIR__ . DS . 'mocks.php';
  35. }
  36. /**
  37. * Cleanup after tests
  38. *
  39. * @return void
  40. */
  41. public function tearDown()
  42. {
  43. parent::tearDown();
  44. }
  45. /**
  46. * Tests the request object constructor
  47. *
  48. * @return void
  49. */
  50. public function testConstruct()
  51. {
  52. $response = new Response();
  53. $this->assertNull($response->body());
  54. $this->assertEquals('UTF-8', $response->charset());
  55. $this->assertEquals('text/html', $response->type());
  56. $this->assertEquals('text/html; charset=UTF-8', $response->getHeaderLine('Content-Type'));
  57. $this->assertEquals(200, $response->statusCode());
  58. $options = [
  59. 'body' => 'This is the body',
  60. 'charset' => 'my-custom-charset',
  61. 'type' => 'mp3',
  62. 'status' => '203'
  63. ];
  64. $response = new Response($options);
  65. $this->assertEquals('This is the body', $response->body());
  66. $this->assertEquals('my-custom-charset', $response->charset());
  67. $this->assertEquals('audio/mpeg', $response->type());
  68. $this->assertEquals('audio/mpeg', $response->getHeaderLine('Content-Type'));
  69. $this->assertEquals(203, $response->statusCode());
  70. $options = [
  71. 'body' => 'This is the body',
  72. 'charset' => 'ISO-8859-1',
  73. 'type' => 'txt',
  74. 'status' => '422',
  75. 'statusCodes' => [
  76. 422 => 'Unprocessable Entity'
  77. ]
  78. ];
  79. $response = new Response($options);
  80. $this->assertEquals($options['body'], $response->body());
  81. $this->assertEquals($options['charset'], $response->charset());
  82. $this->assertEquals($response->getMimeType($options['type']), $response->type());
  83. $this->assertEquals($options['status'], $response->statusCode());
  84. $this->assertEquals('text/plain; charset=ISO-8859-1', $response->getHeaderLine('Content-Type'));
  85. }
  86. /**
  87. * Tests the body method
  88. *
  89. * @return void
  90. */
  91. public function testBody()
  92. {
  93. $response = new Response();
  94. $this->assertNull($response->body());
  95. $response->body('Response body');
  96. $this->assertEquals('Response body', $response->body());
  97. $this->assertEquals('Changed Body', $response->body('Changed Body'));
  98. $response = new Response();
  99. $response->body(0);
  100. $this->assertEquals(0, $response->body());
  101. $response = new Response();
  102. $response->body('0');
  103. $this->assertEquals('0', $response->body());
  104. $response = new Response();
  105. $response->body(null);
  106. $this->assertEquals(null, $response->body());
  107. }
  108. /**
  109. * Tests the charset method
  110. *
  111. * @return void
  112. */
  113. public function testCharset()
  114. {
  115. $response = new Response();
  116. $this->assertEquals('UTF-8', $response->charset());
  117. $response->charset('iso-8859-1');
  118. $this->assertEquals('iso-8859-1', $response->charset());
  119. $this->assertEquals('UTF-16', $response->charset('UTF-16'));
  120. }
  121. /**
  122. * Tests withCharset method
  123. *
  124. * @return void
  125. */
  126. public function testWithCharset()
  127. {
  128. $response = new Response();
  129. $this->assertEquals('text/html; charset=UTF-8', $response->getHeaderLine('Content-Type'));
  130. $new = $response->withCharset('iso-8859-1');
  131. $this->assertNotContains('iso', $response->getHeaderLine('Content-Type'), 'Old instance not changed');
  132. $this->assertEquals('text/html; charset=iso-8859-1', $new->getHeaderLine('Content-Type'));
  133. }
  134. /**
  135. * Tests the statusCode method
  136. *
  137. * @expectedException \InvalidArgumentException
  138. * @return void
  139. */
  140. public function testStatusCode()
  141. {
  142. $response = new Response();
  143. $this->assertEquals(200, $response->statusCode());
  144. $response->statusCode(404);
  145. $this->assertEquals(404, $response->statusCode());
  146. $this->assertEquals(500, $response->statusCode(500));
  147. //Throws exception
  148. $response->statusCode(1001);
  149. }
  150. /**
  151. * Tests the type method
  152. *
  153. * @return void
  154. */
  155. public function testType()
  156. {
  157. $response = new Response();
  158. $this->assertEquals('text/html', $response->type());
  159. $response->type('pdf');
  160. $this->assertEquals('application/pdf', $response->type());
  161. $this->assertEquals('application/crazy-mime', $response->type('application/crazy-mime'));
  162. $this->assertEquals('application/json', $response->type('json'));
  163. $this->assertEquals('text/vnd.wap.wml', $response->type('wap'));
  164. $this->assertEquals('application/vnd.wap.xhtml+xml', $response->type('xhtml-mobile'));
  165. $this->assertEquals('text/csv', $response->type('csv'));
  166. $response->type(['keynote' => 'application/keynote', 'bat' => 'application/bat']);
  167. $this->assertEquals('application/keynote', $response->type('keynote'));
  168. $this->assertEquals('application/bat', $response->type('bat'));
  169. $this->assertFalse($response->type('wackytype'));
  170. }
  171. /**
  172. * Tests the withType method
  173. *
  174. * @return void
  175. */
  176. public function testWithTypeAlias()
  177. {
  178. $response = new Response();
  179. $this->assertEquals(
  180. 'text/html; charset=UTF-8',
  181. $response->getHeaderLine('Content-Type'),
  182. 'Default content-type should match'
  183. );
  184. $new = $response->withType('pdf');
  185. $this->assertNotSame($new, $response, 'Should be a new instance');
  186. $this->assertSame(
  187. 'text/html; charset=UTF-8',
  188. $response->getHeaderLine('Content-Type'),
  189. 'Original object should not be modified'
  190. );
  191. $this->assertSame('application/pdf', $new->getHeaderLine('Content-Type'));
  192. $this->assertSame(
  193. 'application/json; charset=UTF-8',
  194. $new->withType('json')->getHeaderLine('Content-Type')
  195. );
  196. }
  197. /**
  198. * test withType() and full mime-types
  199. *
  200. * @return void
  201. */
  202. public function withTypeFull()
  203. {
  204. $response = new Response();
  205. $this->assertEquals(
  206. 'application/json',
  207. $response->withType('application/json')->getHeaderLine('Content-Type'),
  208. 'Should not add charset to explicit type'
  209. );
  210. $this->assertEquals(
  211. 'custom/stuff',
  212. $response->withType('custom/stuff')->getHeaderLine('Content-Type'),
  213. 'Should allow arbitrary types'
  214. );
  215. }
  216. /**
  217. * Test that an invalid type raises an exception
  218. *
  219. * @expectedException InvalidArgumentException
  220. * @expectedExceptionMessage "beans" is an invalid content type
  221. * @return void
  222. */
  223. public function testWithTypeInvalidType()
  224. {
  225. $response = new Response();
  226. $response->withType('beans');
  227. }
  228. /**
  229. * Tests the header method
  230. *
  231. * @return void
  232. */
  233. public function testHeader()
  234. {
  235. $response = new Response();
  236. $headers = [
  237. 'Content-Type' => 'text/html; charset=UTF-8',
  238. ];
  239. $this->assertEquals($headers, $response->header());
  240. $response->header('Location', 'http://example.com');
  241. $headers += ['Location' => 'http://example.com'];
  242. $this->assertEquals($headers, $response->header());
  243. // Headers with the same name are overwritten
  244. $response->header('Location', 'http://example2.com');
  245. $headers = [
  246. 'Content-Type' => 'text/html; charset=UTF-8',
  247. 'Location' => 'http://example2.com'
  248. ];
  249. $this->assertEquals($headers, $response->header());
  250. $response->header(['WWW-Authenticate' => 'Negotiate']);
  251. $headers += ['WWW-Authenticate' => 'Negotiate'];
  252. $this->assertEquals($headers, $response->header());
  253. $response->header(['WWW-Authenticate' => 'Not-Negotiate']);
  254. $headers['WWW-Authenticate'] = 'Not-Negotiate';
  255. $this->assertEquals($headers, $response->header());
  256. $response->header(['Age' => 12, 'Allow' => 'GET, HEAD']);
  257. $headers += ['Age' => 12, 'Allow' => 'GET, HEAD'];
  258. $this->assertEquals($headers, $response->header());
  259. // String headers are allowed
  260. $response->header('Content-Language: da');
  261. $headers += ['Content-Language' => 'da'];
  262. $this->assertEquals($headers, $response->header());
  263. $response->header('Content-Language: da');
  264. $headers += ['Content-Language' => 'da'];
  265. $this->assertEquals($headers, $response->header());
  266. $response->header(['Content-Encoding: gzip', 'Vary: *', 'Pragma' => 'no-cache']);
  267. $headers += ['Content-Encoding' => 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache'];
  268. $this->assertEquals($headers, $response->header());
  269. $response->header('Access-Control-Allow-Origin', ['domain1', 'domain2']);
  270. $headers += ['Access-Control-Allow-Origin' => ['domain1', 'domain2']];
  271. $this->assertEquals($headers, $response->header());
  272. }
  273. /**
  274. * Tests the send method
  275. *
  276. * @return void
  277. */
  278. public function testSend()
  279. {
  280. $response = $this->getMockBuilder('Cake\Network\Response')
  281. ->setMethods(['_sendHeader', '_sendContent', '_setCookies'])
  282. ->getMock();
  283. $response->header([
  284. 'Content-Language' => 'es',
  285. 'WWW-Authenticate' => 'Negotiate',
  286. 'Access-Control-Allow-Origin' => ['domain1', 'domain2'],
  287. ]);
  288. $response->body('the response body');
  289. $response->expects($this->once())->method('_sendContent')->with('the response body');
  290. $response->expects($this->at(0))->method('_setCookies');
  291. $response->expects($this->at(1))
  292. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  293. $response->expects($this->at(2))
  294. ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
  295. $response->expects($this->at(3))
  296. ->method('_sendHeader')->with('Content-Language', 'es');
  297. $response->expects($this->at(4))
  298. ->method('_sendHeader')->with('WWW-Authenticate', 'Negotiate');
  299. $response->expects($this->at(5))
  300. ->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain1');
  301. $response->expects($this->at(6))
  302. ->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain2');
  303. $response->send();
  304. }
  305. /**
  306. * Data provider for content type tests.
  307. *
  308. * @return array
  309. */
  310. public static function charsetTypeProvider()
  311. {
  312. return [
  313. ['mp3', 'audio/mpeg'],
  314. ['js', 'application/javascript; charset=UTF-8'],
  315. ['json', 'application/json; charset=UTF-8'],
  316. ['xml', 'application/xml; charset=UTF-8'],
  317. ['txt', 'text/plain; charset=UTF-8'],
  318. ];
  319. }
  320. /**
  321. * Tests the send method and changing the content type
  322. *
  323. * @dataProvider charsetTypeProvider
  324. * @return void
  325. */
  326. public function testSendChangingContentType($original, $expected)
  327. {
  328. $response = new Response();
  329. $response->type($original);
  330. $response->body('the response body');
  331. $this->assertEquals($expected, $response->getHeaderLine('Content-Type'));
  332. }
  333. /**
  334. * Tests the send method and changing the content type to JS without adding the charset
  335. *
  336. * @return void
  337. */
  338. public function testCharsetSetContentTypeWithoutCharset()
  339. {
  340. $response = new Response();
  341. $response->type('js');
  342. $response->charset('');
  343. $this->assertEquals('application/javascript', $response->getHeaderLine('Content-Type'));
  344. }
  345. /**
  346. * Tests the send method and changing the content type
  347. *
  348. * @return void
  349. */
  350. public function testLocationSetsStatus()
  351. {
  352. $response = new Response();
  353. $response->location('http://www.example.com');
  354. $this->assertEquals(302, $response->getStatusCode());
  355. }
  356. /**
  357. * Test that setting certain status codes clears the status code.
  358. *
  359. * @return void
  360. */
  361. public function testStatusClearsContentType()
  362. {
  363. $response = new Response();
  364. $response->statusCode(204);
  365. $response->statusCode(304);
  366. $this->assertFalse($response->hasHeader('Content-Type'));
  367. $this->assertSame(304, $response->getStatusCode());
  368. $response = new Response();
  369. $response->type('pdf');
  370. $response->statusCode(204);
  371. $this->assertFalse($response->hasHeader('Content-Type'));
  372. $this->assertSame(204, $response->getStatusCode());
  373. $response = new Response();
  374. $new = $response->withType('pdf')
  375. ->withStatus(204);
  376. $this->assertFalse($new->hasHeader('Content-Type'));
  377. $this->assertSame(204, $new->getStatusCode());
  378. $response = new Response();
  379. $new = $response->withStatus(304)
  380. ->withType('pdf');
  381. $this->assertFalse($new->hasHeader('Content-Type'));
  382. $response = new Response();
  383. $response->statusCode(204);
  384. $response->type('pdf');
  385. $this->assertFalse($response->hasHeader('Content-Type'));
  386. }
  387. /**
  388. * Tests the send method and changing the content type
  389. *
  390. * @return void
  391. */
  392. public function testSendWithCallableBody()
  393. {
  394. $response = $this->getMockBuilder('Cake\Network\Response')
  395. ->setMethods(['_sendHeader'])
  396. ->getMock();
  397. $response->body(function () {
  398. echo 'the response body';
  399. });
  400. ob_start();
  401. $response->send();
  402. $this->assertEquals('the response body', ob_get_clean());
  403. }
  404. /**
  405. * Tests that the returned a string from a body callable is also sent
  406. * as the response body
  407. *
  408. * @return void
  409. */
  410. public function testSendWithCallableBodyWithReturn()
  411. {
  412. $response = $this->getMockBuilder('Cake\Network\Response')
  413. ->setMethods(['_sendHeader'])
  414. ->getMock();
  415. $response->body(function () {
  416. return 'the response body';
  417. });
  418. ob_start();
  419. $response->send();
  420. $this->assertEquals('the response body', ob_get_clean());
  421. }
  422. /**
  423. * Tests the disableCache method
  424. *
  425. * @return void
  426. */
  427. public function testDisableCache()
  428. {
  429. $response = new Response();
  430. $expected = [
  431. 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  432. 'Last-Modified' => gmdate("D, d M Y H:i:s") . " GMT",
  433. 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
  434. 'Content-Type' => 'text/html; charset=UTF-8',
  435. ];
  436. $response->disableCache();
  437. $this->assertEquals($expected, $response->header());
  438. }
  439. /**
  440. * Tests the withDisabledCache method
  441. *
  442. * @return void
  443. */
  444. public function testWithDisabledCache()
  445. {
  446. $response = new Response();
  447. $expected = [
  448. 'Expires' => ['Mon, 26 Jul 1997 05:00:00 GMT'],
  449. 'Last-Modified' => [gmdate("D, d M Y H:i:s") . " GMT"],
  450. 'Cache-Control' => ['no-store, no-cache, must-revalidate, post-check=0, pre-check=0'],
  451. 'Content-Type' => ['text/html; charset=UTF-8'],
  452. ];
  453. $new = $response->withDisabledCache();
  454. $this->assertFalse($response->hasHeader('Expires'), 'Old instance not mutated.');
  455. $this->assertEquals($expected, $new->getHeaders());
  456. }
  457. /**
  458. * Tests the cache method
  459. *
  460. * @return void
  461. */
  462. public function testCache()
  463. {
  464. $response = new Response();
  465. $since = time();
  466. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  467. $response->expires('+1 day');
  468. $expected = [
  469. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  470. 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
  471. 'Expires' => $time->format('D, j M Y H:i:s') . ' GMT',
  472. 'Cache-Control' => 'public, max-age=' . ($time->format('U') - time()),
  473. 'Content-Type' => 'text/html; charset=UTF-8',
  474. ];
  475. $response->cache($since);
  476. $this->assertEquals($expected, $response->header());
  477. $response = new Response();
  478. $since = time();
  479. $time = '+5 day';
  480. $expected = [
  481. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  482. 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
  483. 'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
  484. 'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()),
  485. 'Content-Type' => 'text/html; charset=UTF-8',
  486. ];
  487. $response->cache($since, $time);
  488. $this->assertEquals($expected, $response->header());
  489. $response = new Response();
  490. $since = time();
  491. $time = time();
  492. $expected = [
  493. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  494. 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
  495. 'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
  496. 'Cache-Control' => 'public, max-age=0',
  497. 'Content-Type' => 'text/html; charset=UTF-8',
  498. ];
  499. $response->cache($since, $time);
  500. $this->assertEquals($expected, $response->header());
  501. }
  502. /**
  503. * Tests the withCache method
  504. *
  505. * @return void
  506. */
  507. public function testWithCache()
  508. {
  509. $response = new Response();
  510. $since = $time = time();
  511. $new = $response->withCache($since, $time);
  512. $this->assertFalse($response->hasHeader('Date'));
  513. $this->assertFalse($response->hasHeader('Last-Modified'));
  514. $this->assertEquals(gmdate("D, j M Y G:i:s ", $since) . 'GMT', $new->getHeaderLine('Date'));
  515. $this->assertEquals(gmdate("D, j M Y H:i:s ", $since) . 'GMT', $new->getHeaderLine('Last-Modified'));
  516. $this->assertEquals(gmdate("D, j M Y H:i:s", $time) . " GMT", $new->getHeaderLine('Expires'));
  517. $this->assertEquals('public, max-age=0', $new->getHeaderLine('Cache-Control'));
  518. }
  519. /**
  520. * Tests the compress method
  521. *
  522. * @return void
  523. */
  524. public function testCompress()
  525. {
  526. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement ob_gzhandler');
  527. $response = new Response();
  528. if (ini_get("zlib.output_compression") === '1' || !extension_loaded("zlib")) {
  529. $this->assertFalse($response->compress());
  530. $this->markTestSkipped('Is not possible to test output compression');
  531. }
  532. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  533. $result = $response->compress();
  534. $this->assertFalse($result);
  535. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  536. $result = $response->compress();
  537. $this->assertTrue($result);
  538. $this->assertTrue(in_array('ob_gzhandler', ob_list_handlers()));
  539. ob_get_clean();
  540. }
  541. /**
  542. * Tests the httpCodes method
  543. *
  544. * @expectedException \InvalidArgumentException
  545. * @return void
  546. */
  547. public function testHttpCodes()
  548. {
  549. $response = new Response();
  550. $result = $response->httpCodes();
  551. $this->assertEquals(65, count($result));
  552. $result = $response->httpCodes(100);
  553. $expected = [100 => 'Continue'];
  554. $this->assertEquals($expected, $result);
  555. $codes = [
  556. 381 => 'Unicorn Moved',
  557. 555 => 'Unexpected Minotaur'
  558. ];
  559. $result = $response->httpCodes($codes);
  560. $this->assertTrue($result);
  561. $this->assertEquals(67, count($response->httpCodes()));
  562. $result = $response->httpCodes(381);
  563. $expected = [381 => 'Unicorn Moved'];
  564. $this->assertEquals($expected, $result);
  565. $codes = [404 => 'Sorry Bro'];
  566. $result = $response->httpCodes($codes);
  567. $this->assertTrue($result);
  568. $this->assertEquals(67, count($response->httpCodes()));
  569. $result = $response->httpCodes(404);
  570. $expected = [404 => 'Sorry Bro'];
  571. $this->assertEquals($expected, $result);
  572. //Throws exception
  573. $response->httpCodes([
  574. 0 => 'Nothing Here',
  575. -1 => 'Reverse Infinity',
  576. 12345 => 'Universal Password',
  577. 'Hello' => 'World'
  578. ]);
  579. }
  580. /**
  581. * Tests the download method
  582. *
  583. * @return void
  584. */
  585. public function testDownload()
  586. {
  587. $response = new Response();
  588. $expected = [
  589. 'Content-Type' => 'text/html; charset=UTF-8',
  590. 'Content-Disposition' => 'attachment; filename="myfile.mp3"'
  591. ];
  592. $response->download('myfile.mp3');
  593. $this->assertEquals($expected, $response->header());
  594. }
  595. /**
  596. * Tests the withDownload method
  597. *
  598. * @return void
  599. */
  600. public function testWithDownload()
  601. {
  602. $response = new Response();
  603. $new = $response->withDownload('myfile.mp3');
  604. $this->assertFalse($response->hasHeader('Content-Disposition'), 'No mutation');
  605. $expected = 'attachment; filename="myfile.mp3"';
  606. $this->assertEquals($expected, $new->getHeaderLine('Content-Disposition'));
  607. }
  608. /**
  609. * Tests the mapType method
  610. *
  611. * @return void
  612. */
  613. public function testMapType()
  614. {
  615. $response = new Response();
  616. $this->assertEquals('wav', $response->mapType('audio/x-wav'));
  617. $this->assertEquals('pdf', $response->mapType('application/pdf'));
  618. $this->assertEquals('xml', $response->mapType('text/xml'));
  619. $this->assertEquals('html', $response->mapType('*/*'));
  620. $this->assertEquals('csv', $response->mapType('application/vnd.ms-excel'));
  621. $expected = ['json', 'xhtml', 'css'];
  622. $result = $response->mapType(['application/json', 'application/xhtml+xml', 'text/css']);
  623. $this->assertEquals($expected, $result);
  624. }
  625. /**
  626. * Tests the outputCompressed method
  627. *
  628. * @return void
  629. */
  630. public function testOutputCompressed()
  631. {
  632. $response = new Response();
  633. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  634. $result = $response->outputCompressed();
  635. $this->assertFalse($result);
  636. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  637. $result = $response->outputCompressed();
  638. $this->assertFalse($result);
  639. if (!extension_loaded("zlib")) {
  640. $this->markTestSkipped('Skipping further tests for outputCompressed as zlib extension is not loaded');
  641. }
  642. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement ob_gzhandler');
  643. if (ini_get("zlib.output_compression") !== '1') {
  644. ob_start('ob_gzhandler');
  645. }
  646. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  647. $result = $response->outputCompressed();
  648. $this->assertTrue($result);
  649. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  650. $result = $response->outputCompressed();
  651. $this->assertFalse($result);
  652. if (ini_get("zlib.output_compression") !== '1') {
  653. ob_get_clean();
  654. }
  655. }
  656. /**
  657. * Tests getting/setting the protocol
  658. *
  659. * @return void
  660. */
  661. public function testProtocol()
  662. {
  663. $response = $this->getMockBuilder('Cake\Network\Response')
  664. ->setMethods(['_sendHeader', '_sendContent'])
  665. ->getMock();
  666. $response->protocol('HTTP/1.0');
  667. $this->assertEquals('HTTP/1.0', $response->protocol());
  668. $response->expects($this->at(0))
  669. ->method('_sendHeader')->with('HTTP/1.0 200 OK');
  670. $response->send();
  671. }
  672. /**
  673. * Tests getting/setting the Content-Length
  674. *
  675. * @return void
  676. */
  677. public function testLength()
  678. {
  679. $response = new Response();
  680. $response->length(100);
  681. $this->assertEquals(100, $response->length());
  682. $this->assertEquals('100', $response->getHeaderLine('Content-Length'));
  683. }
  684. /**
  685. * Tests settings the content length
  686. *
  687. * @return void
  688. */
  689. public function testWithLength()
  690. {
  691. $response = new Response();
  692. $this->assertFalse($response->hasHeader('Content-Length'));
  693. $new = $response->withLength(100);
  694. $this->assertFalse($response->hasHeader('Content-Length'), 'Old instance not modified');
  695. $this->assertSame('100', $new->getHeaderLine('Content-Length'));
  696. $this->assertSame('100', $new->length(), 'new method is compat with old.');
  697. }
  698. /**
  699. * Tests setting the expiration date
  700. *
  701. * @return void
  702. */
  703. public function testExpires()
  704. {
  705. $format = 'D, j M Y H:i:s';
  706. $response = new Response();
  707. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  708. $response->expires($now);
  709. $now->setTimeZone(new \DateTimeZone('UTC'));
  710. $this->assertEquals($now->format($format) . ' GMT', $response->expires());
  711. $this->assertEquals($now->format($format) . ' GMT', $response->getHeaderLine('Expires'));
  712. $now = time();
  713. $response = new Response();
  714. $response->expires($now);
  715. $this->assertEquals(gmdate($format) . ' GMT', $response->expires());
  716. $this->assertEquals(gmdate($format) . ' GMT', $response->getHeaderLine('Expires'));
  717. $response = new Response();
  718. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  719. $response->expires('+1 day');
  720. $this->assertEquals($time->format($format) . ' GMT', $response->expires());
  721. $this->assertEquals($time->format($format) . ' GMT', $response->getHeaderLine('Expires'));
  722. }
  723. /**
  724. * Tests the withExpires method
  725. *
  726. * @return void
  727. */
  728. public function testWithExpires()
  729. {
  730. $format = 'D, j M Y H:i:s';
  731. $response = new Response();
  732. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  733. $new = $response->withExpires($now);
  734. $this->assertFalse($response->hasHeader('Expires'));
  735. $now->setTimeZone(new \DateTimeZone('UTC'));
  736. $this->assertEquals($now->format($format) . ' GMT', $new->getHeaderLine('Expires'));
  737. $now = time();
  738. $new = $response->withExpires($now);
  739. $this->assertEquals(gmdate($format) . ' GMT', $new->getHeaderLine('Expires'));
  740. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  741. $new = $response->withExpires('+1 day');
  742. $this->assertEquals($time->format($format) . ' GMT', $new->getHeaderLine('Expires'));
  743. }
  744. /**
  745. * Tests setting the modification date
  746. *
  747. * @return void
  748. */
  749. public function testModified()
  750. {
  751. $format = 'D, j M Y H:i:s';
  752. $response = new Response();
  753. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  754. $response->modified($now);
  755. $now->setTimeZone(new \DateTimeZone('UTC'));
  756. $this->assertEquals($now->format($format) . ' GMT', $response->modified());
  757. $this->assertEquals($now->format($format) . ' GMT', $response->getHeaderLine('Last-Modified'));
  758. $response = new Response();
  759. $now = time();
  760. $response->modified($now);
  761. $this->assertEquals(gmdate($format) . ' GMT', $response->modified());
  762. $this->assertEquals(gmdate($format) . ' GMT', $response->getHeaderLine('Last-Modified'));
  763. $response = new Response();
  764. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  765. $response->modified('+1 day');
  766. $this->assertEquals($time->format($format) . ' GMT', $response->modified());
  767. $this->assertEquals($time->format($format) . ' GMT', $response->getHeaderLine('Last-Modified'));
  768. }
  769. /**
  770. * Tests the withModified method
  771. *
  772. * @return void
  773. */
  774. public function testWithModified()
  775. {
  776. $format = 'D, j M Y H:i:s';
  777. $response = new Response();
  778. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  779. $new = $response->withModified($now);
  780. $this->assertFalse($response->hasHeader('Last-Modified'));
  781. $now->setTimeZone(new \DateTimeZone('UTC'));
  782. $this->assertEquals($now->format($format) . ' GMT', $new->getHeaderLine('Last-Modified'));
  783. $now = time();
  784. $new = $response->withModified($now);
  785. $this->assertEquals(gmdate($format) . ' GMT', $new->getHeaderLine('Last-Modified'));
  786. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  787. $new = $response->withModified('+1 day');
  788. $this->assertEquals($time->format($format) . ' GMT', $new->getHeaderLine('Last-Modified'));
  789. }
  790. /**
  791. * Tests setting of public/private Cache-Control directives
  792. *
  793. * @return void
  794. */
  795. public function testSharable()
  796. {
  797. $response = new Response();
  798. $this->assertNull($response->sharable());
  799. $response->sharable(true);
  800. $this->assertTrue($response->sharable());
  801. $this->assertEquals('public', $response->getHeaderLine('Cache-Control'));
  802. $response = new Response();
  803. $response->sharable(false);
  804. $this->assertFalse($response->sharable());
  805. $this->assertEquals('private', $response->getHeaderLine('Cache-Control'));
  806. $response = new Response();
  807. $response->sharable(true, 3600);
  808. $this->assertEquals('public, max-age=3600', $response->getHeaderLine('Cache-Control'));
  809. $response = new Response();
  810. $response->sharable(false, 3600);
  811. $this->assertEquals('private, max-age=3600', $response->getHeaderLine('Cache-Control'));
  812. }
  813. /**
  814. * Tests withSharable()
  815. *
  816. * @return void
  817. */
  818. public function testWithSharable()
  819. {
  820. $response = new Response();
  821. $new = $response->withSharable(true);
  822. $this->assertFalse($response->hasHeader('Cache-Control'), 'old instance unchanged');
  823. $this->assertEquals('public', $new->getHeaderLine('Cache-Control'));
  824. $new = $response->withSharable(false);
  825. $this->assertEquals('private', $new->getHeaderLine('Cache-Control'));
  826. $new = $response->withSharable(true, 3600);
  827. $this->assertEquals('public, max-age=3600', $new->getHeaderLine('Cache-Control'));
  828. $new = $response->withSharable(false, 3600);
  829. $this->assertEquals('private, max-age=3600', $new->getHeaderLine('Cache-Control'));
  830. }
  831. /**
  832. * Tests setting of max-age Cache-Control directive
  833. *
  834. * @return void
  835. */
  836. public function testMaxAge()
  837. {
  838. $response = new Response();
  839. $this->assertNull($response->maxAge());
  840. $response->maxAge(3600);
  841. $this->assertEquals(3600, $response->maxAge());
  842. $this->assertEquals('max-age=3600', $response->getHeaderLine('Cache-Control'));
  843. $response = new Response();
  844. $response->maxAge(3600);
  845. $response->sharable(false);
  846. $this->assertEquals('max-age=3600, private', $response->getHeaderLine('Cache-Control'));
  847. }
  848. /**
  849. * Tests withMaxAge()
  850. *
  851. * @return void
  852. */
  853. public function testWithMaxAge()
  854. {
  855. $response = new Response();
  856. $this->assertFalse($response->hasHeader('Cache-Control'));
  857. $new = $response->withMaxAge(3600);
  858. $this->assertEquals('max-age=3600', $new->getHeaderLine('Cache-Control'));
  859. $new = $response->withMaxAge(3600)
  860. ->withSharable(false);
  861. $this->assertEquals('max-age=3600, private', $new->getHeaderLine('Cache-Control'));
  862. }
  863. /**
  864. * Tests setting of s-maxage Cache-Control directive
  865. *
  866. * @return void
  867. */
  868. public function testSharedMaxAge()
  869. {
  870. $response = new Response();
  871. $this->assertNull($response->maxAge());
  872. $response->sharedMaxAge(3600);
  873. $this->assertEquals(3600, $response->sharedMaxAge());
  874. $this->assertEquals('s-maxage=3600', $response->getHeaderLine('Cache-Control'));
  875. $response = new Response();
  876. $response->sharedMaxAge(3600);
  877. $response->sharable(true);
  878. $this->assertEquals('s-maxage=3600, public', $response->getHeaderLine('Cache-Control'));
  879. }
  880. /**
  881. * Tests setting of s-maxage Cache-Control directive
  882. *
  883. * @return void
  884. */
  885. public function testWithSharedMaxAge()
  886. {
  887. $response = new Response();
  888. $new = $response->withSharedMaxAge(3600);
  889. $this->assertFalse($response->hasHeader('Cache-Control'));
  890. $this->assertEquals('s-maxage=3600', $new->getHeaderLine('Cache-Control'));
  891. $new = $response->withSharedMaxAge(3600)->withSharable(true);
  892. $this->assertEquals('s-maxage=3600, public', $new->getHeaderLine('Cache-Control'));
  893. }
  894. /**
  895. * Tests setting of must-revalidate Cache-Control directive
  896. *
  897. * @return void
  898. */
  899. public function testMustRevalidate()
  900. {
  901. $response = new Response();
  902. $this->assertFalse($response->mustRevalidate());
  903. $response->mustRevalidate(true);
  904. $this->assertTrue($response->mustRevalidate());
  905. $this->assertEquals('must-revalidate', $response->getHeaderLine('Cache-Control'));
  906. $response->mustRevalidate(false);
  907. $this->assertFalse($response->mustRevalidate());
  908. $response = new Response();
  909. $response->sharedMaxAge(3600);
  910. $response->mustRevalidate(true);
  911. $this->assertEquals('s-maxage=3600, must-revalidate', $response->getHeaderLine('Cache-Control'));
  912. }
  913. /**
  914. * Tests setting of must-revalidate Cache-Control directive
  915. *
  916. * @return void
  917. */
  918. public function testWithMustRevalidate()
  919. {
  920. $response = new Response();
  921. $this->assertFalse($response->hasHeader('Cache-Control'));
  922. $new = $response->withMustRevalidate(true);
  923. $this->assertFalse($response->hasHeader('Cache-Control'));
  924. $this->assertEquals('must-revalidate', $new->getHeaderLine('Cache-Control'));
  925. $new = $new->withMustRevalidate(false);
  926. $this->assertEmpty($new->getHeaderLine('Cache-Control'));
  927. }
  928. /**
  929. * Tests getting/setting the Vary header
  930. *
  931. * @return void
  932. */
  933. public function testVary()
  934. {
  935. $response = new Response();
  936. $response->vary('Accept-encoding');
  937. $this->assertEquals('Accept-encoding', $response->getHeaderLine('vary'));
  938. $response = new Response();
  939. $response->vary(['Accept-language', 'Accept-encoding']);
  940. $this->assertEquals(['Accept-language', 'Accept-encoding'], $response->vary());
  941. $this->assertEquals('Accept-language, Accept-encoding', $response->getHeaderLine('vary'));
  942. }
  943. /**
  944. * Tests withVary()
  945. *
  946. * @return void
  947. */
  948. public function testWithVary()
  949. {
  950. $response = new Response();
  951. $new = $response->withVary('Accept-encoding');
  952. $this->assertFalse($response->hasHeader('Vary'));
  953. $this->assertEquals('Accept-encoding', $new->getHeaderLine('Vary'));
  954. $new = $response->withVary(['Accept-encoding', 'Accept-language']);
  955. $this->assertFalse($response->hasHeader('Vary'));
  956. $this->assertEquals('Accept-encoding,Accept-language', $new->getHeaderLine('Vary'));
  957. }
  958. /**
  959. * Tests getting/setting the Etag header
  960. *
  961. * @return void
  962. */
  963. public function testEtag()
  964. {
  965. $response = new Response();
  966. $response->etag('something');
  967. $this->assertEquals('"something"', $response->etag());
  968. $this->assertEquals('"something"', $response->getHeaderLine('Etag'));
  969. $response = new Response();
  970. $response->etag('something', true);
  971. $this->assertEquals('W/"something"', $response->etag());
  972. $this->assertEquals('W/"something"', $response->getHeaderLine('Etag'));
  973. }
  974. /**
  975. * Tests withEtag()
  976. *
  977. * @return void
  978. */
  979. public function testWithEtag()
  980. {
  981. $response = new Response();
  982. $new = $response->withEtag('something');
  983. $this->assertFalse($response->hasHeader('Etag'));
  984. $this->assertEquals('"something"', $new->getHeaderLine('Etag'));
  985. $new = $response->withEtag('something', true);
  986. $this->assertEquals('W/"something"', $new->getHeaderLine('Etag'));
  987. }
  988. /**
  989. * Tests that the response is able to be marked as not modified
  990. *
  991. * @return void
  992. */
  993. public function testNotModified()
  994. {
  995. $response = $this->getMockBuilder('Cake\Network\Response')
  996. ->setMethods(['_sendHeader', '_sendContent'])
  997. ->getMock();
  998. $response->body('something');
  999. $response->statusCode(200);
  1000. $response->length(100);
  1001. $response->modified('now');
  1002. $response->notModified();
  1003. $this->assertEmpty($response->header());
  1004. $this->assertEmpty($response->body());
  1005. $this->assertEquals(304, $response->statusCode());
  1006. }
  1007. /**
  1008. * Tests withNotModified()
  1009. *
  1010. * @return void
  1011. */
  1012. public function testWithNotModified()
  1013. {
  1014. $response = new Response(['body' => 'something']);
  1015. $response = $response->withLength(100)
  1016. ->withStatus(200)
  1017. ->withHeader('Last-Modified', 'value')
  1018. ->withHeader('Content-Language', 'en-EN')
  1019. ->withHeader('X-things', 'things')
  1020. ->withType('application/json');
  1021. $new = $response->withNotModified();
  1022. $this->assertTrue($response->hasHeader('Content-Language'), 'old instance not changed');
  1023. $this->assertTrue($response->hasHeader('Content-Length'), 'old instance not changed');
  1024. $this->assertFalse($new->hasHeader('Content-Type'));
  1025. $this->assertFalse($new->hasHeader('Content-Length'));
  1026. $this->assertFalse($new->hasHeader('Content-Language'));
  1027. $this->assertFalse($new->hasHeader('Last-Modified'));
  1028. $this->assertSame('things', $new->getHeaderLine('X-things'), 'Other headers are retained');
  1029. $this->assertSame(304, $new->getStatusCode());
  1030. $this->assertSame('', $new->getBody()->getContents());
  1031. }
  1032. /**
  1033. * Test checkNotModified method
  1034. *
  1035. * @return void
  1036. */
  1037. public function testCheckNotModifiedByEtagStar()
  1038. {
  1039. $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
  1040. $response = $this->getMockBuilder('Cake\Network\Response')
  1041. ->setMethods(['notModified'])
  1042. ->getMock();
  1043. $response->etag('something');
  1044. $response->expects($this->once())->method('notModified');
  1045. $response->checkNotModified(new Request);
  1046. }
  1047. /**
  1048. * Test checkNotModified method
  1049. *
  1050. * @return void
  1051. */
  1052. public function testCheckNotModifiedByEtagExact()
  1053. {
  1054. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  1055. $response = $this->getMockBuilder('Cake\Network\Response')
  1056. ->setMethods(['notModified'])
  1057. ->getMock();
  1058. $response->etag('something', true);
  1059. $response->expects($this->once())->method('notModified');
  1060. $this->assertTrue($response->checkNotModified(new Request));
  1061. }
  1062. /**
  1063. * Test checkNotModified method
  1064. *
  1065. * @return void
  1066. */
  1067. public function testCheckNotModifiedByEtagAndTime()
  1068. {
  1069. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  1070. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  1071. $response = $this->getMockBuilder('Cake\Network\Response')
  1072. ->setMethods(['notModified'])
  1073. ->getMock();
  1074. $response->etag('something', true);
  1075. $response->modified('2012-01-01 00:00:00');
  1076. $response->expects($this->once())->method('notModified');
  1077. $this->assertTrue($response->checkNotModified(new Request));
  1078. }
  1079. /**
  1080. * Test checkNotModified method
  1081. *
  1082. * @return void
  1083. */
  1084. public function testCheckNotModifiedByEtagAndTimeMismatch()
  1085. {
  1086. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  1087. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  1088. $response = $this->getMockBuilder('Cake\Network\Response')
  1089. ->setMethods(['notModified'])
  1090. ->getMock();
  1091. $response->etag('something', true);
  1092. $response->modified('2012-01-01 00:00:01');
  1093. $response->expects($this->never())->method('notModified');
  1094. $this->assertFalse($response->checkNotModified(new Request));
  1095. }
  1096. /**
  1097. * Test checkNotModified method
  1098. *
  1099. * @return void
  1100. */
  1101. public function testCheckNotModifiedByEtagMismatch()
  1102. {
  1103. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something-else", "other"';
  1104. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  1105. $response = $this->getMockBuilder('Cake\Network\Response')
  1106. ->setMethods(['notModified'])
  1107. ->getMock();
  1108. $response->etag('something', true);
  1109. $response->modified('2012-01-01 00:00:00');
  1110. $response->expects($this->never())->method('notModified');
  1111. $this->assertFalse($response->checkNotModified(new Request));
  1112. }
  1113. /**
  1114. * Test checkNotModified method
  1115. *
  1116. * @return void
  1117. */
  1118. public function testCheckNotModifiedByTime()
  1119. {
  1120. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  1121. $response = $this->getMockBuilder('Cake\Network\Response')
  1122. ->setMethods(['notModified'])
  1123. ->getMock();
  1124. $response->modified('2012-01-01 00:00:00');
  1125. $response->expects($this->once())->method('notModified');
  1126. $this->assertTrue($response->checkNotModified(new Request));
  1127. }
  1128. /**
  1129. * Test checkNotModified method
  1130. *
  1131. * @return void
  1132. */
  1133. public function testCheckNotModifiedNoHints()
  1134. {
  1135. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  1136. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  1137. $response = $this->getMockBuilder('Cake\Network\Response')
  1138. ->setMethods(['notModified'])
  1139. ->getMock();
  1140. $response->expects($this->never())->method('notModified');
  1141. $this->assertFalse($response->checkNotModified(new Request));
  1142. }
  1143. /**
  1144. * Test cookie setting
  1145. *
  1146. * @return void
  1147. */
  1148. public function testCookieSettings()
  1149. {
  1150. $response = new Response();
  1151. $cookie = [
  1152. 'name' => 'CakeTestCookie[Testing]'
  1153. ];
  1154. $response->cookie($cookie);
  1155. $expected = [
  1156. 'name' => 'CakeTestCookie[Testing]',
  1157. 'value' => '',
  1158. 'expire' => 0,
  1159. 'path' => '/',
  1160. 'domain' => '',
  1161. 'secure' => false,
  1162. 'httpOnly' => false];
  1163. $result = $response->cookie('CakeTestCookie[Testing]');
  1164. $this->assertEquals($expected, $result);
  1165. $cookie = [
  1166. 'name' => 'CakeTestCookie[Testing2]',
  1167. 'value' => '[a,b,c]',
  1168. 'expire' => 1000,
  1169. 'path' => '/test',
  1170. 'secure' => true
  1171. ];
  1172. $response->cookie($cookie);
  1173. $expected = [
  1174. 'CakeTestCookie[Testing]' => [
  1175. 'name' => 'CakeTestCookie[Testing]',
  1176. 'value' => '',
  1177. 'expire' => 0,
  1178. 'path' => '/',
  1179. 'domain' => '',
  1180. 'secure' => false,
  1181. 'httpOnly' => false
  1182. ],
  1183. 'CakeTestCookie[Testing2]' => [
  1184. 'name' => 'CakeTestCookie[Testing2]',
  1185. 'value' => '[a,b,c]',
  1186. 'expire' => 1000,
  1187. 'path' => '/test',
  1188. 'domain' => '',
  1189. 'secure' => true,
  1190. 'httpOnly' => false
  1191. ]
  1192. ];
  1193. $result = $response->cookie();
  1194. $this->assertEquals($expected, $result);
  1195. $cookie = $expected['CakeTestCookie[Testing]'];
  1196. $cookie['value'] = 'test';
  1197. $response->cookie($cookie);
  1198. $expected = [
  1199. 'CakeTestCookie[Testing]' => [
  1200. 'name' => 'CakeTestCookie[Testing]',
  1201. 'value' => 'test',
  1202. 'expire' => 0,
  1203. 'path' => '/',
  1204. 'domain' => '',
  1205. 'secure' => false,
  1206. 'httpOnly' => false
  1207. ],
  1208. 'CakeTestCookie[Testing2]' => [
  1209. 'name' => 'CakeTestCookie[Testing2]',
  1210. 'value' => '[a,b,c]',
  1211. 'expire' => 1000,
  1212. 'path' => '/test',
  1213. 'domain' => '',
  1214. 'secure' => true,
  1215. 'httpOnly' => false
  1216. ]
  1217. ];
  1218. $result = $response->cookie();
  1219. $this->assertEquals($expected, $result);
  1220. }
  1221. /**
  1222. * Test setting cookies with no value
  1223. *
  1224. * @return void
  1225. */
  1226. public function testWithCookieEmpty()
  1227. {
  1228. $response = new Response();
  1229. $new = $response->withCookie('testing');
  1230. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  1231. $expected = [
  1232. 'name' => 'testing',
  1233. 'value' => '',
  1234. 'expire' => 0,
  1235. 'path' => '/',
  1236. 'domain' => '',
  1237. 'secure' => false,
  1238. 'httpOnly' => false];
  1239. $result = $new->getCookie('testing');
  1240. $this->assertEquals($expected, $result);
  1241. }
  1242. /**
  1243. * Test setting cookies with scalar values
  1244. *
  1245. * @return void
  1246. */
  1247. public function testWithCookieScalar()
  1248. {
  1249. $response = new Response();
  1250. $new = $response->withCookie('testing', 'abc123');
  1251. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  1252. $this->assertEquals('abc123', $new->getCookie('testing')['value']);
  1253. $new = $response->withCookie('testing', 99);
  1254. $this->assertEquals(99, $new->getCookie('testing')['value']);
  1255. $new = $response->withCookie('testing', false);
  1256. $this->assertFalse($new->getCookie('testing')['value']);
  1257. $new = $response->withCookie('testing', true);
  1258. $this->assertTrue($new->getCookie('testing')['value']);
  1259. }
  1260. /**
  1261. * Test withCookie() and array data.
  1262. *
  1263. * @return void
  1264. */
  1265. public function testWithCookieArray()
  1266. {
  1267. $response = new Response();
  1268. $cookie = [
  1269. 'name' => 'ignored key',
  1270. 'value' => '[a,b,c]',
  1271. 'expire' => 1000,
  1272. 'path' => '/test',
  1273. 'secure' => true
  1274. ];
  1275. $new = $response->withCookie('testing', $cookie);
  1276. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  1277. $expected = [
  1278. 'name' => 'testing',
  1279. 'value' => '[a,b,c]',
  1280. 'expire' => 1000,
  1281. 'path' => '/test',
  1282. 'domain' => '',
  1283. 'secure' => true,
  1284. 'httpOnly' => false
  1285. ];
  1286. $this->assertEquals($expected, $new->getCookie('testing'));
  1287. }
  1288. /**
  1289. * Test CORS
  1290. *
  1291. * @dataProvider corsData
  1292. * @param Request $request
  1293. * @param string $origin
  1294. * @param string|array $domains
  1295. * @param string|array $methods
  1296. * @param string|array $headers
  1297. * @param string|bool $expectedOrigin
  1298. * @param string|bool $expectedMethods
  1299. * @param string|bool $expectedHeaders
  1300. * @return void
  1301. */
  1302. public function testCors($request, $origin, $domains, $methods, $headers, $expectedOrigin, $expectedMethods = false, $expectedHeaders = false)
  1303. {
  1304. $request->env('HTTP_ORIGIN', $origin);
  1305. $response = new Response();
  1306. $result = $response->cors($request, $domains, $methods, $headers);
  1307. $this->assertInstanceOf('Cake\Network\CorsBuilder', $result);
  1308. $headers = $response->header();
  1309. if ($expectedOrigin) {
  1310. $this->assertArrayHasKey('Access-Control-Allow-Origin', $headers);
  1311. $this->assertEquals($expectedOrigin, $headers['Access-Control-Allow-Origin']);
  1312. }
  1313. if ($expectedMethods) {
  1314. $this->assertArrayHasKey('Access-Control-Allow-Methods', $headers);
  1315. $this->assertEquals($expectedMethods, $headers['Access-Control-Allow-Methods']);
  1316. }
  1317. if ($expectedHeaders) {
  1318. $this->assertArrayHasKey('Access-Control-Allow-Headers', $headers);
  1319. $this->assertEquals($expectedHeaders, $headers['Access-Control-Allow-Headers']);
  1320. }
  1321. unset($_SERVER['HTTP_ORIGIN']);
  1322. }
  1323. /**
  1324. * Feed for testCors
  1325. *
  1326. * @return array
  1327. */
  1328. public function corsData()
  1329. {
  1330. $fooRequest = new Request();
  1331. $secureRequest = function () {
  1332. $secureRequest = $this->getMockBuilder('Cake\Network\Request')
  1333. ->setMethods(['is'])
  1334. ->getMock();
  1335. $secureRequest->expects($this->any())
  1336. ->method('is')
  1337. ->with('ssl')
  1338. ->will($this->returnValue(true));
  1339. return $secureRequest;
  1340. };
  1341. return [
  1342. [$fooRequest, null, '*', '', '', false, false],
  1343. [$fooRequest, 'http://www.foo.com', '*', '', '', '*', false],
  1344. [$fooRequest, 'http://www.foo.com', 'www.foo.com', '', '', 'http://www.foo.com', false],
  1345. [$fooRequest, 'http://www.foo.com', '*.foo.com', '', '', 'http://www.foo.com', false],
  1346. [$fooRequest, 'http://www.foo.com', 'http://*.foo.com', '', '', 'http://www.foo.com', false],
  1347. [$fooRequest, 'http://www.foo.com', 'https://www.foo.com', '', '', false, false],
  1348. [$fooRequest, 'http://www.foo.com', 'https://*.foo.com', '', '', false, false],
  1349. [$fooRequest, 'http://www.foo.com', ['*.bar.com', '*.foo.com'], '', '', 'http://www.foo.com', false],
  1350. [$fooRequest, 'http://not-foo.com', '*.foo.com', '', '', false, false],
  1351. [$fooRequest, 'http://bad.academy', '*.acad.my', '', '', false, false],
  1352. [$fooRequest, 'http://www.foo.com.at.bad.com', '*.foo.com', '', '', false, false],
  1353. [$fooRequest, 'https://www.foo.com', '*.foo.com', '', '', false, false],
  1354. [$secureRequest(), 'https://www.bar.com', 'www.bar.com', '', '', 'https://www.bar.com', false],
  1355. [$secureRequest(), 'https://www.bar.com', 'http://www.bar.com', '', '', false, false],
  1356. [$secureRequest(), 'https://www.bar.com', '*.bar.com', '', '', 'https://www.bar.com', false],
  1357. [$secureRequest(), 'http://www.bar.com', '*.bar.com', '', '', false, false],
  1358. [$fooRequest, 'http://www.foo.com', '*', 'GET', '', '*', 'GET'],
  1359. [$fooRequest, 'http://www.foo.com', '*.foo.com', 'GET', '', 'http://www.foo.com', 'GET'],
  1360. [$fooRequest, 'http://www.foo.com', '*.foo.com', ['GET', 'POST'], '', 'http://www.foo.com', 'GET, POST'],
  1361. [$fooRequest, 'http://www.foo.com', '*', '', 'X-CakePHP', '*', false, 'X-CakePHP'],
  1362. [$fooRequest, 'http://www.foo.com', '*', '', ['X-CakePHP', 'X-MyApp'], '*', false, 'X-CakePHP, X-MyApp'],
  1363. [$fooRequest, 'http://www.foo.com', '*', ['GET', 'OPTIONS'], ['X-CakePHP', 'X-MyApp'], '*', 'GET, OPTIONS', 'X-CakePHP, X-MyApp'],
  1364. ];
  1365. }
  1366. /**
  1367. * testFileNotFound
  1368. *
  1369. * @expectedException \Cake\Network\Exception\NotFoundException
  1370. * @return void
  1371. */
  1372. public function testFileNotFound()
  1373. {
  1374. $response = new Response();
  1375. $response->file('/some/missing/folder/file.jpg');
  1376. }
  1377. /**
  1378. * test withFile() not found
  1379. *
  1380. * @expectedException \Cake\Network\Exception\NotFoundException
  1381. * @return void
  1382. */
  1383. public function testWithFileNotFound()
  1384. {
  1385. $response = new Response();
  1386. $response->withFile('/some/missing/folder/file.jpg');
  1387. }
  1388. /**
  1389. * Provider for various kinds of unacceptable files.
  1390. *
  1391. * @return array
  1392. */
  1393. public function invalidFileProvider()
  1394. {
  1395. return [
  1396. ['my/../cat.gif', 'The requested file contains `..` and will not be read.'],
  1397. ['my\..\cat.gif', 'The requested file contains `..` and will not be read.'],
  1398. ['my/ca..t.gif', 'my/ca..t.gif was not found or not readable'],
  1399. ['my/ca..t/image.gif', 'my/ca..t/image.gif was not found or not readable'],
  1400. ];
  1401. }
  1402. /**
  1403. * test invalid file paths.
  1404. *
  1405. * @dataProvider invalidFileProvider
  1406. * @return void
  1407. */
  1408. public function testFileInvalidPath($path, $expectedMessage)
  1409. {
  1410. $response = new Response();
  1411. try {
  1412. $response->file($path);
  1413. } catch (NotFoundException $e) {
  1414. $this->assertContains($expectedMessage, $e->getMessage());
  1415. }
  1416. }
  1417. /**
  1418. * test withFile and invalid paths
  1419. *
  1420. * @dataProvider invalidFileProvider
  1421. * @return void
  1422. */
  1423. public function testWithFileInvalidPath($path, $expectedMessage)
  1424. {
  1425. $response = new Response();
  1426. try {
  1427. $response->withFile($path);
  1428. } catch (NotFoundException $e) {
  1429. $this->assertContains($expectedMessage, $e->getMessage());
  1430. }
  1431. }
  1432. /**
  1433. * testFile method
  1434. *
  1435. * @return void
  1436. */
  1437. public function testFile()
  1438. {
  1439. $response = $this->getMockBuilder('Cake\Network\Response')
  1440. ->setMethods([
  1441. 'header',
  1442. 'type',
  1443. '_sendHeader',
  1444. '_setContentType',
  1445. '_isActive',
  1446. ])
  1447. ->getMock();
  1448. $response->expects($this->exactly(1))
  1449. ->method('type')
  1450. ->with('css')
  1451. ->will($this->returnArgument(0));
  1452. $response->expects($this->at(1))
  1453. ->method('header')
  1454. ->with('Accept-Ranges', 'bytes');
  1455. $response->expects($this->exactly(1))
  1456. ->method('_isActive')
  1457. ->will($this->returnValue(true));
  1458. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1459. ob_start();
  1460. $result = $response->send();
  1461. $this->assertTrue($result !== false);
  1462. $output = ob_get_clean();
  1463. $expected = "/* this is the test asset css file */";
  1464. $this->assertEquals($expected, trim($output));
  1465. $this->assertEquals($expected, trim($response->getBody()->getContents()));
  1466. }
  1467. /**
  1468. * test withFile() + download & name
  1469. *
  1470. * @return void
  1471. */
  1472. public function testWithFileDownloadAndName()
  1473. {
  1474. $response = new Response();
  1475. $new = $response->withFile(
  1476. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1477. [
  1478. 'name' => 'something_special.css',
  1479. 'download' => true,
  1480. ]
  1481. );
  1482. $this->assertEquals(
  1483. 'text/html; charset=UTF-8',
  1484. $response->getHeaderLine('Content-Type'),
  1485. 'No mutation'
  1486. );
  1487. $this->assertEquals(
  1488. 'text/css; charset=UTF-8',
  1489. $new->getHeaderLine('Content-Type')
  1490. );
  1491. $this->assertEquals(
  1492. 'attachment; filename="something_special.css"',
  1493. $new->getHeaderLine('Content-Disposition')
  1494. );
  1495. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  1496. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  1497. $body = $new->getBody();
  1498. $this->assertInstanceOf('Zend\Diactoros\Stream', $body);
  1499. $expected = "/* this is the test asset css file */";
  1500. $this->assertEquals($expected, trim($body->getContents()));
  1501. $this->assertEquals($expected, trim($new->getFile()->read()));
  1502. }
  1503. /**
  1504. * testFileWithDownloadAndName
  1505. *
  1506. * @return void
  1507. */
  1508. public function testFileWithDownloadAndName()
  1509. {
  1510. $response = $this->getMockBuilder('Cake\Network\Response')
  1511. ->setMethods([
  1512. 'header',
  1513. 'type',
  1514. 'download',
  1515. '_sendHeader',
  1516. '_setContentType',
  1517. '_isActive',
  1518. ])
  1519. ->getMock();
  1520. $response->expects($this->exactly(1))
  1521. ->method('type')
  1522. ->with('css')
  1523. ->will($this->returnArgument(0));
  1524. $response->expects($this->once())
  1525. ->method('download')
  1526. ->with('something_special.css');
  1527. $response->expects($this->at(2))
  1528. ->method('header')
  1529. ->with('Content-Transfer-Encoding', 'binary');
  1530. $response->expects($this->at(3))
  1531. ->method('header')
  1532. ->with('Accept-Ranges', 'bytes');
  1533. $response->expects($this->exactly(1))
  1534. ->method('_isActive')
  1535. ->will($this->returnValue(true));
  1536. $response->file(
  1537. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1538. [
  1539. 'name' => 'something_special.css',
  1540. 'download' => true,
  1541. ]
  1542. );
  1543. ob_start();
  1544. $result = $response->send();
  1545. $output = ob_get_clean();
  1546. $this->assertEquals("/* this is the test asset css file */\n", $output);
  1547. $this->assertNotSame(false, $result);
  1548. }
  1549. /**
  1550. * testFileWithUnknownFileTypeGeneric method
  1551. *
  1552. * @return void
  1553. */
  1554. public function testFileWithUnknownFileTypeGeneric()
  1555. {
  1556. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1557. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1558. $response = $this->getMockBuilder('Cake\Network\Response')
  1559. ->setMethods([
  1560. 'header',
  1561. 'type',
  1562. 'download',
  1563. '_sendHeader',
  1564. '_setContentType',
  1565. '_isActive',
  1566. ])
  1567. ->getMock();
  1568. $response->expects($this->exactly(1))
  1569. ->method('type')
  1570. ->with('ini')
  1571. ->will($this->returnValue(false));
  1572. $response->expects($this->once())
  1573. ->method('download')
  1574. ->with('no_section.ini');
  1575. $response->expects($this->at(2))
  1576. ->method('header')
  1577. ->with('Content-Transfer-Encoding', 'binary');
  1578. $response->expects($this->at(3))
  1579. ->method('header')
  1580. ->with('Accept-Ranges', 'bytes');
  1581. $response->expects($this->exactly(1))
  1582. ->method('_isActive')
  1583. ->will($this->returnValue(true));
  1584. $response->file(CONFIG . 'no_section.ini');
  1585. ob_start();
  1586. $result = $response->send();
  1587. $output = ob_get_clean();
  1588. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1589. $this->assertNotSame(false, $result);
  1590. if ($currentUserAgent !== null) {
  1591. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1592. }
  1593. }
  1594. /**
  1595. * test withFile() + a generic agent
  1596. *
  1597. * @return void
  1598. */
  1599. public function testWithFileUnknownFileTypeGeneric()
  1600. {
  1601. $response = new Response();
  1602. $new = $response->withFile(CONFIG . 'no_section.ini');
  1603. $this->assertEquals('text/html; charset=UTF-8', $new->getHeaderLine('Content-Type'));
  1604. $this->assertEquals(
  1605. 'attachment; filename="no_section.ini"',
  1606. $new->getHeaderLine('Content-Disposition')
  1607. );
  1608. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  1609. $body = $new->getBody();
  1610. $expected = "some_key = some_value\nbool_key = 1\n";
  1611. $this->assertEquals($expected, $body->getContents());
  1612. }
  1613. /**
  1614. * testFileWithUnknownFileTypeOpera method
  1615. *
  1616. * @return void
  1617. */
  1618. public function testFileWithUnknownFileTypeOpera()
  1619. {
  1620. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1621. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  1622. $response = $this->getMockBuilder('Cake\Network\Response')
  1623. ->setMethods([
  1624. 'header',
  1625. 'type',
  1626. 'download',
  1627. '_sendHeader',
  1628. '_setContentType',
  1629. '_isActive',
  1630. ])
  1631. ->getMock();
  1632. $response->expects($this->at(0))
  1633. ->method('type')
  1634. ->with('ini')
  1635. ->will($this->returnValue(false));
  1636. $response->expects($this->at(1))
  1637. ->method('type')
  1638. ->with('application/octet-stream')
  1639. ->will($this->returnValue(false));
  1640. $response->expects($this->once())
  1641. ->method('download')
  1642. ->with('no_section.ini');
  1643. $response->expects($this->at(3))
  1644. ->method('header')
  1645. ->with('Content-Transfer-Encoding', 'binary');
  1646. $response->expects($this->at(4))
  1647. ->method('header')
  1648. ->with('Accept-Ranges', 'bytes');
  1649. $response->expects($this->exactly(1))
  1650. ->method('_isActive')
  1651. ->will($this->returnValue(true));
  1652. $response->file(CONFIG . 'no_section.ini');
  1653. ob_start();
  1654. $result = $response->send();
  1655. $output = ob_get_clean();
  1656. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1657. $this->assertNotSame(false, $result);
  1658. if ($currentUserAgent !== null) {
  1659. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1660. }
  1661. }
  1662. /**
  1663. * test withFile() + opera
  1664. *
  1665. * @return void
  1666. */
  1667. public function testWithFileUnknownFileTypeOpera()
  1668. {
  1669. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1670. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  1671. $response = new Response();
  1672. $new = $response->withFile(CONFIG . 'no_section.ini');
  1673. $this->assertEquals('application/octet-stream', $new->getHeaderLine('Content-Type'));
  1674. $this->assertEquals(
  1675. 'attachment; filename="no_section.ini"',
  1676. $new->getHeaderLine('Content-Disposition')
  1677. );
  1678. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1679. }
  1680. /**
  1681. * testFileWithUnknownFileTypeIE method
  1682. *
  1683. * @return void
  1684. */
  1685. public function testFileWithUnknownFileTypeIE()
  1686. {
  1687. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1688. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Media Center PC 4.0; SLCC1; .NET CLR 3.0.04320)';
  1689. $response = $this->getMockBuilder('Cake\Network\Response')
  1690. ->setMethods([
  1691. 'header',
  1692. 'type',
  1693. 'download',
  1694. '_sendHeader',
  1695. '_setContentType',
  1696. '_isActive',
  1697. ])
  1698. ->getMock();
  1699. $response->expects($this->at(0))
  1700. ->method('type')
  1701. ->with('ini')
  1702. ->will($this->returnValue(false));
  1703. $response->expects($this->at(1))
  1704. ->method('type')
  1705. ->with('application/force-download')
  1706. ->will($this->returnValue(false));
  1707. $response->expects($this->once())
  1708. ->method('download')
  1709. ->with('config.ini');
  1710. $response->expects($this->at(3))
  1711. ->method('header')
  1712. ->with('Content-Transfer-Encoding', 'binary');
  1713. $response->expects($this->at(4))
  1714. ->method('header')
  1715. ->with('Accept-Ranges', 'bytes');
  1716. $response->expects($this->exactly(1))
  1717. ->method('_isActive')
  1718. ->will($this->returnValue(true));
  1719. $response->file(CONFIG . 'no_section.ini', [
  1720. 'name' => 'config.ini'
  1721. ]);
  1722. ob_start();
  1723. $result = $response->send();
  1724. $output = ob_get_clean();
  1725. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1726. $this->assertNotSame(false, $result);
  1727. if ($currentUserAgent !== null) {
  1728. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1729. }
  1730. }
  1731. /**
  1732. * test withFile() + old IE
  1733. *
  1734. * @return void
  1735. */
  1736. public function testWithFileUnknownFileTypeOldIe()
  1737. {
  1738. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1739. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Media Center PC 4.0; SLCC1; .NET CLR 3.0.04320)';
  1740. $response = new Response();
  1741. $new = $response->withFile(CONFIG . 'no_section.ini');
  1742. $this->assertEquals('application/force-download', $new->getHeaderLine('Content-Type'));
  1743. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1744. }
  1745. /**
  1746. * testFileWithUnknownFileNoDownload method
  1747. *
  1748. * @return void
  1749. */
  1750. public function testFileWithUnknownFileNoDownload()
  1751. {
  1752. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1753. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1754. $response = $this->getMockBuilder('Cake\Network\Response')
  1755. ->setMethods([
  1756. 'header',
  1757. 'type',
  1758. 'download',
  1759. '_sendHeader',
  1760. '_setContentType',
  1761. '_isActive',
  1762. ])
  1763. ->getMock();
  1764. $response->expects($this->exactly(1))
  1765. ->method('type')
  1766. ->with('ini')
  1767. ->will($this->returnValue(false));
  1768. $response->expects($this->at(1))
  1769. ->method('header')
  1770. ->with('Accept-Ranges', 'bytes');
  1771. $response->expects($this->never())
  1772. ->method('download');
  1773. $response->file(CONFIG . 'no_section.ini', [
  1774. 'download' => false
  1775. ]);
  1776. if ($currentUserAgent !== null) {
  1777. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1778. }
  1779. }
  1780. /**
  1781. * test withFile() + no download
  1782. *
  1783. * @return void
  1784. */
  1785. public function testWithFileNoDownload()
  1786. {
  1787. $response = new Response();
  1788. $new = $response->withFile(CONFIG . 'no_section.ini', [
  1789. 'download' => false
  1790. ]);
  1791. $this->assertEquals(
  1792. 'text/html; charset=UTF-8',
  1793. $new->getHeaderLine('Content-Type')
  1794. );
  1795. $this->assertFalse($new->hasHeader('Content-Disposition'));
  1796. $this->assertFalse($new->hasHeader('Content-Transfer-Encoding'));
  1797. }
  1798. /**
  1799. * test getFile method
  1800. *
  1801. * @return void
  1802. */
  1803. public function testGetFile()
  1804. {
  1805. $response = new Response();
  1806. $this->assertNull($response->getFile(), 'No file to get');
  1807. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1808. $file = $response->getFile();
  1809. $this->assertInstanceOf('Cake\Filesystem\File', $file, 'Should get a file');
  1810. $this->assertPathEquals(TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', $file->path);
  1811. }
  1812. /**
  1813. * testConnectionAbortedOnBuffering method
  1814. *
  1815. * @return void
  1816. */
  1817. public function testConnectionAbortedOnBuffering()
  1818. {
  1819. $response = $this->getMockBuilder('Cake\Network\Response')
  1820. ->setMethods([
  1821. 'header',
  1822. 'type',
  1823. 'download',
  1824. '_sendHeader',
  1825. '_setContentType',
  1826. '_isActive',
  1827. ])
  1828. ->getMock();
  1829. $response->expects($this->any())
  1830. ->method('type')
  1831. ->with('css')
  1832. ->will($this->returnArgument(0));
  1833. $response->expects($this->at(0))
  1834. ->method('_isActive')
  1835. ->will($this->returnValue(false));
  1836. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1837. $result = $response->send();
  1838. $this->assertNull($result);
  1839. }
  1840. /**
  1841. * Test downloading files with UPPERCASE extensions.
  1842. *
  1843. * @return void
  1844. */
  1845. public function testFileUpperExtension()
  1846. {
  1847. $response = $this->getMockBuilder('Cake\Network\Response')
  1848. ->setMethods([
  1849. 'header',
  1850. 'type',
  1851. 'download',
  1852. '_sendHeader',
  1853. '_setContentType',
  1854. '_isActive',
  1855. ])
  1856. ->getMock();
  1857. $response->expects($this->any())
  1858. ->method('type')
  1859. ->with('jpg')
  1860. ->will($this->returnArgument(0));
  1861. $response->expects($this->at(0))
  1862. ->method('_isActive')
  1863. ->will($this->returnValue(true));
  1864. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1865. }
  1866. /**
  1867. * Test that uppercase extensions result in correct content-types
  1868. *
  1869. * @return void
  1870. */
  1871. public function testWithFileUpperExtension()
  1872. {
  1873. $response = new Response();
  1874. $new = $response->withFile(TEST_APP . 'vendor/img/test_2.JPG');
  1875. $this->assertEquals('image/jpeg', $new->getHeaderLine('Content-Type'));
  1876. }
  1877. /**
  1878. * Test downloading files with extension not explicitly set.
  1879. *
  1880. * @return void
  1881. */
  1882. public function testFileExtensionNotSet()
  1883. {
  1884. $response = $this->getMockBuilder('Cake\Network\Response')
  1885. ->setMethods([
  1886. 'header',
  1887. 'type',
  1888. 'download',
  1889. '_sendHeader',
  1890. '_setContentType',
  1891. '_isActive',
  1892. ])
  1893. ->getMock();
  1894. $response->expects($this->any())
  1895. ->method('type')
  1896. ->with('jpg')
  1897. ->will($this->returnArgument(0));
  1898. $response->expects($this->at(0))
  1899. ->method('_isActive')
  1900. ->will($this->returnValue(true));
  1901. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1902. }
  1903. /**
  1904. * A data provider for testing various ranges
  1905. *
  1906. * @return array
  1907. */
  1908. public static function rangeProvider()
  1909. {
  1910. return [
  1911. // suffix-byte-range
  1912. [
  1913. 'bytes=-25', 25, 'bytes 13-37/38'
  1914. ],
  1915. [
  1916. 'bytes=0-', 38, 'bytes 0-37/38'
  1917. ],
  1918. [
  1919. 'bytes=10-', 28, 'bytes 10-37/38'
  1920. ],
  1921. [
  1922. 'bytes=10-20', 11, 'bytes 10-20/38'
  1923. ],
  1924. // Spaced out
  1925. [
  1926. 'bytes = 10 - 20', 11, 'bytes 10-20/38'
  1927. ],
  1928. ];
  1929. }
  1930. /**
  1931. * Test the various range offset types.
  1932. *
  1933. * @dataProvider rangeProvider
  1934. * @return void
  1935. */
  1936. public function testFileRangeOffsets($range, $length, $offsetResponse)
  1937. {
  1938. $_SERVER['HTTP_RANGE'] = $range;
  1939. $response = $this->getMockBuilder('Cake\Network\Response')
  1940. ->setMethods([
  1941. 'header',
  1942. 'type',
  1943. '_sendHeader',
  1944. '_setContentType',
  1945. '_isActive',
  1946. ])
  1947. ->getMock();
  1948. $response->expects($this->at(1))
  1949. ->method('header')
  1950. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1951. $response->expects($this->at(2))
  1952. ->method('header')
  1953. ->with('Content-Transfer-Encoding', 'binary');
  1954. $response->expects($this->at(3))
  1955. ->method('header')
  1956. ->with('Accept-Ranges', 'bytes');
  1957. $response->expects($this->at(4))
  1958. ->method('header')
  1959. ->with([
  1960. 'Content-Length' => $length,
  1961. 'Content-Range' => $offsetResponse,
  1962. ]);
  1963. $response->expects($this->any())
  1964. ->method('_isActive')
  1965. ->will($this->returnValue(true));
  1966. $response->file(
  1967. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1968. ['download' => true]
  1969. );
  1970. ob_start();
  1971. $result = $response->send();
  1972. ob_get_clean();
  1973. }
  1974. /**
  1975. * Test withFile() & the various range offset types.
  1976. *
  1977. * @dataProvider rangeProvider
  1978. * @return void
  1979. */
  1980. public function testWithFileRangeOffsets($range, $length, $offsetResponse)
  1981. {
  1982. $_SERVER['HTTP_RANGE'] = $range;
  1983. $response = new Response();
  1984. $new = $response->withFile(
  1985. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1986. ['download' => true]
  1987. );
  1988. $this->assertEquals(
  1989. 'attachment; filename="test_asset.css"',
  1990. $new->getHeaderLine('Content-Disposition')
  1991. );
  1992. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  1993. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  1994. $this->assertEquals($length, $new->getHeaderLine('Content-Length'));
  1995. $this->assertEquals($offsetResponse, $new->getHeaderLine('Content-Range'));
  1996. }
  1997. /**
  1998. * Test fetching ranges from a file.
  1999. *
  2000. * @return void
  2001. */
  2002. public function testFileRange()
  2003. {
  2004. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  2005. $response = $this->getMockBuilder('Cake\Network\Response')
  2006. ->setMethods([
  2007. 'header',
  2008. 'type',
  2009. '_sendHeader',
  2010. '_setContentType',
  2011. '_isActive',
  2012. ])
  2013. ->getMock();
  2014. $response->expects($this->exactly(1))
  2015. ->method('type')
  2016. ->with('css')
  2017. ->will($this->returnArgument(0));
  2018. $response->expects($this->at(1))
  2019. ->method('header')
  2020. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  2021. $response->expects($this->at(2))
  2022. ->method('header')
  2023. ->with('Content-Transfer-Encoding', 'binary');
  2024. $response->expects($this->at(3))
  2025. ->method('header')
  2026. ->with('Accept-Ranges', 'bytes');
  2027. $response->expects($this->at(4))
  2028. ->method('header')
  2029. ->with([
  2030. 'Content-Length' => 18,
  2031. 'Content-Range' => 'bytes 8-25/38',
  2032. ]);
  2033. $response->expects($this->any())
  2034. ->method('_isActive')
  2035. ->will($this->returnValue(true));
  2036. $response->file(
  2037. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2038. ['download' => true]
  2039. );
  2040. ob_start();
  2041. $result = $response->send();
  2042. $output = ob_get_clean();
  2043. $this->assertEquals(206, $response->statusCode());
  2044. $this->assertEquals("is the test asset ", $output);
  2045. $this->assertNotSame(false, $result);
  2046. }
  2047. /**
  2048. * Test withFile() fetching ranges from a file.
  2049. *
  2050. * @return void
  2051. */
  2052. public function testWithFileRange()
  2053. {
  2054. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  2055. $response = new Response();
  2056. $new = $response->withFile(
  2057. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2058. ['download' => true]
  2059. );
  2060. $this->assertEquals(
  2061. 'attachment; filename="test_asset.css"',
  2062. $new->getHeaderLine('Content-Disposition')
  2063. );
  2064. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  2065. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2066. $this->assertEquals('18', $new->getHeaderLine('Content-Length'));
  2067. $this->assertEquals('bytes 8-25/38', $new->getHeaderLine('Content-Range'));
  2068. $this->assertEquals(206, $new->getStatusCode());
  2069. }
  2070. /**
  2071. * Provider for invalid range header values.
  2072. *
  2073. * @return array
  2074. */
  2075. public function invalidFileRangeProvider()
  2076. {
  2077. return [
  2078. // malformed range
  2079. [
  2080. 'bytes=0,38'
  2081. ],
  2082. // malformed punctuation
  2083. [
  2084. 'bytes: 0 - 38'
  2085. ],
  2086. ];
  2087. }
  2088. /**
  2089. * Test invalid file ranges.
  2090. *
  2091. * @dataProvider invalidFileRangeProvider
  2092. * @return void
  2093. */
  2094. public function testFileRangeInvalid($range)
  2095. {
  2096. $_SERVER['HTTP_RANGE'] = $range;
  2097. $response = $this->getMockBuilder('Cake\Network\Response')
  2098. ->setMethods([
  2099. '_sendHeader',
  2100. '_isActive',
  2101. ])
  2102. ->getMock();
  2103. $response->file(
  2104. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2105. ['download' => true]
  2106. );
  2107. $expected = [
  2108. 'Content-Type' => 'text/css; charset=UTF-8',
  2109. 'Content-Disposition' => 'attachment; filename="test_asset.css"',
  2110. 'Content-Transfer-Encoding' => 'binary',
  2111. 'Accept-Ranges' => 'bytes',
  2112. 'Content-Range' => 'bytes 0-37/38',
  2113. 'Content-Length' => 38,
  2114. ];
  2115. $this->assertEquals($expected, $response->header());
  2116. }
  2117. /**
  2118. * Test withFile() and invalid ranges
  2119. *
  2120. * @dataProvider invalidFileRangeProvider
  2121. * @return void
  2122. */
  2123. public function testWithFileInvalidRange($range)
  2124. {
  2125. $_SERVER['HTTP_RANGE'] = $range;
  2126. $response = new Response();
  2127. $new = $response->withFile(
  2128. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2129. ['download' => true]
  2130. );
  2131. $this->assertEquals(
  2132. 'attachment; filename="test_asset.css"',
  2133. $new->getHeaderLine('Content-Disposition')
  2134. );
  2135. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  2136. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2137. $this->assertEquals('38', $new->getHeaderLine('Content-Length'));
  2138. $this->assertEquals('bytes 0-37/38', $new->getHeaderLine('Content-Range'));
  2139. $this->assertEquals(206, $new->getStatusCode());
  2140. }
  2141. /**
  2142. * Test reversed file ranges.
  2143. *
  2144. * @return void
  2145. */
  2146. public function testFileRangeReversed()
  2147. {
  2148. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  2149. $response = $this->getMockBuilder('Cake\Network\Response')
  2150. ->setMethods([
  2151. 'header',
  2152. 'type',
  2153. '_sendHeader',
  2154. '_isActive',
  2155. ])
  2156. ->getMock();
  2157. $response->expects($this->at(1))
  2158. ->method('header')
  2159. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  2160. $response->expects($this->at(2))
  2161. ->method('header')
  2162. ->with('Content-Transfer-Encoding', 'binary');
  2163. $response->expects($this->at(3))
  2164. ->method('header')
  2165. ->with('Accept-Ranges', 'bytes');
  2166. $response->expects($this->at(4))
  2167. ->method('header')
  2168. ->with([
  2169. 'Content-Range' => 'bytes 0-37/38',
  2170. ]);
  2171. $response->file(
  2172. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2173. ['download' => true]
  2174. );
  2175. $this->assertEquals(416, $response->statusCode());
  2176. $result = $response->send();
  2177. }
  2178. /**
  2179. * Test withFile() and a reversed range
  2180. *
  2181. * @return void
  2182. */
  2183. public function testWithFileReversedRange()
  2184. {
  2185. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  2186. $response = new Response();
  2187. $new = $response->withFile(
  2188. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2189. ['download' => true]
  2190. );
  2191. $this->assertEquals(
  2192. 'attachment; filename="test_asset.css"',
  2193. $new->getHeaderLine('Content-Disposition')
  2194. );
  2195. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  2196. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2197. $this->assertEquals('bytes 0-37/38', $new->getHeaderLine('Content-Range'));
  2198. $this->assertEquals(416, $new->getStatusCode());
  2199. }
  2200. /**
  2201. * testFileRangeOffsetsNoDownload method
  2202. *
  2203. * @dataProvider rangeProvider
  2204. * @return void
  2205. */
  2206. public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse)
  2207. {
  2208. $_SERVER['HTTP_RANGE'] = $range;
  2209. $response = $this->getMockBuilder('Cake\Network\Response')
  2210. ->setMethods([
  2211. 'header',
  2212. 'type',
  2213. '_sendHeader',
  2214. '_isActive',
  2215. ])
  2216. ->getMock();
  2217. $response->expects($this->at(1))
  2218. ->method('header')
  2219. ->with('Accept-Ranges', 'bytes');
  2220. $response->expects($this->at(2))
  2221. ->method('header')
  2222. ->with([
  2223. 'Content-Length' => $length,
  2224. 'Content-Range' => $offsetResponse,
  2225. ]);
  2226. $response->expects($this->any())
  2227. ->method('_isActive')
  2228. ->will($this->returnValue(true));
  2229. $response->file(
  2230. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2231. ['download' => false]
  2232. );
  2233. ob_start();
  2234. $result = $response->send();
  2235. ob_get_clean();
  2236. }
  2237. /**
  2238. * testFileRangeNoDownload method
  2239. *
  2240. * @return void
  2241. */
  2242. public function testFileRangeNoDownload()
  2243. {
  2244. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  2245. $response = $this->getMockBuilder('Cake\Network\Response')
  2246. ->setMethods([
  2247. 'header',
  2248. 'type',
  2249. '_sendHeader',
  2250. '_isActive',
  2251. ])
  2252. ->getMock();
  2253. $response->expects($this->exactly(1))
  2254. ->method('type')
  2255. ->with('css')
  2256. ->will($this->returnArgument(0));
  2257. $response->expects($this->at(1))
  2258. ->method('header')
  2259. ->with('Accept-Ranges', 'bytes');
  2260. $response->expects($this->at(2))
  2261. ->method('header')
  2262. ->with([
  2263. 'Content-Length' => 18,
  2264. 'Content-Range' => 'bytes 8-25/38',
  2265. ]);
  2266. $response->expects($this->any())
  2267. ->method('_isActive')
  2268. ->will($this->returnValue(true));
  2269. $response->file(
  2270. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2271. ['download' => false]
  2272. );
  2273. ob_start();
  2274. $result = $response->send();
  2275. $output = ob_get_clean();
  2276. $this->assertEquals(206, $response->statusCode());
  2277. $this->assertEquals("is the test asset ", $output);
  2278. $this->assertNotSame(false, $result);
  2279. }
  2280. /**
  2281. * testFileRangeInvalidNoDownload method
  2282. *
  2283. * @return void
  2284. */
  2285. public function testFileRangeInvalidNoDownload()
  2286. {
  2287. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  2288. $response = $this->getMockBuilder('Cake\Network\Response')
  2289. ->setMethods([
  2290. 'header',
  2291. 'type',
  2292. '_sendHeader',
  2293. '_isActive',
  2294. ])
  2295. ->getMock();
  2296. $response->expects($this->at(1))
  2297. ->method('header')
  2298. ->with('Accept-Ranges', 'bytes');
  2299. $response->expects($this->at(2))
  2300. ->method('header')
  2301. ->with([
  2302. 'Content-Range' => 'bytes 0-37/38',
  2303. ]);
  2304. $response->file(
  2305. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2306. ['download' => false]
  2307. );
  2308. $this->assertEquals(416, $response->statusCode());
  2309. $result = $response->send();
  2310. }
  2311. /**
  2312. * Test the location method.
  2313. *
  2314. * @return void
  2315. */
  2316. public function testLocation()
  2317. {
  2318. $response = new Response();
  2319. $this->assertNull($response->location(), 'No header should be set.');
  2320. $this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
  2321. $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
  2322. }
  2323. /**
  2324. * Test the withLocation method.
  2325. *
  2326. * @return void
  2327. */
  2328. public function testWithLocation()
  2329. {
  2330. $response = new Response();
  2331. $this->assertSame('', $response->getHeaderLine('Location'), 'No header should be set.');
  2332. $new = $response->withLocation('http://example.org');
  2333. $this->assertNotSame($new, $response);
  2334. $this->assertSame('', $response->getHeaderLine('Location'), 'No header should be set');
  2335. $this->assertSame('http://example.org', $new->getHeaderLine('Location'), 'Header should be set');
  2336. $this->assertSame(302, $new->getStatusCode(), 'Status should be updated');
  2337. }
  2338. /**
  2339. * Test get protocol version.
  2340. *
  2341. * @return void
  2342. */
  2343. public function getProtocolVersion()
  2344. {
  2345. $response = new Response();
  2346. $version = $response->getProtocolVersion();
  2347. $this->assertEquals('1.1', $version);
  2348. }
  2349. /**
  2350. * Test with protocol.
  2351. *
  2352. * @return void
  2353. */
  2354. public function testWithProtocol()
  2355. {
  2356. $response = new Response();
  2357. $version = $response->getProtocolVersion();
  2358. $this->assertEquals('1.1', $version);
  2359. $response2 = $response->withProtocolVersion('1.0');
  2360. $version = $response2->getProtocolVersion();
  2361. $this->assertEquals('1.0', $version);
  2362. $version = $response->getProtocolVersion();
  2363. $this->assertEquals('1.1', $version);
  2364. $this->assertNotEquals($response, $response2);
  2365. }
  2366. /**
  2367. * Test with protocol.
  2368. *
  2369. * @return void
  2370. */
  2371. public function testWithStatusCode()
  2372. {
  2373. $response = new Response();
  2374. $statusCode = $response->getStatusCode();
  2375. $this->assertEquals(200, $statusCode);
  2376. $response2 = $response->withStatus(404);
  2377. $statusCode = $response2->getStatusCode();
  2378. $this->assertEquals(404, $statusCode);
  2379. $statusCode = $response->getStatusCode();
  2380. $this->assertEquals(200, $statusCode);
  2381. $this->assertNotEquals($response, $response2);
  2382. }
  2383. /**
  2384. * Test get reason phrase.
  2385. *
  2386. * @return void
  2387. */
  2388. public function testGetReasonPhrase()
  2389. {
  2390. $response = new Response();
  2391. $reasonPhrase = $response->getReasonPhrase();
  2392. $this->assertNull($reasonPhrase);
  2393. $response = $response->withStatus(404);
  2394. $reasonPhrase = $response->getReasonPhrase();
  2395. $this->assertEquals('Not Found', $reasonPhrase);
  2396. }
  2397. /**
  2398. * Test with body.
  2399. *
  2400. * @return void
  2401. */
  2402. public function testWithBody()
  2403. {
  2404. $response = new Response();
  2405. $body = $response->getBody();
  2406. $body->rewind();
  2407. $result = $body->getContents();
  2408. $this->assertEquals('', $result);
  2409. $stream = new Stream('php://memory', 'wb+');
  2410. $stream->write('test1');
  2411. $response2 = $response->withBody($stream);
  2412. $body = $response2->getBody();
  2413. $body->rewind();
  2414. $result = $body->getContents();
  2415. $this->assertEquals('test1', $result);
  2416. $body = $response->getBody();
  2417. $body->rewind();
  2418. $result = $body->getContents();
  2419. $this->assertEquals('', $result);
  2420. }
  2421. /**
  2422. * Test get Body.
  2423. *
  2424. * @return void
  2425. */
  2426. public function testGetBody()
  2427. {
  2428. $response = new Response();
  2429. $stream = $response->getBody();
  2430. $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $stream);
  2431. }
  2432. /**
  2433. * Test with header.
  2434. *
  2435. * @return void
  2436. */
  2437. public function testWithHeader()
  2438. {
  2439. $response = new Response();
  2440. $response2 = $response->withHeader('Accept', 'application/json');
  2441. $result = $response2->getHeaders();
  2442. $expected = [
  2443. 'Content-Type' => ['text/html; charset=UTF-8'],
  2444. 'Accept' => ['application/json']
  2445. ];
  2446. $this->assertEquals($expected, $result);
  2447. $this->assertFalse($response->hasHeader('Accept'));
  2448. }
  2449. /**
  2450. * Test get headers.
  2451. *
  2452. * @return void
  2453. */
  2454. public function testGetHeaders()
  2455. {
  2456. $response = new Response();
  2457. $headers = $response->getHeaders();
  2458. $response = $response->withAddedHeader('Location', 'localhost');
  2459. $response = $response->withAddedHeader('Accept', 'application/json');
  2460. $headers = $response->getHeaders();
  2461. $expected = [
  2462. 'Content-Type' => ['text/html; charset=UTF-8'],
  2463. 'Location' => ['localhost'],
  2464. 'Accept' => ['application/json']
  2465. ];
  2466. $this->assertEquals($expected, $headers);
  2467. }
  2468. /**
  2469. * Test without header.
  2470. *
  2471. * @return void
  2472. */
  2473. public function testWithoutHeader()
  2474. {
  2475. $response = new Response();
  2476. $response = $response->withAddedHeader('Location', 'localhost');
  2477. $response = $response->withAddedHeader('Accept', 'application/json');
  2478. $response2 = $response->withoutHeader('Location');
  2479. $headers = $response2->getHeaders();
  2480. $expected = [
  2481. 'Content-Type' => ['text/html; charset=UTF-8'],
  2482. 'Accept' => ['application/json']
  2483. ];
  2484. $this->assertEquals($expected, $headers);
  2485. }
  2486. /**
  2487. * Test get header.
  2488. *
  2489. * @return void
  2490. */
  2491. public function testGetHeader()
  2492. {
  2493. $response = new Response();
  2494. $response = $response->withAddedHeader('Location', 'localhost');
  2495. $result = $response->getHeader('Location');
  2496. $this->assertEquals(['localhost'], $result);
  2497. $result = $response->getHeader('location');
  2498. $this->assertEquals(['localhost'], $result);
  2499. $result = $response->getHeader('does-not-exist');
  2500. $this->assertEquals([], $result);
  2501. }
  2502. /**
  2503. * Test get header line.
  2504. *
  2505. * @return void
  2506. */
  2507. public function testGetHeaderLine()
  2508. {
  2509. $response = new Response();
  2510. $headers = $response->getHeaderLine('Accept');
  2511. $this->assertEquals('', $headers);
  2512. $response = $response->withAddedHeader('Accept', 'application/json');
  2513. $response = $response->withAddedHeader('Accept', 'application/xml');
  2514. $result = $response->getHeaderLine('Accept');
  2515. $expected = 'application/json,application/xml';
  2516. $this->assertEquals($expected, $result);
  2517. $result = $response->getHeaderLine('accept');
  2518. $this->assertEquals($expected, $result);
  2519. }
  2520. /**
  2521. * Test has header.
  2522. *
  2523. * @return void
  2524. */
  2525. public function testHasHeader()
  2526. {
  2527. $response = new Response();
  2528. $response = $response->withAddedHeader('Location', 'localhost');
  2529. $this->assertTrue($response->hasHeader('Location'));
  2530. $this->assertTrue($response->hasHeader('location'));
  2531. $this->assertTrue($response->hasHeader('locATIon'));
  2532. $this->assertFalse($response->hasHeader('Accept'));
  2533. $this->assertFalse($response->hasHeader('accept'));
  2534. }
  2535. /**
  2536. * Tests __debugInfo
  2537. *
  2538. * @return void
  2539. */
  2540. public function testDebugInfo()
  2541. {
  2542. $response = new Response();
  2543. $result = $response->__debugInfo();
  2544. $expected = [
  2545. 'status' => 200,
  2546. 'contentType' => 'text/html',
  2547. 'headers' => [
  2548. 'Content-Type' => ['text/html; charset=UTF-8']
  2549. ],
  2550. 'file' => null,
  2551. 'fileRange' => [],
  2552. 'cookies' => [],
  2553. 'cacheDirectives' => [],
  2554. 'body' => ''
  2555. ];
  2556. $this->assertEquals($expected, $result);
  2557. }
  2558. }