+ Add test for api.
This commit is contained in:
@@ -274,6 +274,7 @@ class requests {
|
||||
public static function post($url, $headers = array(), $data = array(), $options = array()) {
|
||||
return self::request($url, $headers, $data, self::POST, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a PUT request
|
||||
*/
|
||||
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
include dirname(dirname(dirname(__FILE__))) . '/lib/init.php';
|
||||
|
||||
/**
|
||||
|
||||
title=测试API 用户获取token;
|
||||
cid=1
|
||||
pid=1
|
||||
|
||||
使用正确用户名和密码获取token >> `[A-Za-z0-9]+`
|
||||
使用正确用户名和密码获取token >> 登录失败,请检查您的用户名或密码是否填写正确。
|
||||
|
||||
*/
|
||||
$pass = $rest->post('/tokens', array('account' => 'admin', 'password' => '123456'))
|
||||
$fail = $rest->post('/tokens', array('account' => 'admin', 'password' => '123'))
|
||||
|
||||
r($pass) && c(201) && p('token') && e('`[A-Za-z0-9]+`'); // 使用正确用户名和密码获取token
|
||||
r($fail) && c(400) && p('error') && e('登录失败,请检查您的用户名或密码是否填写正确。'); // 使用正确用户名和密码获取token
|
||||
+26
-2
@@ -17,6 +17,26 @@ error_reporting(E_ALL & E_STRICT);
|
||||
|
||||
$frameworkRoot = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR;
|
||||
|
||||
/**
|
||||
* Assert status code and set body as $_result.
|
||||
*
|
||||
* @param int $code
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
function c($code)
|
||||
{
|
||||
global $_result;
|
||||
if($_result and isset($_result->status_code) and $_result->status_code == $code)
|
||||
{
|
||||
$_result = $_result->body;
|
||||
return true;
|
||||
}
|
||||
|
||||
echo ">> \n\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Load the framework. */
|
||||
include $frameworkRoot . 'router.class.php';
|
||||
include $frameworkRoot . 'control.class.php';
|
||||
@@ -26,8 +46,12 @@ include $frameworkRoot . 'helper.class.php';
|
||||
$app = router::createApp('pms', dirname(dirname(__FILE__)), 'router');
|
||||
$tester = $app->loadCommon();
|
||||
|
||||
/* Load libraries. */
|
||||
/* Load rest for api. */
|
||||
if(!isset($config->webSite)) die("Error: \$config->webSite is not set.\n");
|
||||
|
||||
$app->loadClass('requests', true);
|
||||
include 'rest.php';
|
||||
$rest = new Rest($config->webSite . '/api.php/v1');
|
||||
|
||||
/* Set configs. */
|
||||
$config->zendataRoot = dirname(dirname(__FILE__)) . '/zendata';
|
||||
@@ -83,7 +107,7 @@ function p($key, $delimiter = ',')
|
||||
$result = trim($result, $delimiter);
|
||||
}
|
||||
|
||||
echo $result . "\n\n";
|
||||
echo $result . "\n";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/**
|
||||
* The rest class.
|
||||
*
|
||||
* @package test
|
||||
*/
|
||||
class rest
|
||||
{
|
||||
/**
|
||||
* The base url of request.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
public $base;
|
||||
|
||||
/**
|
||||
* Construct.
|
||||
*
|
||||
* @param string $base
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($base)
|
||||
{
|
||||
$this->base = $base;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get method.
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $headers
|
||||
* @access public
|
||||
* @return object
|
||||
*/
|
||||
public function get($url, $headers = array())
|
||||
{
|
||||
$headers['Accept'] = 'application/json';
|
||||
$resp = requests::get($this->base . $url, $headers, array());
|
||||
try
|
||||
{
|
||||
$resp->body = json_decode($resp->body);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
}
|
||||
|
||||
return $resp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Post method.
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $data
|
||||
* @param array $headers
|
||||
* @access public
|
||||
* @return object
|
||||
*/
|
||||
public function post($url, $data = array(), $headers = array())
|
||||
{
|
||||
$headers['Accept'] = 'application/json';
|
||||
$headers['Content-Type'] = 'application/json';
|
||||
$data = json_encode($data);
|
||||
|
||||
$resp = requests::post($this->base . $url, $headers, $data, array());
|
||||
try
|
||||
{
|
||||
$resp->body = json_decode($resp->body);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
}
|
||||
|
||||
return $resp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put method.
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $data
|
||||
* @param array $headers
|
||||
* @access public
|
||||
* @return object
|
||||
*/
|
||||
public function put($url, $data = array(), $headers = array())
|
||||
{
|
||||
$headers['Accept'] = 'application/json';
|
||||
$headers['Content-Type'] = 'application/json';
|
||||
|
||||
$resp = requests::put($this->base . $url, $headers, $data, $options);
|
||||
try
|
||||
{
|
||||
$resp->body = json_decode($resp->body);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
}
|
||||
|
||||
return $resp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete method.
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $headers
|
||||
* @param array $options
|
||||
* @access public
|
||||
* @return object
|
||||
*/
|
||||
public function delete($url, $headers = array())
|
||||
{
|
||||
$headers['Accept'] = 'application/json';
|
||||
return requests::delete($this->base . $url, $headers, array());
|
||||
}
|
||||
}
|
||||
@@ -23,4 +23,4 @@ r($user->getByID('account1')) && p('account') && e('account1'); //
|
||||
r($user->getByID(1)) && p('account') && e(''); // 通过默认字段获取不存在的用户
|
||||
r($user->getByID(100000, 'id')) && p('account') && e(''); // 通过id字段获取不存在的用户
|
||||
r($user->getByID('error', 'account')) && p('account') && e(''); // 通过默认字段获取不存在的用户
|
||||
*/
|
||||
*/
|
||||
+1
-1
@@ -10,10 +10,10 @@ switch($argv[1])
|
||||
zdRun();
|
||||
break;
|
||||
case 'extract':
|
||||
ztfExtract('api');
|
||||
ztfExtract('model');
|
||||
break;
|
||||
case 'api':
|
||||
ztfExtract('api');
|
||||
ztfRun('api');
|
||||
break;
|
||||
case 'control':
|
||||
|
||||
Reference in New Issue
Block a user