JsonRpcTestBase.php in JSON-RPC 8
File
tests/src/Functional/JsonRpcTestBase.php
View source
<?php
namespace Drupal\Tests\jsonrpc\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use GuzzleHttp\RequestOptions;
abstract class JsonRpcTestBase extends BrowserTestBase {
protected function postRpc(array $rpc_request, AccountInterface $account = NULL) {
$url = $this
->buildUrl(Url::fromRoute('jsonrpc.handler'));
$request_options = [
RequestOptions::HTTP_ERRORS => FALSE,
RequestOptions::ALLOW_REDIRECTS => FALSE,
RequestOptions::JSON => $rpc_request,
];
if (NULL !== $account) {
$request_options[RequestOptions::AUTH] = [
$account
->getAccountName(),
$account->passRaw,
];
}
$client = $this
->getHttpClient();
return $client
->request('POST', $url, $request_options);
}
protected function getRpc(array $rpc_request, AccountInterface $account = NULL) {
$url = $this
->buildUrl(Url::fromRoute('jsonrpc.handler'));
$request_options = [
RequestOptions::HTTP_ERRORS => FALSE,
RequestOptions::ALLOW_REDIRECTS => FALSE,
RequestOptions::QUERY => [
'query' => Json::encode($rpc_request),
],
];
if ($account !== NULL) {
$request_options[RequestOptions::AUTH] = [
$account
->getAccountName(),
$account->passRaw,
];
}
$client = $this
->getHttpClient();
return $client
->request('GET', $url, $request_options);
}
}