MakesHttpRequestsTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Illuminate\Tests\Foundation\Bootstrap\Testing\Concerns;
  3. use Illuminate\Contracts\Routing\Registrar;
  4. use Illuminate\Contracts\Routing\UrlGenerator;
  5. use Illuminate\Http\RedirectResponse;
  6. use Orchestra\Testbench\TestCase;
  7. class MakesHttpRequestsTest extends TestCase
  8. {
  9. public function testFromSetsHeaderAndSession()
  10. {
  11. $this->from('previous/url');
  12. $this->assertSame('previous/url', $this->defaultHeaders['referer']);
  13. $this->assertSame('previous/url', $this->app['session']->previousUrl());
  14. }
  15. public function testWithTokenSetsAuthorizationHeader()
  16. {
  17. $this->withToken('foobar');
  18. $this->assertSame('Bearer foobar', $this->defaultHeaders['Authorization']);
  19. $this->withToken('foobar', 'Basic');
  20. $this->assertSame('Basic foobar', $this->defaultHeaders['Authorization']);
  21. }
  22. public function testWithoutAndWithMiddleware()
  23. {
  24. $this->assertFalse($this->app->has('middleware.disable'));
  25. $this->withoutMiddleware();
  26. $this->assertTrue($this->app->has('middleware.disable'));
  27. $this->assertTrue($this->app->make('middleware.disable'));
  28. $this->withMiddleware();
  29. $this->assertFalse($this->app->has('middleware.disable'));
  30. }
  31. public function testWithoutAndWithMiddlewareWithParameter()
  32. {
  33. $next = function ($request) {
  34. return $request;
  35. };
  36. $this->assertFalse($this->app->has(MyMiddleware::class));
  37. $this->assertSame(
  38. 'fooWithMiddleware',
  39. $this->app->make(MyMiddleware::class)->handle('foo', $next)
  40. );
  41. $this->withoutMiddleware(MyMiddleware::class);
  42. $this->assertTrue($this->app->has(MyMiddleware::class));
  43. $this->assertSame(
  44. 'foo',
  45. $this->app->make(MyMiddleware::class)->handle('foo', $next)
  46. );
  47. $this->withMiddleware(MyMiddleware::class);
  48. $this->assertFalse($this->app->has(MyMiddleware::class));
  49. $this->assertSame(
  50. 'fooWithMiddleware',
  51. $this->app->make(MyMiddleware::class)->handle('foo', $next)
  52. );
  53. }
  54. public function testWithCookieSetCookie()
  55. {
  56. $this->withCookie('foo', 'bar');
  57. $this->assertCount(1, $this->defaultCookies);
  58. $this->assertSame('bar', $this->defaultCookies['foo']);
  59. }
  60. public function testWithCookiesSetsCookiesAndOverwritesPreviousValues()
  61. {
  62. $this->withCookie('foo', 'bar');
  63. $this->withCookies([
  64. 'foo' => 'baz',
  65. 'new-cookie' => 'new-value',
  66. ]);
  67. $this->assertCount(2, $this->defaultCookies);
  68. $this->assertSame('baz', $this->defaultCookies['foo']);
  69. $this->assertSame('new-value', $this->defaultCookies['new-cookie']);
  70. }
  71. public function testWithUnencryptedCookieSetCookie()
  72. {
  73. $this->withUnencryptedCookie('foo', 'bar');
  74. $this->assertCount(1, $this->unencryptedCookies);
  75. $this->assertSame('bar', $this->unencryptedCookies['foo']);
  76. }
  77. public function testWithUnencryptedCookiesSetsCookiesAndOverwritesPreviousValues()
  78. {
  79. $this->withUnencryptedCookie('foo', 'bar');
  80. $this->withUnencryptedCookies([
  81. 'foo' => 'baz',
  82. 'new-cookie' => 'new-value',
  83. ]);
  84. $this->assertCount(2, $this->unencryptedCookies);
  85. $this->assertSame('baz', $this->unencryptedCookies['foo']);
  86. $this->assertSame('new-value', $this->unencryptedCookies['new-cookie']);
  87. }
  88. public function testWithoutAndWithCredentials()
  89. {
  90. $this->encryptCookies = false;
  91. $this->assertSame([], $this->prepareCookiesForJsonRequest());
  92. $this->withCredentials();
  93. $this->defaultCookies = ['foo' => 'bar'];
  94. $this->assertSame(['foo' => 'bar'], $this->prepareCookiesForJsonRequest());
  95. }
  96. public function testFollowingRedirects()
  97. {
  98. $router = $this->app->make(Registrar::class);
  99. $url = $this->app->make(UrlGenerator::class);
  100. $router->get('from', function () use ($url) {
  101. return new RedirectResponse($url->to('to'));
  102. });
  103. $router->get('to', function () {
  104. return 'OK';
  105. });
  106. $this->followingRedirects()
  107. ->get('from')
  108. ->assertOk()
  109. ->assertSee('OK');
  110. }
  111. public function testFollowingRedirectsTerminatesInExpectedOrder()
  112. {
  113. $router = $this->app->make(Registrar::class);
  114. $url = $this->app->make(UrlGenerator::class);
  115. $callOrder = [];
  116. TerminatingMiddleware::$callback = function ($request) use (&$callOrder) {
  117. $callOrder[] = $request->path();
  118. };
  119. $router->get('from', function () use ($url) {
  120. return new RedirectResponse($url->to('to'));
  121. })->middleware(TerminatingMiddleware::class);
  122. $router->get('to', function () {
  123. return 'OK';
  124. })->middleware(TerminatingMiddleware::class);
  125. $this->followingRedirects()->get('from');
  126. $this->assertEquals(['from', 'to'], $callOrder);
  127. }
  128. }
  129. class MyMiddleware
  130. {
  131. public function handle($request, $next)
  132. {
  133. return $next($request.'WithMiddleware');
  134. }
  135. }
  136. class TerminatingMiddleware
  137. {
  138. public static $callback;
  139. public function handle($request, $next)
  140. {
  141. return $next($request);
  142. }
  143. public function terminate($request, $response)
  144. {
  145. call_user_func(static::$callback, $request, $response);
  146. }
  147. }