-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[10.x] Allow creation of PSR request with merged data (#48696)
* Allow creation of PSR request with merged data * Add dependencies to require-dev
- Loading branch information
1 parent
7817052
commit 967dab6
Showing
3 changed files
with
117 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
tests/Integration/Foundation/RoutingServiceProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?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 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', | ||
]); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} |