我正在尝试为使用Guzzle http客户端的端点编写测试用例。但这并没有被嘲笑。它仍然符合实际要求并等待响应。我的代码如下。
测试用例
new Response(200, ['X-Foo' => 'Bar'], 'Hello, World'),
]);
$handler = HandlerStack::create($mockHandler);
$client = new Client(['handler' => $handler]);
$this->app->instance(\GuzzleHttp\Client::class, $client);
$response = $this->actingAs($user)
->get('/api/request?' . http_build_query($request));
我在服务类中使用Guzzle客户端的方式如下
{
public function __construct(\GuzzleHttp\Client $client, $extraInfo)
{
$this->client = $client;
$this->setExtraInfo($extraInfo);
}
private function getToken($scope)
{
$response = $this->client->post(
'/post/request/url',
[
'form_params' => [
"param_1" => "value_1",
"param_2" => 'value_2'
],
'headers' => [
"Authorization"=> "Basic 5235252333",
"Content-Type" => 'application/x-www-form-urlencoded'
],
self::COMMON_REQUEST_PARAMS,
'cert' => ['cert path' , 'pass phrase']
]
);
$jsonOutput = json_decode($response->getBody()->getContents());
return $jsonOutput->access_token;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。