Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix incorrect handling of request data.
Request data that is invalid should not be parsed into null. Instead it
should always be cast as an array. This prevents other parts of the
framework from failing. For example patchEntity() will emit errors when
passed non-array data.
  • Loading branch information
markstory committed May 8, 2015
1 parent 522ed2f commit b896127
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Controller/Component/RequestHandlerComponent.php
Expand Up @@ -240,8 +240,8 @@ public function startup(Event $event)

foreach ($this->_inputTypeMap as $type => $handler) {
if ($this->requestedWith($type)) {
$input = call_user_func_array([$controller->request, 'input'], $handler);
$request->data = $input;
$input = call_user_func_array([$request, 'input'], $handler);
$request->data = (array)$input;
}
}
}
Expand Down
Expand Up @@ -473,6 +473,39 @@ public function testStartupCallbackCharset()
$this->assertFalse(is_object($this->Controller->request->data));
}

/**
* Test that processing data results in an array.
*
* @return void
* @triggers Controller.startup $this->Controller
*/
public function testStartupProcessData()
{
$this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
$this->Controller->request->expects($this->at(0))
->method('_readInput')
->will($this->returnValue(''));
$this->Controller->request->expects($this->at(1))
->method('_readInput')
->will($this->returnValue('"invalid"'));
$this->Controller->request->expects($this->at(2))
->method('_readInput')
->will($this->returnValue('{"valid":true}'));

$this->Controller->request->env('REQUEST_METHOD', 'POST');
$this->Controller->request->env('CONTENT_TYPE', 'application/json');

$event = new Event('Controller.startup', $this->Controller);
$this->RequestHandler->startup($event);
$this->assertEquals([], $this->Controller->request->data);

$this->RequestHandler->startup($event);
$this->assertEquals(['invalid'], $this->Controller->request->data);

$this->RequestHandler->startup($event);
$this->assertEquals(['valid' => true], $this->Controller->request->data);
}

/**
* Test mapping a new type and having startup process it.
*
Expand Down

0 comments on commit b896127

Please sign in to comment.