Skip to content

Commit

Permalink
ption 'protocolVersion' added to Request::options
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Jul 25, 2016
1 parent eb6b380 commit 83599c3
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ Yii Framework 2 HTTP client extension Change Log
- Enh #43: Events `EVENT_BEFORE_SEND` and `EVENT_AFTER_SEND` added to `Request` and `Client` (klimov-paul)
- Enh #46: Added `Request::getFullUrl()` allowing getting the full actual request URL (klimov-paul)
- Enh #47: Added `Message::addData()` allowing addition of the content data to already existing one (klimov-paul)
- Enh #50: Option 'protocolVersion' added to `Request::options` allowing specification of the HTTP protocol version (klimov-paul)
- Enh: Added `XmlFormatter::useTraversableAsArray` allowing processing `\Traversable` as array (klimov-paul)


Expand Down
1 change: 1 addition & 0 deletions CurlTransport.php
Expand Up @@ -186,6 +186,7 @@ private function initCurl(array $curlOptions)
private function composeCurlOptions(array $options)
{
static $optionMap = [
'protocolVersion' => CURLOPT_HTTP_VERSION,
'maxRedirects' => CURLOPT_MAXREDIRS,
'sslCapath' => CURLOPT_CAPATH,
'sslCafile' => CURLOPT_CAINFO,
Expand Down
1 change: 1 addition & 0 deletions Request.php
Expand Up @@ -109,6 +109,7 @@ public function getMethod()
* - userAgent: string, the contents of the "User-Agent: " header to be used in a HTTP request.
* - followLocation: boolean, whether to follow any "Location: " header that the server sends as part of the HTTP header.
* - maxRedirects: integer, the max number of redirects to follow.
* - protocolVersion: float|string, HTTP protocol version.
* - sslVerifyPeer: boolean, whether verification of the peer's certificate should be performed.
* - sslCafile: string, location of Certificate Authority file on local filesystem which should be used with
* the 'sslVerifyPeer' option to authenticate the identity of the remote peer.
Expand Down
31 changes: 31 additions & 0 deletions tests/CurlTransportTest.php
Expand Up @@ -16,4 +16,35 @@ protected function transport()
{
return CurlTransport::className();
}

public function testComposeCurlOptions()
{
$transport = $this->createClient()->getTransport();

$options = [
'protocolVersion' => '1.1',
'timeout' => 12,
'proxy' => 'tcp://proxy.example.com:5100',
'userAgent' => 'Test User Agent',
'followLocation' => true,
'maxRedirects' => 11,
'sslVerifyPeer' => true,
'sslCafile' => '/path/to/some/file',
'sslCapath' => '/some/path',
];
$contextOptions = $this->invoke($transport, 'composeCurlOptions', [$options]);

$expectedContextOptions = [
CURLOPT_HTTP_VERSION => $options['protocolVersion'],
CURLOPT_TIMEOUT => $options['timeout'],
CURLOPT_PROXY => $options['proxy'],
CURLOPT_USERAGENT => $options['userAgent'],
CURLOPT_FOLLOWLOCATION => $options['followLocation'],
CURLOPT_MAXREDIRS => $options['maxRedirects'],
CURLOPT_SSL_VERIFYPEER => $options['sslVerifyPeer'],
CURLOPT_CAINFO => $options['sslCafile'],
CURLOPT_CAPATH => $options['sslCapath'],
];
$this->assertEquals($expectedContextOptions, $contextOptions);
}
}
35 changes: 35 additions & 0 deletions tests/StreamTransportTest.php
Expand Up @@ -28,4 +28,39 @@ public function testFollowLocation()

parent::testFollowLocation();
}

public function testComposeContextOptions()
{
$transport = $this->createClient()->getTransport();

$options = [
'protocolVersion' => '1.1',
'timeout' => 12,
'proxy' => 'tcp://proxy.example.com:5100',
'userAgent' => 'Test User Agent',
'followLocation' => true,
'maxRedirects' => 11,
'sslVerifyPeer' => true,
'sslCafile' => '/path/to/some/file',
'sslCapath' => '/some/path',
];
$contextOptions = $this->invoke($transport, 'composeContextOptions', [$options]);

$expectedContextOptions = [
'http' => [
'protocol_version' => $options['protocolVersion'],
'timeout' => $options['timeout'],
'proxy' => $options['proxy'],
'user_agent' => $options['userAgent'],
'follow_location' => $options['followLocation'],
'max_redirects' => $options['maxRedirects'],
],
'ssl' => [
'verify_peer' => $options['sslVerifyPeer'],
'cafile' => $options['sslCafile'],
'capath' => $options['sslCapath'],
],
];
$this->assertEquals($expectedContextOptions, $contextOptions);
}
}
17 changes: 17 additions & 0 deletions tests/TestCase.php
Expand Up @@ -73,4 +73,21 @@ public function assertEqualsWithoutLE($expected, $actual)
$actual = str_replace(["\r", "\n"], '', $actual);
$this->assertEquals($expected, $actual);
}

/**
* Invokes object method, even if it is private or protected.
* @param object $object object.
* @param string $method method name.
* @param array $args method arguments
* @return mixed method result
*/
protected function invoke($object, $method, array $args = [])
{
$classReflection = new \ReflectionClass(get_class($object));
$methodReflection = $classReflection->getMethod($method);
$methodReflection->setAccessible(true);
$result = $methodReflection->invokeArgs($object, $args);
$methodReflection->setAccessible(false);
return $result;
}
}

0 comments on commit 83599c3

Please sign in to comment.