Skip to content

Commit

Permalink
Transport\Curl/Fsockopen::request_multiple(): add input validation
Browse files Browse the repository at this point in the history
This commit adds input validation to the `Curl::request_multiple()` and the `Fsockopen::request_multiple()` methods along the same lines as added to `Requests::request_multiple()`.

* For `$requests`, arrays and iterable objects with array access should be accepted based on how the parameter is used within this class.
* The `$options` parameter is validated to allow only an array - same as in `Requests::request()`.

Includes aligning the behaviour when receiving an empty `$requests` array between the `Curl` and the `Fsockopen` classes and returning an empty array in that case.

Includes adding perfunctory tests for the new exceptions.
  • Loading branch information
jrfnl committed Nov 16, 2021
1 parent b948ed8 commit f703215
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Transport/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,24 @@ public function request($url, $headers = [], $data = [], $options = []) {
* @param array $requests Request data
* @param array $options Global options
* @return array Array of \WpOrg\Requests\Response objects (may contain \WpOrg\Requests\Exception or string responses as well)
*
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $requests argument is not an array or iterable object with array access.
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array.
*/
public function request_multiple($requests, $options) {
// If you're not requesting, we can't get any responses ¯\_(ツ)_/¯
if (empty($requests)) {
return [];
}

if (InputValidator::has_array_access($requests) === false || InputValidator::is_iterable($requests) === false) {
throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests));
}

if (is_array($options) === false) {
throw InvalidArgument::create(2, '$options', 'array', gettype($options));
}

$multihandle = curl_multi_init();
$subrequests = [];
$subhandles = [];
Expand Down
16 changes: 16 additions & 0 deletions src/Transport/Fsockopen.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,24 @@ public function request($url, $headers = [], $data = [], $options = []) {
* @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see \WpOrg\Requests\Transport::request()}
* @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation
* @return array Array of \WpOrg\Requests\Response objects (may contain \WpOrg\Requests\Exception or string responses as well)
*
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $requests argument is not an array or iterable object with array access.
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array.
*/
public function request_multiple($requests, $options) {
// If you're not requesting, we can't get any responses ¯\_(ツ)_/¯
if (empty($requests)) {
return [];
}

if (InputValidator::has_array_access($requests) === false || InputValidator::is_iterable($requests) === false) {
throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests));
}

if (is_array($options) === false) {
throw InvalidArgument::create(2, '$options', 'array', gettype($options));
}

$responses = [];
$class = get_class($this);
foreach ($requests as $id => $request) {
Expand Down
83 changes: 83 additions & 0 deletions tests/Transport/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WpOrg\Requests\Tests\Transport;

use EmptyIterator;
use stdClass;
use WpOrg\Requests\Capability;
use WpOrg\Requests\Exception;
Expand Down Expand Up @@ -333,6 +334,88 @@ public function testRequestInvalidOptions($input) {
$transport->request('/', [], [], $input);
}

/**
* Tests receiving an exception when the request_multiple() method received an invalid input type as `$requests`.
*
* @dataProvider dataRequestMultipleReturnsEmptyArrayWhenRequestsIsEmpty
*
* @covers \WpOrg\Requests\Transport\Curl::request_multiple
* @covers \WpOrg\Requests\Transport\Fsockopen::request_multiple
*
* @param mixed $input Invalid parameter input.
*
* @return void
*/
public function testRequestMultipleReturnsEmptyArrayWhenRequestsIsEmpty($input) {
$transport = new $this->transport();
$this->assertSame([], $transport->request_multiple($input, $this->getOptions()));
}

/**
* Data Provider.
*
* @return array
*/
public function dataRequestMultipleReturnsEmptyArrayWhenRequestsIsEmpty() {
return [
'null' => [null],
'empty array' => [[]],
];
}

/**
* Tests receiving an exception when the request_multiple() method received an invalid input type as `$requests`.
*
* @dataProvider dataRequestMultipleInvalidRequests
*
* @covers \WpOrg\Requests\Transport\Curl::request_multiple
* @covers \WpOrg\Requests\Transport\Fsockopen::request_multiple
*
* @param mixed $input Invalid parameter input.
*
* @return void
*/
public function testRequestMultipleInvalidRequests($input) {
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('Argument #1 ($requests) must be of type array|ArrayAccess&Traversable');

$transport = new $this->transport();
$transport->request_multiple($input, $this->getOptions());
}

/**
* Data Provider.
*
* @return array
*/
public function dataRequestMultipleInvalidRequests() {
return [
'text string' => ['array'],
'iterator object without array access' => [new EmptyIterator()],
'array accessible object not iterable' => [new ArrayAccessibleObject([1, 2, 3])],
];
}

/**
* Tests receiving an exception when the request_multiple() method received an invalid input type as `$option`.
*
* @dataProvider dataInvalidTypeNotArray
*
* @covers \WpOrg\Requests\Transport\Curl::request_multiple
* @covers \WpOrg\Requests\Transport\Fsockopen::request_multiple
*
* @param mixed $input Invalid parameter input.
*
* @return void
*/
public function testRequestMultipleInvalidOptions($input) {
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('Argument #2 ($options) must be of type array');

$transport = new $this->transport();
$transport->request_multiple(['notempty'], $input);
}

/**
* Data Provider.
*
Expand Down

0 comments on commit f703215

Please sign in to comment.