Skip to content

Commit

Permalink
[2.x] Fixes PUT verb form url encoded bodies and file uploads (#126)
Browse files Browse the repository at this point in the history
* Fixes `PUT` verb form url encoded bodies and file uploads

* Adds missing test for lb
  • Loading branch information
nunomaduro authored Apr 1, 2022
1 parent e35ab60 commit 372fdf7
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Runtime/Octane/OctaneRequestContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected static function parseFiles($contentType, $body)
*/
protected static function uploadedFiles($method, $contentType, $body)
{
if ($method !== 'POST' ||
if (! in_array($method, ['POST', 'PUT']) ||
is_null($contentType) ||
static::isUrlEncodedForm($contentType)) {
return [];
Expand All @@ -154,7 +154,7 @@ protected static function uploadedFiles($method, $contentType, $body)
*/
protected static function parsedBody($method, $contentType, $body)
{
if ($method !== 'POST' || is_null($contentType)) {
if (! in_array($method, ['POST', 'PUT']) || is_null($contentType)) {
return;
}

Expand Down
69 changes: 69 additions & 0 deletions tests/Feature/LoadBalancedOctaneHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,31 @@ public function test_request_body()
], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_request_form_url_encoded_without_inline_input_method()
{
$handler = new LoadBalancedOctaneHandler();

Route::put('/', function (Request $request) {
return $request->all();
});

$response = $handler->handle([
'httpMethod' => 'PUT',
'path' => '/',
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
],
'body' => <<<'EOF'
name=nuno&[email protected]
EOF
]);

static::assertEquals([
'name' => 'nuno',
'email' => '[email protected]',
], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_request_file_uploads()
{
$handler = new LoadBalancedOctaneHandler();
Expand Down Expand Up @@ -333,6 +358,50 @@ public function test_request_file_uploads()
], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_request_file_uploads_without_inline_input_method()
{
$handler = new LoadBalancedOctaneHandler();

Route::put('/', function (Request $request) {
return array_merge($request->all(), [
'filename' => $request->file('file')->getClientOriginalName(),
'file' => $request->file('file')->getContent(),
]);
});

$response = $handler->handle([
'httpMethod' => 'PUT',
'path' => '/',
'headers' => [
'Content-Type' => 'multipart/form-data; boundary=---------------------------317050813134112680482597024243',
],
'body' => <<<'EOF'
-----------------------------317050813134112680482597024243
Content-Disposition: form-data; name="name"
nuno
-----------------------------317050813134112680482597024243
Content-Disposition: form-data; name="email"
[email protected]
-----------------------------317050813134112680482597024243
Content-Disposition: form-data; name="file"; filename="my_uploaded.txt"
Content-Type: text/plain
foo
-----------------------------317050813134112680482597024243--
EOF
]);

static::assertEquals([
'name' => 'nuno',
'email' => '[email protected]',
'filename' => 'my_uploaded.txt',
'file' => 'foo',
], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_request_cookies()
{
$handler = new LoadBalancedOctaneHandler();
Expand Down
69 changes: 69 additions & 0 deletions tests/Feature/OctaneHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,31 @@ public function test_request_body()
], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_request_form_url_encoded_without_inline_input_method()
{
$handler = new OctaneHandler();

Route::put('/', function (Request $request) {
return $request->all();
});

$response = $handler->handle([
'httpMethod' => 'PUT',
'path' => '/',
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
],
'body' => <<<'EOF'
name=nuno&[email protected]
EOF
]);

static::assertEquals([
'name' => 'nuno',
'email' => '[email protected]',
], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_request_cookies()
{
$handler = new OctaneHandler();
Expand Down Expand Up @@ -357,6 +382,50 @@ public function test_request_file_uploads()
], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_request_file_uploads_without_inline_input_method()
{
$handler = new OctaneHandler();

Route::put('/', function (Request $request) {
return array_merge($request->all(), [
'filename' => $request->file('file')->getClientOriginalName(),
'file' => $request->file('file')->getContent(),
]);
});

$response = $handler->handle([
'httpMethod' => 'PUT',
'path' => '/',
'headers' => [
'Content-Type' => 'multipart/form-data; boundary=---------------------------317050813134112680482597024243',
],
'body' => <<<'EOF'
-----------------------------317050813134112680482597024243
Content-Disposition: form-data; name="name"
nuno
-----------------------------317050813134112680482597024243
Content-Disposition: form-data; name="email"
[email protected]
-----------------------------317050813134112680482597024243
Content-Disposition: form-data; name="file"; filename="my_uploaded.txt"
Content-Type: text/plain
foo
-----------------------------317050813134112680482597024243--
EOF
]);

static::assertEquals([
'name' => 'nuno',
'email' => '[email protected]',
'filename' => 'my_uploaded.txt',
'file' => 'foo',
], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_request_query_string()
{
$handler = new OctaneHandler();
Expand Down

0 comments on commit 372fdf7

Please sign in to comment.