Skip to content
Permalink
Browse files

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 7, 2015
1 parent 522ed2f commit b896127099089c1394c3df66d5112281aefa554e
@@ -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;
}
}
}
@@ -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.
*

0 comments on commit b896127

Please sign in to comment.
You can’t perform that action at this time.