我正在尝试在 CakePHP 1.3 中测试一个需要 POST 请求的控制器,但 testAction 总是生成一个 GET 请求。我将其归结为一个简单的示例操作,该操作纯粹使用以下方法报告请求方法:
$this->RequestHandler->isPost()
或
$this->RequestHandler->isGet()
结果始终是 GET,无论我是否设置'method' => 'post'
或发送数据数组。
测试的形式操作我尝试过:
$this->testAction('/testing/requesttype', array('method' => 'post'));
$this->testAction('/testing/requesttype', array('data' => array('Post' => array('title' => 'test')), 'method' => 'post'));
$this->testAction('/testing/requesttype', array('data' => array('Post' => array('title' => 'test'))));
$this->testAction('/testing/requesttype', array('form' => array('test' => 'test'), 'data' => array('Post' => array('title' => 'test')), 'method' => 'post'));
以上所有内容都会生成一个 GET 请求。如果在 CakePHP 1.3 中无法做到这一点,那么'method => 'post'
的意义何在?
我这样做,它很脏,但对我有用:
$_SERVER['REQUEST_METHOD'] = 'POST';
$result = $this->testAction($url,
array(
'form' => $data
)
);