gpt4 book ai didi

rest - PHPUnit - REST API 测试

转载 作者:行者123 更新时间:2023-12-05 08:12:09 25 4
gpt4 key购买 nike


我有用 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);


}
}

最佳答案

你能试试吗

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"}"

关于rest - PHPUnit - REST API 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26732031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com