我有用php写的REST API,我想用phpunit测试一下。
我这样写的测试,它工作,但响应体是空的。我用fiddler测试过了,它会发送响应体。
对不起,我的英语不好。
class ProgrammerControllerTest extends PHPUnit_Framework_TestCase
{
public function testPOST()
{
// create our http client (Guzzle)
$client = new Guzzle\Http\Client();
$response = $client->post("http://api.loc/v2/", array(
'headers' => "User-Agent: Fiddler\r\n" .
"Host: api.loc\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: 34\r\n",
'body' => array('kle' =>'sino','lat' => '41', 'long' => '69'),
));
var_dump($response);
}
} 发布于 2014-11-04 19:29:44
您可以使用Laravel的内置方法call来测试您的控制器。
$response = $this->call('POST', '/api/v1.0/pages', $parameters);
$data = $response->getData();您的ProgrammerControllerTest应该从TestCase扩展
发布于 2016-07-12 16:18:05
你能试试吗?
var_dump($response->getBody()->getContents());这是我的代码片段(它在Get中,但它是相同的)
$client = new GuzzleHttp\Client();
$response = $client->get('http://192.168.99.100/v1/hello');
var_dump($response->getBody()->getContents());结果:
string(13) "{"bar":"foo"}"发布于 2015-03-16 02:30:09
我知道亚马逊使用Guzzle来测试AWS SDK,你可以在http://guzzle3.readthedocs.org/testing/unit-testing.html上阅读更多信息
而且,不要犹豫去挖掘其他人正在使用的东西(如Amazon,Facebook等),这就是为什么开源如此伟大的原因!
https://stackoverflow.com/questions/26732031
复制相似问题