View source
<?php
namespace Drupal\Tests;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\BrowserKit\Response;
class DrupalTestBrowser extends AbstractBrowser {
protected $client;
public function setClient(ClientInterface $client) {
$this->client = $client;
if ($this
->getServerParameter('HTTP_HOST', NULL) !== NULL || ($base_uri = $client
->getConfig('base_uri') === NULL)) {
return $this;
}
$path = $base_uri
->getPath();
if ($path !== '' && $path !== '/') {
throw new \InvalidArgumentException('Setting a path in the Guzzle "base_uri" config option is not supported by DrupalTestBrowser.');
}
if ($this
->getServerParameter('HTTPS', NULL) === NULL && $base_uri
->getScheme() === 'https') {
$this
->setServerParameter('HTTPS', 'on');
}
$host = $base_uri
->getHost();
if (($port = $base_uri
->getPort()) !== NULL) {
$host .= ":{$port}";
}
$this
->setServerParameter('HTTP_HOST', $host);
return $this;
}
public function getClient() {
if (!$this->client) {
$this->client = new Client([
'allow_redirects' => FALSE,
'cookies' => TRUE,
]);
}
return $this->client;
}
protected function doRequest($request) : object {
$headers = [];
foreach ($request
->getServer() as $key => $val) {
$key = strtolower(str_replace('_', '-', $key));
$content_headers = [
'content-length' => TRUE,
'content-md5' => TRUE,
'content-type' => TRUE,
];
if (strpos($key, 'http-') === 0) {
$headers[substr($key, 5)] = $val;
}
elseif (isset($content_headers[$key])) {
$headers[$key] = $val;
}
}
$cookies = CookieJar::fromArray($this
->getCookieJar()
->allRawValues($request
->getUri()), parse_url($request
->getUri(), PHP_URL_HOST));
$request_options = [
'cookies' => $cookies,
'allow_redirects' => FALSE,
];
if (!\in_array($request
->getMethod(), [
'GET',
'HEAD',
], TRUE)) {
if (NULL !== ($content = $request
->getContent())) {
$request_options['body'] = $content;
}
else {
if ($files = $request
->getFiles()) {
$request_options['multipart'] = [];
$this
->addPostFields($request
->getParameters(), $request_options['multipart']);
$this
->addPostFiles($files, $request_options['multipart']);
}
else {
$request_options['form_params'] = $request
->getParameters();
}
}
}
if (!empty($headers)) {
$request_options['headers'] = $headers;
}
$method = $request
->getMethod();
$uri = $request
->getUri();
try {
$response = $this
->getClient()
->request($method, $uri, $request_options);
} catch (RequestException $e) {
if (!$e
->hasResponse()) {
throw $e;
}
$response = $e
->getResponse();
}
return $this
->createResponse($response);
}
protected function addPostFiles(array $files, array &$multipart, $array_name = '') {
if (empty($files)) {
return;
}
foreach ($files as $name => $info) {
if (!empty($array_name)) {
$name = $array_name . '[' . $name . ']';
}
$file = [
'name' => $name,
];
if (\is_array($info)) {
if (isset($info['tmp_name'])) {
if ($info['tmp_name'] !== '') {
$file['contents'] = fopen($info['tmp_name'], 'r');
if (isset($info['name'])) {
$file['filename'] = $info['name'];
}
}
else {
continue;
}
}
else {
$this
->addPostFiles($info, $multipart, $name);
continue;
}
}
else {
$file['contents'] = fopen($info, 'r');
}
$multipart[] = $file;
}
}
public function addPostFields(array $formParams, array &$multipart, $array_name = '') {
foreach ($formParams as $name => $value) {
if (!empty($array_name)) {
$name = $array_name . '[' . $name . ']';
}
if (\is_array($value)) {
$this
->addPostFields($value, $multipart, $name);
}
else {
$multipart[] = [
'name' => $name,
'contents' => $value,
];
}
}
}
protected function createResponse(ResponseInterface $response) {
return new Response((string) $response
->getBody(), $response
->getStatusCode(), $response
->getHeaders());
}
protected function filterResponse($response) {
$content_type = $response
->getHeader('Content-Type');
if (!$content_type || strpos($content_type, 'charset=') === FALSE) {
if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $response
->getContent(), $matches)) {
$headers = $response
->getHeaders();
$headers['Content-Type'] = $content_type . ';charset=' . $matches[1];
$response = new Response($response
->getContent(), $response
->getStatus(), $headers);
}
}
return parent::filterResponse($response);
}
}