Adding first kick at the can for reading request body input.

Adding testcases.
This commit is contained in:
mark_story 2011-04-26 23:03:17 -04:00
parent d61ebaee5d
commit 60ee04a0fb
2 changed files with 58 additions and 2 deletions

View file

@ -662,6 +662,35 @@ class CakeRequest implements ArrayAccess {
return Set::classicExtract($this->data, $name);
}
/**
* Read data from `php://stdin`. Useful when interacting with XML or JSON
* request body content.
*
* @param string $callback A decoding callback that will convert the string data to another
* representation. Leave empty to access the raw input data.
* @return The decoded/processed request data.
*/
public function input($callback = null) {
$input = $this->_readStdin();
if ($callback) {
return call_user_func($callback, $input);
}
return $input;
}
/**
* Read data from php://stdin, mocked in tests.
*
* @return string contents of stdin
*/
protected function _readStdin() {
$fh = fopen('php://stdin', 'r');
rewind($fh);
$content = stream_get_contents($fh);
fclose($fh);
return $content;
}
/**
* Array access read implementation
*
@ -711,4 +740,4 @@ class CakeRequest implements ArrayAccess {
public function offsetUnset($name) {
unset($this->params[$name]);
}
}
}

View file

@ -1434,6 +1434,33 @@ class CakeRequestTestCase extends CakeTestCase {
$this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
}
/**
* Test the input() method.
*
* @return void
*/
function testInput() {
$request = $this->getMock('CakeRequest', array('_readStdin'));
$request->expects($this->once())->method('_readStdin')
->will($this->returnValue('I came from stdin'));
$result = $request->input();
$this->assertEquals('I came from stdin', $result);
}
/**
* Test input() decoding.
*
* @return void
*/
function testInputDecode() {
$request = $this->getMock('CakeRequest', array('_readStdin'));
$request->expects($this->once())->method('_readStdin')
->will($this->returnValue('{"name":"value"}'));
$result = $request->input('json_decode');
$this->assertEquals(array('name' => 'value'), (array)$result);
}
/**
* loadEnvironment method
*
@ -1465,4 +1492,4 @@ class CakeRequestTestCase extends CakeTestCase {
}
}
}
}