| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace Illuminate\Tests\Foundation\Bootstrap\Testing\Concerns;
- use Illuminate\Contracts\Routing\Registrar;
- use Illuminate\Contracts\Routing\UrlGenerator;
- use Illuminate\Http\RedirectResponse;
- use Orchestra\Testbench\TestCase;
- class MakesHttpRequestsTest extends TestCase
- {
- public function testFromSetsHeaderAndSession()
- {
- $this->from('previous/url');
- $this->assertSame('previous/url', $this->defaultHeaders['referer']);
- $this->assertSame('previous/url', $this->app['session']->previousUrl());
- }
- public function testWithTokenSetsAuthorizationHeader()
- {
- $this->withToken('foobar');
- $this->assertSame('Bearer foobar', $this->defaultHeaders['Authorization']);
- $this->withToken('foobar', 'Basic');
- $this->assertSame('Basic foobar', $this->defaultHeaders['Authorization']);
- }
- public function testWithoutAndWithMiddleware()
- {
- $this->assertFalse($this->app->has('middleware.disable'));
- $this->withoutMiddleware();
- $this->assertTrue($this->app->has('middleware.disable'));
- $this->assertTrue($this->app->make('middleware.disable'));
- $this->withMiddleware();
- $this->assertFalse($this->app->has('middleware.disable'));
- }
- public function testWithoutAndWithMiddlewareWithParameter()
- {
- $next = function ($request) {
- return $request;
- };
- $this->assertFalse($this->app->has(MyMiddleware::class));
- $this->assertSame(
- 'fooWithMiddleware',
- $this->app->make(MyMiddleware::class)->handle('foo', $next)
- );
- $this->withoutMiddleware(MyMiddleware::class);
- $this->assertTrue($this->app->has(MyMiddleware::class));
- $this->assertSame(
- 'foo',
- $this->app->make(MyMiddleware::class)->handle('foo', $next)
- );
- $this->withMiddleware(MyMiddleware::class);
- $this->assertFalse($this->app->has(MyMiddleware::class));
- $this->assertSame(
- 'fooWithMiddleware',
- $this->app->make(MyMiddleware::class)->handle('foo', $next)
- );
- }
- public function testWithCookieSetCookie()
- {
- $this->withCookie('foo', 'bar');
- $this->assertCount(1, $this->defaultCookies);
- $this->assertSame('bar', $this->defaultCookies['foo']);
- }
- public function testWithCookiesSetsCookiesAndOverwritesPreviousValues()
- {
- $this->withCookie('foo', 'bar');
- $this->withCookies([
- 'foo' => 'baz',
- 'new-cookie' => 'new-value',
- ]);
- $this->assertCount(2, $this->defaultCookies);
- $this->assertSame('baz', $this->defaultCookies['foo']);
- $this->assertSame('new-value', $this->defaultCookies['new-cookie']);
- }
- public function testWithUnencryptedCookieSetCookie()
- {
- $this->withUnencryptedCookie('foo', 'bar');
- $this->assertCount(1, $this->unencryptedCookies);
- $this->assertSame('bar', $this->unencryptedCookies['foo']);
- }
- public function testWithUnencryptedCookiesSetsCookiesAndOverwritesPreviousValues()
- {
- $this->withUnencryptedCookie('foo', 'bar');
- $this->withUnencryptedCookies([
- 'foo' => 'baz',
- 'new-cookie' => 'new-value',
- ]);
- $this->assertCount(2, $this->unencryptedCookies);
- $this->assertSame('baz', $this->unencryptedCookies['foo']);
- $this->assertSame('new-value', $this->unencryptedCookies['new-cookie']);
- }
- public function testWithoutAndWithCredentials()
- {
- $this->encryptCookies = false;
- $this->assertSame([], $this->prepareCookiesForJsonRequest());
- $this->withCredentials();
- $this->defaultCookies = ['foo' => 'bar'];
- $this->assertSame(['foo' => 'bar'], $this->prepareCookiesForJsonRequest());
- }
- public function testFollowingRedirects()
- {
- $router = $this->app->make(Registrar::class);
- $url = $this->app->make(UrlGenerator::class);
- $router->get('from', function () use ($url) {
- return new RedirectResponse($url->to('to'));
- });
- $router->get('to', function () {
- return 'OK';
- });
- $this->followingRedirects()
- ->get('from')
- ->assertOk()
- ->assertSee('OK');
- }
- public function testFollowingRedirectsTerminatesInExpectedOrder()
- {
- $router = $this->app->make(Registrar::class);
- $url = $this->app->make(UrlGenerator::class);
- $callOrder = [];
- TerminatingMiddleware::$callback = function ($request) use (&$callOrder) {
- $callOrder[] = $request->path();
- };
- $router->get('from', function () use ($url) {
- return new RedirectResponse($url->to('to'));
- })->middleware(TerminatingMiddleware::class);
- $router->get('to', function () {
- return 'OK';
- })->middleware(TerminatingMiddleware::class);
- $this->followingRedirects()->get('from');
- $this->assertEquals(['from', 'to'], $callOrder);
- }
- }
- class MyMiddleware
- {
- public function handle($request, $next)
- {
- return $next($request.'WithMiddleware');
- }
- }
- class TerminatingMiddleware
- {
- public static $callback;
- public function handle($request, $next)
- {
- return $next($request);
- }
- public function terminate($request, $response)
- {
- call_user_func(static::$callback, $request, $response);
- }
- }
|