Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/TestHelpers/AppConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
namespace TestHelpers;

use GuzzleHttp\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Helper to set various configurations through the testing app
Expand Down
2 changes: 1 addition & 1 deletion tests/TestHelpers/DeleteHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
namespace TestHelpers;

use GuzzleHttp\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Helper for deleting files
Expand Down
2 changes: 1 addition & 1 deletion tests/TestHelpers/DownloadHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
namespace TestHelpers;

use GuzzleHttp\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Helper for Downloads
Expand Down
2 changes: 1 addition & 1 deletion tests/TestHelpers/EmailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
namespace TestHelpers;

use GuzzleHttp\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Helper to test email sending, using mailhog
Expand Down
116 changes: 65 additions & 51 deletions tests/TestHelpers/HttpRequestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use SimpleXMLElement;
use Sabre\Xml\LibXMLException;
use Sabre\Xml\Reader;
use GuzzleHttp\BatchResults;
use GuzzleHttp\Pool;

/**
Expand Down Expand Up @@ -68,25 +70,28 @@ public static function sendRequest(
$client = null
) {
if ($client === null) {
$client = new Client();
$client = self::createClient(
$user,
$password,
$config,
$cookies,
$stream,
$timeout
);
}
/**
* @var RequestInterface $request
*/
$request = self::createRequest(
$url,
$method,
$user,
$password,
$headers,
$body,
$config,
$cookies,
$stream,
$timeout,
$client
$body
);

try {
$response = $client->send($request);
} catch (BadResponseException $ex) {
} catch (GuzzleException $ex) {
$response = $ex->getResponse();

//if the response was null for some reason do not return it but re-throw
Expand All @@ -103,65 +108,44 @@ public static function sendRequest(
* It will send all the requests to the server using the Pool object in guzzle.
*
* @param array $requests
* @param Client|null $client
* @param Client $client
*
* @return BatchResults
* @return array
*/
public static function sendBatchRequest(
$requests,
$client = null
$client
) {
if (!isset($client)) {
$client = new Client();
}
$results = Pool::batch($client, $requests);
return $results;
}

/**
* Create an http request based on given parameters.
* This creates an RequestInterface object before sending it to the server.
* This also enables to create multiple requests in advance so that we can send them to the server at once in parallel.
* Create a Guzzle Client
* This creates a client object that can be used later to send a request object(s)
*
* @param string $url
* @param string $method
* @param string $user
* @param string $password
* @param array $headers ['X-MyHeader' => 'value']
* @param mixed $body
* @param array $config
* @param CookieJar $cookies
* @param bool $stream Set to true to stream a response rather
* than download it all up-front.
* @param int $timeout
* @param Client|null $client
*
* @return RequestInterface
* @return Client
*/
public static function createRequest(
$url,
$method = 'GET',
public static function createClient(
$user = null,
$password = null,
$headers = null,
$body = null,
$config = null,
$cookies = null,
$stream = false,
$timeout = 0,
$client = null
$timeout = 0
) {
if ($client === null) {
$client = new Client();
}

$options = [];
if ($user !== null) {
$options['auth'] = [$user, $password];
}
if ($body !== null) {
$options['body'] = $body;
}
if ($config !== null) {
$options['config'] = $config;
}
Expand All @@ -171,16 +155,44 @@ public static function createRequest(
$options['stream'] = $stream;
$options['verify'] = false;
$options['timeout'] = $timeout;
$request = $client->createRequest($method, $url, $options);
if ($headers !== null) {
foreach ($headers as $key => $value) {
if ($request->hasHeader($key) === true) {
$request->setHeader($key, $value);
} else {
$request->addHeader($key, $value);
}
}
$client = new Client($options);
return $client;
}

/**
* Create an http request based on given parameters.
* This creates a RequestInterface object that can be used with a client to send a request.
* This enables us to create multiple requests in advance so that we can send them to the server at once in parallel.
*
* @param string $url
* @param string $method
* @param array $headers ['X-MyHeader' => 'value']
* @param string|array $body either the actual string to send in the body,
* or an array of key-value pairs to be converted
* into a body with http_build_query.
*
* @return RequestInterface
*/
public static function createRequest(
$url,
$method = 'GET',
$headers = null,
$body = null
) {
if ($headers === null) {
$headers = [];
}
if (\is_array($body)) {
// when creating the client, it is possible to set 'form_params' and
// the Client constructor sorts out doing this http_build_query stuff.
// But 'new Request' does not have the flexibility to do that.
// So we need to do it here.
$body = \http_build_query($body, '', '&');
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
$request = new Request(
$method, $url, $headers, $body
);
return $request;
}

Expand Down Expand Up @@ -320,7 +332,9 @@ public static function delete(
* @return SimpleXMLElement
*/
public static function getResponseXml($response) {
return $response->xml();
// rewind just to make sure we can re-parse it in case it was parsed already...
$response->getBody()->rewind();
return new SimpleXMLElement($response->getBody()->getContents());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestHelpers/MoveCopyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
namespace TestHelpers;

use GuzzleHttp\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Helper for move and copy files
Expand Down
18 changes: 4 additions & 14 deletions tests/TestHelpers/OcsApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
namespace TestHelpers;

use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Client;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Helper to make requests to the OCS API
Expand Down Expand Up @@ -59,19 +59,16 @@ public static function sendRequest(
/**
*
* @param string $baseUrl
* @param string $user if set to null no authentication header will be sent
* @param string $password
* @param string $method HTTP Method
* @param string $path
* @param array $body array of key, value pairs e.g ['value' => 'yes']
* @param Client|null $client
* @param int $ocsApiVersion (1|2) default 2
* @param array $headers
*
* @return RequestInterface
*/
public static function createOcsRequest(
$baseUrl, $user, $password, $method, $path, $body = [], $client = null, $ocsApiVersion = 2, $headers = []
$baseUrl, $method, $path, $body = [], $ocsApiVersion = 2, $headers = []
) {
$fullUrl = $baseUrl;
if (\substr($fullUrl, -1) !== '/') {
Expand All @@ -81,15 +78,8 @@ public static function createOcsRequest(
return HttpRequestHelper::createRequest(
$fullUrl,
$method,
$user,
$password,
$headers,
$body,
null,
null,
null,
null,
$client
$body
);
}
}
9 changes: 5 additions & 4 deletions tests/TestHelpers/SetupHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Behat\Testwork\Hook\Scope\HookScope;
use GuzzleHttp\Exception\ServerException;
use Exception;
use GuzzleHttp\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface;
use SimpleXMLElement;

/**
Expand Down Expand Up @@ -681,9 +681,10 @@ public static function runOcc(
}

$return = [];
$return['code'] = $result->xml()->xpath("//ocs/data/code");
$return['stdOut'] = $result->xml()->xpath("//ocs/data/stdOut");
$return['stdErr'] = $result->xml()->xpath("//ocs/data/stdErr");
$resultXml = \simplexml_load_string($result->getBody()->getContents());
$return['code'] = $resultXml->xpath("//ocs/data/code");
$return['stdOut'] = $resultXml->xpath("//ocs/data/stdOut");
$return['stdErr'] = $resultXml->xpath("//ocs/data/stdErr");

if (!isset($return['code'][0])) {
throw new Exception(
Expand Down
2 changes: 1 addition & 1 deletion tests/TestHelpers/SharingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
namespace TestHelpers;

use GuzzleHttp\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface;

/**
* manage Shares via OCS API
Expand Down
2 changes: 1 addition & 1 deletion tests/TestHelpers/TagsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
namespace TestHelpers;

use GuzzleHttp\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface;
use Exception;
use SimpleXMLElement;

Expand Down
Loading