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
|
<?php
namespace Illuminate\Tests\Integration\Foundation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Orchestra\Testbench\TestCase;
use Psr\Http\Message\ServerRequestInterface;
class RoutingServiceProviderTest extends TestCase
{
public function testItIncludesMergedDataInServerRequestInterfaceInstancesUsingGetRequests()
{
Route::get('test-route', function (ServerRequestInterface $request) {
return $request->getParsedBody();
})->middleware(MergeDataMiddleware::class);
$response = $this->withoutExceptionHandling()->get('test-route?'.http_build_query([
'sent' => 'sent-data',
'overridden' => 'overriden-sent-data',
]));
$response->assertOk();
$response->assertExactJson([
'request-data' => 'request-data',
]);
}
public function testItWorksNormallyWithoutMergeDataMiddlewareWithEmptyRequests()
{
Route::get('test-route', function (ServerRequestInterface $request) {
return $request->getParsedBody();
});
$response = $this->withoutExceptionHandling()->get('test-route', [
'content-type' => 'application/json',
]);
$response->assertOk();
$response->assertExactJson([]);
}
public function testItIncludesMergedDataInServerRequestInterfaceInstancesUsingGetJsonRequestsWithContentTypeHeader()
{
Route::get('test-route', function (ServerRequestInterface $request) {
return $request->getParsedBody();
})->middleware(MergeDataMiddleware::class);
$response = $this->getJson('test-route?'.http_build_query([
'sent' => 'sent-data',
'overridden' => 'overriden-sent-data',
]), [
'content-type' => 'application/json',
]);
$response->assertOk();
$response->assertExactJson([
'json-data' => 'json-data',
'merged' => 'replaced-merged-data',
'overridden' => 'overriden-merged-data',
'request-data' => 'request-data',
]);
}
public function testItIncludesMergedDataInServerRequestInterfaceInstancesUsingGetJsonRequests()
{
Route::get('test-route', function (ServerRequestInterface $request) {
return $request->getParsedBody();
})->middleware(MergeDataMiddleware::class);
$response = $this->getJson('test-route?'.http_build_query([
'sent' => 'sent-data',
'overridden' => 'overriden-sent-data',
]));
$response->assertOk();
$response->assertExactJson([
'json-data' => 'json-data',
'merged' => 'replaced-merged-data',
'overridden' => 'overriden-merged-data',
'request-data' => 'request-data',
]);
}
public function testItIncludesMergedDataInServerRequestInterfaceInstancesUsingPostRequests()
{
Route::post('test-route', function (ServerRequestInterface $request) {
return $request->getParsedBody();
})->middleware(MergeDataMiddleware::class);
$response = $this->post('test-route', [
'sent' => 'sent-data',
'overridden' => 'overriden-sent-data',
]);
$response->assertOk();
$response->assertExactJson([
'sent' => 'sent-data',
'merged' => 'replaced-merged-data',
'overridden' => 'overriden-merged-data',
'request-data' => 'request-data',
]);
}
public function testItIncludesMergedDataInServerRequestInterfaceInstancesUsingPostJsonRequests()
{
Route::post('test-route', function (ServerRequestInterface $request) {
return $request->getParsedBody();
})->middleware(MergeDataMiddleware::class);
$response = $this->postJson('test-route', [
'sent' => 'sent-data',
'overridden' => 'overriden-sent-data',
]);
$response->assertOk();
$response->assertExactJson([
'json-data' => 'json-data',
'sent' => 'sent-data',
'merged' => 'replaced-merged-data',
'overridden' => 'overriden-merged-data',
'request-data' => 'request-data',
]);
}
public function testItHandlesGzippedBodyPayloadsWhenCreatingServerRequestInterfaceInstances()
{
Route::post('test-route', function (ServerRequestInterface $request) {
return gzdecode((string) $request->getBody());
});
$response = $this->call('POST', 'test-route', content: file_get_contents(__DIR__.'/Fixtures/laravel.txt.gz'));
$response->assertOk();
$response->assertContent("Laravel\n");
}
}
class MergeDataMiddleware
{
public function handle(Request $request, $next)
{
$request->merge(['merged' => 'first-merged-data']);
$request->merge(['merged' => 'replaced-merged-data']);
$request->merge(['overridden' => 'overriden-merged-data']);
$request->request->set('request-data', 'request-data');
$request->query->set('query-data', 'query-data');
$request->json()->set('json-data', 'json-data');
return $next($request);
}
}
|