Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

RESTful: Handle single field request with empty value #251

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/Controller/AbstractRestfulController.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,13 @@ protected function processBodyContent($request)

parse_str($content, $parsedParams);

// If parse_str fails to decode, or we have a single element with empty value
if (! is_array($parsedParams) || empty($parsedParams)
|| (1 == count($parsedParams) && '' === reset($parsedParams))
) {
// If parse_str fails to decode
if (! is_array($parsedParams) || empty($parsedParams)) {
return $content;
}

// If we have a single element with empty value
if (count($parsedParams) === 1 && reset($parsedParams) === '' && substr($content, -1) !== '=') {
return $content;
}

Expand Down
56 changes: 40 additions & 16 deletions test/Controller/RestfulControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,6 @@ public function testDispatchInvokesCreateMethodWhenNoActionPresentAndPostInvoked
$this->assertEquals('create', $this->routeMatch->getParam('action'));
}

public function testCanReceiveStringAsRequestContent()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test has been merged into the new test added below.

{
$string = "any content";
$this->request->setMethod('PUT');
$this->request->setContent($string);
$this->routeMatch->setParam('id', $id = 1);

$controller = new RestfulContentTypeTestController();
$controller->setEvent($this->event);
$result = $controller->dispatch($this->request, $this->response);

$this->assertEquals($id, $result['id']);
$this->assertEquals($string, $result['data']);
$this->assertEquals('update', $this->routeMatch->getParam('action'));
}

public function testDispatchInvokesUpdateMethodWhenNoActionPresentAndPutInvokedWithIdentifier()
{
$entity = ['name' => __FUNCTION__];
Expand Down Expand Up @@ -561,4 +545,44 @@ public function providerNotImplementedMethodSets504HttpCodeProvider()
['PUT', json_encode(['foo' => 1]), []], // AbstractRestfulController::replaceList()
];
}

/**
* @dataProvider nonJsonRequestContent
*
* @param string $action
* @param string $method
* @param string $content
* @param array|string $expectedResult
*/
public function testParseNonJsonRequestContent($action, $method, $content, $expectedResult)
{
$this->request->setMethod($method);
$this->request->setContent($content);
$this->routeMatch->setParam('id', $id = 1);

$controller = new RestfulContentTypeTestController();
$controller->setEvent($this->event);
$result = $controller->dispatch($this->request, $this->response);

$this->assertSame($id, $result['id']);
$this->assertSame($expectedResult, $result['data']);
$this->assertSame($action, $this->routeMatch->getParam('action'));
}

/**
* @return string[]|mixed[]
*/
public function nonJsonRequestContent()
{
yield ['update', 'PUT', 'foo=', ['foo' => '']];
yield ['update', 'PUT', 'foo', 'foo'];
yield ['patch', 'PATCH', 'foo=', ['foo' => '']];
yield ['patch', 'PATCH', 'foo', 'foo'];
yield ['update', 'PUT', 'foo&bar', ['foo' => '', 'bar' => '']];
yield ['patch', 'PATCH', 'foo&bar', ['foo' => '', 'bar' => '']];
yield ['update', 'PUT', 'foo=bar', ['foo' => 'bar']];
yield ['patch', 'PATCH', 'foo=bar', ['foo' => 'bar']];
yield ['update', 'PUT', 'any content', 'any content'];
yield ['patch', 'PATCH', 'any content', 'any content'];
}
}
15 changes: 15 additions & 0 deletions test/Controller/TestAsset/RestfulContentTypeTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ public function update($id, $data)
'data' => $data,
];
}

/**
* Patch an existing resource
*
* @param mixed $id
* @param mixed $data
* @return array
*/
public function patch($id, $data)
{
return [
'id' => $id,
'data' => $data,
];
}
}