ResponseTest.php 91 KB

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