ResponseTest.php 91 KB

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