From 9e6e29bc9326ac4517fd6b9aa3e6382c1c2ab4c5 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 11 Oct 2015 18:50:48 +0200 Subject: [PATCH] working on API: simplifying PUT request mocking --- lib/request.php | 16 ++++----- tst/jsonApi.php | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ tst/request.php | 2 +- 3 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 tst/jsonApi.php diff --git a/lib/request.php b/lib/request.php index 7b80f3b9..5edd74d8 100644 --- a/lib/request.php +++ b/lib/request.php @@ -23,7 +23,7 @@ class request * @access private * @var string */ - private $_inputStream = 'php://input'; + private static $_inputStream = 'php://input'; /** * Operation to perform. @@ -80,7 +80,8 @@ class request switch (array_key_exists('REQUEST_METHOD', $_SERVER) ? $_SERVER['REQUEST_METHOD'] : 'GET') { case 'PUT': - parse_str(file_get_contents($this->_inputStream), $this->_params); + $this->_operation = 'create'; + parse_str(file_get_contents(self::$_inputStream), $this->_params); break; case 'POST': $this->_params = $_POST; @@ -89,7 +90,7 @@ class request $this->_params = $_GET; } - // prepare paremeters, depending on current operation + // prepare parameters, depending on current operation if ( (array_key_exists('data', $this->_params) && !empty($this->_params['data'])) || (array_key_exists('attachment', $this->_params) && !empty($this->_params['attachment'])) @@ -107,7 +108,7 @@ class request // display an existing paste elseif (array_key_exists('QUERY_STRING', $_SERVER) && !empty($_SERVER['QUERY_STRING'])) { - $this->_operation = 'read'; + if ($this->_operation != 'create') $this->_operation = 'read'; $this->_params['pasteid'] = $_SERVER['QUERY_STRING']; } } @@ -148,13 +149,12 @@ class request } /** - * Override the default input stream source + * Override the default input stream source, used for unit testing. * * @param unknown $input */ - public function setInputStream($input) + public static function setInputStream($input) { - $this->_inputStream = $input; - $this->__construct(); + self::$_inputStream = $input; } } \ No newline at end of file diff --git a/tst/jsonApi.php b/tst/jsonApi.php new file mode 100644 index 00000000..6bedf0d5 --- /dev/null +++ b/tst/jsonApi.php @@ -0,0 +1,92 @@ +_model = zerobin_data::getInstance(array('dir' => PATH . 'data')); + serversalt::setPath(PATH . 'data'); + $this->reset(); + } + + public function tearDown() + { + /* Tear Down Routine */ + helper::confRestore(); + } + + public function reset() + { + $_POST = array(); + $_GET = array(); + $_SERVER = array(); + if ($this->_model->exists(helper::getPasteId())) + $this->_model->delete(helper::getPasteId()); + helper::confRestore(); + } + + /** + * @runInSeparateProcess + */ + public function testCreate() + { + $this->reset(); + $options = parse_ini_file(CONF, true); + $options['traffic']['limit'] = 0; + helper::confBackup(); + helper::createIniFile(CONF, $options); + $_POST = helper::getPaste(); + $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_SERVER['REMOTE_ADDR'] = '::1'; + ob_start(); + new zerobin; + $content = ob_get_contents(); + $response = json_decode($content, true); + $this->assertEquals(0, $response['status'], 'outputs status'); + $this->assertEquals( + hash_hmac('sha1', $response['id'], serversalt::get()), + $response['deletetoken'], + 'outputs valid delete token' + ); + $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste'); + $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data'); + } + + /** + * @runInSeparateProcess + */ + public function testPut() + { + $this->reset(); + $options = parse_ini_file(CONF, true); + $options['traffic']['limit'] = 0; + helper::confBackup(); + helper::createIniFile(CONF, $options); + $file = tempnam(sys_get_temp_dir(), 'FOO'); + $paste = helper::getPaste(); + unset($paste['meta']); + file_put_contents($file, http_build_query($paste)); + request::setInputStream($file); + $_SERVER['QUERY_STRING'] = helper::getPasteId(); + $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; + $_SERVER['REQUEST_METHOD'] = 'PUT'; + $_SERVER['REMOTE_ADDR'] = '::1'; + ob_start(); + new zerobin; + $content = ob_get_contents(); + $response = json_decode($content, true); + $this->assertEquals(0, $response['status'], 'outputs status'); + $this->assertEquals(helper::getPasteId(), $response['id'], 'outputted paste ID matches input'); + $this->assertEquals( + hash_hmac('sha1', $response['id'], serversalt::get()), + $response['deletetoken'], + 'outputs valid delete token' + ); + $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste'); + $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data'); + } + +} \ No newline at end of file diff --git a/tst/request.php b/tst/request.php index e6901eb0..eb76f37a 100644 --- a/tst/request.php +++ b/tst/request.php @@ -60,8 +60,8 @@ class requestTest extends PHPUnit_Framework_TestCase $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; $file = tempnam(sys_get_temp_dir(), 'FOO'); file_put_contents($file, 'data=foo'); + request::setInputStream($file); $request = new request; - $request->setInputStream($file); $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call'); $this->assertEquals('create', $request->getOperation()); $this->assertEquals('foo', $request->getParam('data'));