1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
<?php
namespace Illuminate\Tests\Foundation\Testing\Concerns;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests;
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 testFromRouteSetsHeaderAndSession()
{
$router = $this->app->make(Registrar::class);
$router->get('previous/url', fn () => 'ok')->name('previous-url');
$this->fromRoute('previous-url');
$this->assertSame('http://localhost/previous/url', $this->defaultHeaders['referer']);
$this->assertSame('http://localhost/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 testWithBasicAuthSetsAuthorizationHeader()
{
$callback = function ($username, $password) {
return base64_encode("$username:$password");
};
$username = 'foo';
$password = 'bar';
$this->withBasicAuth($username, $password);
$this->assertSame('Basic '.$callback($username, $password), $this->defaultHeaders['Authorization']);
$password = 'buzz';
$this->withBasicAuth($username, $password);
$this->assertSame('Basic '.$callback($username, $password), $this->defaultHeaders['Authorization']);
}
public function testWithoutTokenRemovesAuthorizationHeader()
{
$this->withToken('foobar');
$this->assertSame('Bearer foobar', $this->defaultHeaders['Authorization']);
$this->withoutToken();
$this->assertArrayNotHasKey('Authorization', $this->defaultHeaders);
}
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);
}
public function testWithPrecognition()
{
$this->withPrecognition();
$this->assertSame('true', $this->defaultHeaders['Precognition']);
$this->app->make(Registrar::class)
->get('test-route', fn () => 'ok')->middleware(HandlePrecognitiveRequests::class);
$this->get('test-route')
->assertStatus(204)
->assertHeader('Precognition', 'true')
->assertHeader('Precognition-Success', 'true');
}
}
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);
}
}
|