diff options
author | Thomas Tanghus <thomas@tanghus.net> | 2013-09-28 01:35:24 +0200 |
---|---|---|
committer | Thomas Tanghus <thomas@tanghus.net> | 2013-10-01 20:13:13 +0200 |
commit | 973bcccd7cc18867ea22c24f83421ed180b5022c (patch) | |
tree | dcb1dcf2832b9c73c65f73d050bc9228a4f8f33d /tests | |
parent | 36d1156cf8fbd340ecfcc9d9a06e69004acfe154 (diff) | |
download | nextcloud-server-973bcccd7cc18867ea22c24f83421ed180b5022c.tar.gz nextcloud-server-973bcccd7cc18867ea22c24f83421ed180b5022c.zip |
Implement PUT an PATCH support
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/appframework/http/RequestTest.php | 101 | ||||
-rw-r--r-- | tests/lib/appframework/http/requeststream.php | 107 |
2 files changed, 207 insertions, 1 deletions
diff --git a/tests/lib/appframework/http/RequestTest.php b/tests/lib/appframework/http/RequestTest.php index ff4a8357f06..847c6610fe8 100644 --- a/tests/lib/appframework/http/RequestTest.php +++ b/tests/lib/appframework/http/RequestTest.php @@ -8,6 +8,7 @@ namespace OC\AppFramework\Http; +global $data; class RequestTest extends \PHPUnit_Framework_TestCase { @@ -32,6 +33,8 @@ class RequestTest extends \PHPUnit_Framework_TestCase { $this->assertEquals('Joey', $request->get['nickname']); // Always returns null if variable not set. $this->assertEquals(null, $request->{'flickname'}); + + require_once __DIR__ . '/requeststream.php'; } // urlParams has precedence over POST which has precedence over GET @@ -75,7 +78,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase { } /** - * @expectedException BadMethodCallException + * @expectedException LogicException */ public function testGetTheMethodRight() { $vars = array( @@ -100,4 +103,100 @@ class RequestTest extends \PHPUnit_Framework_TestCase { $this->assertEquals('Joey', $result['nickname']); } + public function testJsonPost() { + $vars = array( + 'post' => '{"name": "John Q. Public", "nickname": "Joey"}', + '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']); + } + + public function testPatch() { + global $data; + $data = http_build_query(array('name' => 'John Q. Public', 'nickname' => 'Joey'), '', '&'); + + if (in_array('fakeinput', stream_get_wrappers())) { + stream_wrapper_unregister('fakeinput'); + } + stream_wrapper_register('fakeinput', 'RequestStream'); + + $vars = array( + 'patch' => $data, + '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']); + + stream_wrapper_unregister('fakeinput'); + } + + public function testJsonPatch() { + global $data; + $data = '{"name": "John Q. Public", "nickname": null}'; + + if (in_array('fakeinput', stream_get_wrappers())) { + stream_wrapper_unregister('fakeinput'); + } + stream_wrapper_register('fakeinput', 'RequestStream'); + + $vars = array( + 'patch' => $data, + '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']); + + stream_wrapper_unregister('fakeinput'); + } + + public function testPutSteam() { + global $data; + $data = file_get_contents(__DIR__ . '/../../../data/testimage.png'); + + if (in_array('fakeinput', stream_get_wrappers())) { + stream_wrapper_unregister('fakeinput'); + } + stream_wrapper_register('fakeinput', 'RequestStream'); + + $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) { + stream_wrapper_unregister('fakeinput'); + 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; + } +} |