summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorBart Visscher <bartv@thisnet.nl>2013-10-04 09:05:20 -0700
committerBart Visscher <bartv@thisnet.nl>2013-10-04 09:05:20 -0700
commitbae121b16dbabed3ec307ea58e3b8a73ab27161c (patch)
treecda1d903087731cec2f2eab69f1225b51db6b272 /tests
parentfd34c969d233724399651f6481f32d5c7c1ee18b (diff)
parentaedc427ffd73f9ab249f1722c197205bc366ed8d (diff)
downloadnextcloud-server-bae121b16dbabed3ec307ea58e3b8a73ab27161c.tar.gz
nextcloud-server-bae121b16dbabed3ec307ea58e3b8a73ab27161c.zip
Merge pull request #5068 from owncloud/improved_request
Improved request
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/appframework/http/RequestTest.php134
-rw-r--r--tests/lib/appframework/http/requeststream.php107
2 files changed, 241 insertions, 0 deletions
diff --git a/tests/lib/appframework/http/RequestTest.php b/tests/lib/appframework/http/RequestTest.php
index 0371c870cf2..00473a8c44f 100644
--- a/tests/lib/appframework/http/RequestTest.php
+++ b/tests/lib/appframework/http/RequestTest.php
@@ -8,12 +8,26 @@
namespace OC\AppFramework\Http;
+global $data;
class RequestTest extends \PHPUnit_Framework_TestCase {
+ public function setUp() {
+ require_once __DIR__ . '/requeststream.php';
+ if (in_array('fakeinput', stream_get_wrappers())) {
+ stream_wrapper_unregister('fakeinput');
+ }
+ stream_wrapper_register('fakeinput', 'RequestStream');
+ }
+
+ public function tearDown() {
+ stream_wrapper_unregister('fakeinput');
+ }
+
public function testRequestAccessors() {
$vars = array(
'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
+ 'method' => 'GET',
);
$request = new Request($vars);
@@ -31,6 +45,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals('Joey', $request->get['nickname']);
// Always returns null if variable not set.
$this->assertEquals(null, $request->{'flickname'});
+
}
// urlParams has precedence over POST which has precedence over GET
@@ -73,4 +88,123 @@ class RequestTest extends \PHPUnit_Framework_TestCase {
$request->{'nickname'} = 'Janey';
}
+ /**
+ * @expectedException LogicException
+ */
+ public function testGetTheMethodRight() {
+ $vars = array(
+ 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
+ 'method' => 'GET',
+ );
+
+ $request = new Request($vars);
+ $result = $request->post;
+ }
+
+ public function testTheMethodIsRight() {
+ $vars = array(
+ 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
+ 'method' => 'GET',
+ );
+
+ $request = new Request($vars);
+ $this->assertEquals('GET', $request->method);
+ $result = $request->get;
+ $this->assertEquals('John Q. Public', $result['name']);
+ $this->assertEquals('Joey', $result['nickname']);
+ }
+
+ public function testJsonPost() {
+ global $data;
+ $data = '{"name": "John Q. Public", "nickname": "Joey"}';
+ $vars = array(
+ 'method' => 'POST',
+ 'server' => array('CONTENT_TYPE' => 'application/json; utf-8'),
+ );
+
+ $request = new Request($vars);
+ $this->assertEquals('POST', $request->method);
+ $result = $request->post;
+ $this->assertEquals('John Q. Public', $result['name']);
+ $this->assertEquals('Joey', $result['nickname']);
+ $this->assertEquals('Joey', $request->params['nickname']);
+ $this->assertEquals('Joey', $request['nickname']);
+ }
+
+ public function testPatch() {
+ global $data;
+ $data = http_build_query(array('name' => 'John Q. Public', 'nickname' => 'Joey'), '', '&');
+
+ $vars = array(
+ 'method' => 'PATCH',
+ 'server' => array('CONTENT_TYPE' => 'application/x-www-form-urlencoded'),
+ );
+
+ $request = new Request($vars);
+
+ $this->assertEquals('PATCH', $request->method);
+ $result = $request->patch;
+
+ $this->assertEquals('John Q. Public', $result['name']);
+ $this->assertEquals('Joey', $result['nickname']);
+ }
+
+ public function testJsonPatchAndPut() {
+ global $data;
+
+ // PUT content
+ $data = '{"name": "John Q. Public", "nickname": "Joey"}';
+ $vars = array(
+ 'method' => 'PUT',
+ 'server' => array('CONTENT_TYPE' => 'application/json; utf-8'),
+ );
+
+ $request = new Request($vars);
+
+ $this->assertEquals('PUT', $request->method);
+ $result = $request->put;
+
+ $this->assertEquals('John Q. Public', $result['name']);
+ $this->assertEquals('Joey', $result['nickname']);
+
+ // PATCH content
+ $data = '{"name": "John Q. Public", "nickname": null}';
+ $vars = array(
+ 'method' => 'PATCH',
+ 'server' => array('CONTENT_TYPE' => 'application/json; utf-8'),
+ );
+
+ $request = new Request($vars);
+
+ $this->assertEquals('PATCH', $request->method);
+ $result = $request->patch;
+
+ $this->assertEquals('John Q. Public', $result['name']);
+ $this->assertEquals(null, $result['nickname']);
+ }
+
+ public function testPutStream() {
+ global $data;
+ $data = file_get_contents(__DIR__ . '/../../../data/testimage.png');
+
+ $vars = array(
+ 'put' => $data,
+ 'method' => 'PUT',
+ 'server' => array('CONTENT_TYPE' => 'image/png'),
+ );
+
+ $request = new Request($vars);
+ $this->assertEquals('PUT', $request->method);
+ $resource = $request->put;
+ $contents = stream_get_contents($resource);
+ $this->assertEquals($data, $contents);
+
+ try {
+ $resource = $request->put;
+ } catch(\LogicException $e) {
+ return;
+ }
+ $this->fail('Expected LogicException.');
+
+ }
}
diff --git a/tests/lib/appframework/http/requeststream.php b/tests/lib/appframework/http/requeststream.php
new file mode 100644
index 00000000000..e1bf5c2c6bb
--- /dev/null
+++ b/tests/lib/appframework/http/requeststream.php
@@ -0,0 +1,107 @@
+<?php
+/**
+ * Copy of http://dk1.php.net/manual/en/stream.streamwrapper.example-1.php
+ * Used to simulate php://input for Request tests
+ */
+class RequestStream {
+ protected $position;
+ protected $varname;
+
+ function stream_open($path, $mode, $options, &$opened_path) {
+ $url = parse_url($path);
+ $this->varname = $url["host"];
+ $this->position = 0;
+
+ return true;
+ }
+
+ function stream_read($count) {
+ $ret = substr($GLOBALS[$this->varname], $this->position, $count);
+ $this->position += strlen($ret);
+ return $ret;
+ }
+
+ function stream_write($data) {
+ $left = substr($GLOBALS[$this->varname], 0, $this->position);
+ $right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
+ $GLOBALS[$this->varname] = $left . $data . $right;
+ $this->position += strlen($data);
+ return strlen($data);
+ }
+
+ function stream_tell() {
+ return $this->position;
+ }
+
+ function stream_eof() {
+ return $this->position >= strlen($GLOBALS[$this->varname]);
+ }
+
+ function stream_seek($offset, $whence) {
+ switch ($whence) {
+ case SEEK_SET:
+ if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
+ $this->position = $offset;
+ return true;
+ } else {
+ return false;
+ }
+ break;
+
+ case SEEK_CUR:
+ if ($offset >= 0) {
+ $this->position += $offset;
+ return true;
+ } else {
+ return false;
+ }
+ break;
+
+ case SEEK_END:
+ if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
+ $this->position = strlen($GLOBALS[$this->varname]) + $offset;
+ return true;
+ } else {
+ return false;
+ }
+ break;
+
+ default:
+ return false;
+ }
+ }
+
+ public function stream_stat() {
+ $size = strlen($GLOBALS[$this->varname]);
+ $time = time();
+ $data = array(
+ 'dev' => 0,
+ 'ino' => 0,
+ 'mode' => 0777,
+ 'nlink' => 1,
+ 'uid' => 0,
+ 'gid' => 0,
+ 'rdev' => '',
+ 'size' => $size,
+ 'atime' => $time,
+ 'mtime' => $time,
+ 'ctime' => $time,
+ 'blksize' => -1,
+ 'blocks' => -1,
+ );
+ return array_values($data) + $data;
+ //return false;
+ }
+
+ function stream_metadata($path, $option, $var) {
+ if($option == STREAM_META_TOUCH) {
+ $url = parse_url($path);
+ $varname = $url["host"];
+ if(!isset($GLOBALS[$varname])) {
+ $GLOBALS[$varname] = '';
+ }
+ return true;
+ }
+ return false;
+ }
+}