HttpResponseTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace Illuminate\Tests\Http;
  3. use BadMethodCallException;
  4. use Illuminate\Contracts\Support\Arrayable;
  5. use Illuminate\Contracts\Support\Jsonable;
  6. use Illuminate\Contracts\Support\MessageProvider;
  7. use Illuminate\Contracts\Support\Renderable;
  8. use Illuminate\Http\RedirectResponse;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Response;
  11. use Illuminate\Session\Store;
  12. use Illuminate\Support\MessageBag;
  13. use Illuminate\Support\ViewErrorBag;
  14. use JsonSerializable;
  15. use Mockery as m;
  16. use PHPUnit\Framework\TestCase;
  17. use Symfony\Component\HttpFoundation\Cookie;
  18. use Symfony\Component\HttpFoundation\HeaderBag;
  19. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  20. class HttpResponseTest extends TestCase
  21. {
  22. protected function tearDown(): void
  23. {
  24. m::close();
  25. }
  26. public function testJsonResponsesAreConvertedAndHeadersAreSet()
  27. {
  28. $response = new Response(new ArrayableStub);
  29. $this->assertSame('{"foo":"bar"}', $response->getContent());
  30. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  31. $response = new Response(new JsonableStub);
  32. $this->assertSame('foo', $response->getContent());
  33. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  34. $response = new Response(new ArrayableAndJsonableStub);
  35. $this->assertSame('{"foo":"bar"}', $response->getContent());
  36. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  37. $response = new Response;
  38. $response->setContent(['foo' => 'bar']);
  39. $this->assertSame('{"foo":"bar"}', $response->getContent());
  40. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  41. $response = new Response(new JsonSerializableStub);
  42. $this->assertSame('{"foo":"bar"}', $response->getContent());
  43. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  44. $response = new Response(new ArrayableStub);
  45. $this->assertSame('{"foo":"bar"}', $response->getContent());
  46. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  47. $response->setContent('{"foo": "bar"}');
  48. $this->assertSame('{"foo": "bar"}', $response->getContent());
  49. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  50. }
  51. public function testRenderablesAreRendered()
  52. {
  53. $mock = m::mock(Renderable::class);
  54. $mock->shouldReceive('render')->once()->andReturn('foo');
  55. $response = new Response($mock);
  56. $this->assertSame('foo', $response->getContent());
  57. }
  58. public function testHeader()
  59. {
  60. $response = new Response;
  61. $this->assertNull($response->headers->get('foo'));
  62. $response->header('foo', 'bar');
  63. $this->assertSame('bar', $response->headers->get('foo'));
  64. $response->header('foo', 'baz', false);
  65. $this->assertSame('bar', $response->headers->get('foo'));
  66. $response->header('foo', 'baz');
  67. $this->assertSame('baz', $response->headers->get('foo'));
  68. }
  69. public function testWithCookie()
  70. {
  71. $response = new Response;
  72. $this->assertCount(0, $response->headers->getCookies());
  73. $this->assertEquals($response, $response->withCookie(new Cookie('foo', 'bar')));
  74. $cookies = $response->headers->getCookies();
  75. $this->assertCount(1, $cookies);
  76. $this->assertSame('foo', $cookies[0]->getName());
  77. $this->assertSame('bar', $cookies[0]->getValue());
  78. }
  79. public function testGetOriginalContent()
  80. {
  81. $arr = ['foo' => 'bar'];
  82. $response = new Response;
  83. $response->setContent($arr);
  84. $this->assertSame($arr, $response->getOriginalContent());
  85. }
  86. public function testGetOriginalContentRetrievesTheFirstOriginalContent()
  87. {
  88. $previousResponse = new Response(['foo' => 'bar']);
  89. $response = new Response($previousResponse);
  90. $this->assertSame(['foo' => 'bar'], $response->getOriginalContent());
  91. }
  92. public function testSetAndRetrieveStatusCode()
  93. {
  94. $response = new Response('foo');
  95. $response->setStatusCode(404);
  96. $this->assertSame(404, $response->getStatusCode());
  97. }
  98. public function testSetStatusCodeAndRetrieveStatusText()
  99. {
  100. $response = new Response('foo');
  101. $response->setStatusCode(404);
  102. $this->assertSame('Not Found', $response->statusText());
  103. }
  104. public function testOnlyInputOnRedirect()
  105. {
  106. $response = new RedirectResponse('foo.bar');
  107. $response->setRequest(Request::create('/', 'GET', ['name' => 'Taylor', 'age' => 26]));
  108. $response->setSession($session = m::mock(Store::class));
  109. $session->shouldReceive('flashInput')->once()->with(['name' => 'Taylor']);
  110. $response->onlyInput('name');
  111. }
  112. public function testExceptInputOnRedirect()
  113. {
  114. $response = new RedirectResponse('foo.bar');
  115. $response->setRequest(Request::create('/', 'GET', ['name' => 'Taylor', 'age' => 26]));
  116. $response->setSession($session = m::mock(Store::class));
  117. $session->shouldReceive('flashInput')->once()->with(['name' => 'Taylor']);
  118. $response->exceptInput('age');
  119. }
  120. public function testFlashingErrorsOnRedirect()
  121. {
  122. $response = new RedirectResponse('foo.bar');
  123. $response->setRequest(Request::create('/', 'GET', ['name' => 'Taylor', 'age' => 26]));
  124. $response->setSession($session = m::mock(Store::class));
  125. $session->shouldReceive('get')->with('errors', m::type(ViewErrorBag::class))->andReturn(new ViewErrorBag);
  126. $session->shouldReceive('flash')->once()->with('errors', m::type(ViewErrorBag::class));
  127. $provider = m::mock(MessageProvider::class);
  128. $provider->shouldReceive('getMessageBag')->once()->andReturn(new MessageBag);
  129. $response->withErrors($provider);
  130. }
  131. public function testSettersGettersOnRequest()
  132. {
  133. $response = new RedirectResponse('foo.bar');
  134. $this->assertNull($response->getRequest());
  135. $this->assertNull($response->getSession());
  136. $request = Request::create('/', 'GET');
  137. $session = m::mock(Store::class);
  138. $response->setRequest($request);
  139. $response->setSession($session);
  140. $this->assertSame($request, $response->getRequest());
  141. $this->assertSame($session, $response->getSession());
  142. }
  143. public function testRedirectWithErrorsArrayConvertsToMessageBag()
  144. {
  145. $response = new RedirectResponse('foo.bar');
  146. $response->setRequest(Request::create('/', 'GET', ['name' => 'Taylor', 'age' => 26]));
  147. $response->setSession($session = m::mock(Store::class));
  148. $session->shouldReceive('get')->with('errors', m::type(ViewErrorBag::class))->andReturn(new ViewErrorBag);
  149. $session->shouldReceive('flash')->once()->with('errors', m::type(ViewErrorBag::class));
  150. $provider = ['foo' => 'bar'];
  151. $response->withErrors($provider);
  152. }
  153. public function testWithHeaders()
  154. {
  155. $response = new Response(null, 200, ['foo' => 'bar']);
  156. $this->assertSame('bar', $response->headers->get('foo'));
  157. $response->withHeaders(['foo' => 'BAR', 'bar' => 'baz']);
  158. $this->assertSame('BAR', $response->headers->get('foo'));
  159. $this->assertSame('baz', $response->headers->get('bar'));
  160. $responseMessageBag = new ResponseHeaderBag(['bar' => 'BAZ', 'titi' => 'toto']);
  161. $response->withHeaders($responseMessageBag);
  162. $this->assertSame('BAZ', $response->headers->get('bar'));
  163. $this->assertSame('toto', $response->headers->get('titi'));
  164. $headerBag = new HeaderBag(['bar' => 'BAAA', 'titi' => 'TATA']);
  165. $response->withHeaders($headerBag);
  166. $this->assertSame('BAAA', $response->headers->get('bar'));
  167. $this->assertSame('TATA', $response->headers->get('titi'));
  168. }
  169. public function testMagicCall()
  170. {
  171. $response = new RedirectResponse('foo.bar');
  172. $response->setRequest(Request::create('/', 'GET', ['name' => 'Taylor', 'age' => 26]));
  173. $response->setSession($session = m::mock(Store::class));
  174. $session->shouldReceive('flash')->once()->with('foo', 'bar');
  175. $response->withFoo('bar');
  176. }
  177. public function testMagicCallException()
  178. {
  179. $this->expectException(BadMethodCallException::class);
  180. $this->expectExceptionMessage('Call to undefined method Illuminate\Http\RedirectResponse::doesNotExist()');
  181. $response = new RedirectResponse('foo.bar');
  182. $response->doesNotExist('bar');
  183. }
  184. }
  185. class ArrayableStub implements Arrayable
  186. {
  187. public function toArray()
  188. {
  189. return ['foo' => 'bar'];
  190. }
  191. }
  192. class ArrayableAndJsonableStub implements Arrayable, Jsonable
  193. {
  194. public function toJson($options = 0)
  195. {
  196. return '{"foo":"bar"}';
  197. }
  198. public function toArray()
  199. {
  200. return [];
  201. }
  202. }
  203. class JsonableStub implements Jsonable
  204. {
  205. public function toJson($options = 0)
  206. {
  207. return 'foo';
  208. }
  209. }
  210. class JsonSerializableStub implements JsonSerializable
  211. {
  212. public function jsonSerialize(): array
  213. {
  214. return ['foo' => 'bar'];
  215. }
  216. }