[HttpClient] Add tests - update code style nits.

This commit is contained in:
Pol Dellaiera 2019-04-08 20:49:29 +02:00 committed by Nicolas Grekas
parent 6a5de47ed7
commit e77108d24e
7 changed files with 49 additions and 15 deletions

View File

@ -71,7 +71,7 @@ class CachingHttpClient implements HttpClientInterface
$url = implode('', $url);
$options['extra']['no_cache'] = $options['extra']['no_cache'] ?? !$options['buffer'];
if ($options['extra']['no_cache'] || !empty($options['body']) || !\in_array($method, ['GET', 'HEAD', 'OPTIONS'])) {
if (!empty($options['body']) || $options['extra']['no_cache'] || !\in_array($method, ['GET', 'HEAD', 'OPTIONS'])) {
return $this->client->request($method, $url, $options);
}

View File

@ -48,6 +48,10 @@ final class CurlHttpClient implements HttpClientInterface, LoggerAwareInterface
*/
public function __construct(array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50)
{
if (!\extension_loaded('curl')) {
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\CurlHttpClient" as the "curl" extension is not installed.');
}
if ($defaultOptions) {
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, self::OPTIONS_DEFAULTS);
}
@ -109,7 +113,7 @@ final class CurlHttpClient implements HttpClientInterface, LoggerAwareInterface
$options['headers']['range'] ?? null,
];
if ('GET' === $method && !$options['body'] && $expectedHeaders === $pushedHeaders) {
if ('GET' === $method && $expectedHeaders === $pushedHeaders && !$options['body']) {
$this->logger && $this->logger->debug(sprintf('Connecting request to pushed response: "%s %s"', $method, $url));
// Reinitialize the pushed response with request's options

View File

@ -12,7 +12,6 @@
namespace Symfony\Component\HttpClient;
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* Provides the common logic from writing HttpClientInterface implementations.
@ -278,7 +277,7 @@ trait HttpClientTrait
$fingerprint[$algo] = 'pin-sha256' === $algo ? (array) $hash : str_replace(':', '', $hash);
}
} else {
throw new InvalidArgumentException(sprintf('Option "peer_fingerprint" must be string or array, %s given.', \gettype($body)));
throw new InvalidArgumentException(sprintf('Option "peer_fingerprint" must be string or array, %s given.', \gettype($fingerprint)));
}
return $fingerprint;

View File

@ -37,6 +37,7 @@ class HttpOptions
public function setAuthBasic(string $user, string $password = '')
{
$this->options['auth_basic'] = $user;
if ('' !== $password) {
$this->options['auth_basic'] .= ':'.$password;
}

View File

@ -39,7 +39,7 @@ class MockHttpClient implements HttpClientInterface
$responseFactory = [$responseFactory];
}
if (null !== $responseFactory && !\is_callable($responseFactory) && !$responseFactory instanceof \Iterator) {
if (!$responseFactory instanceof \Iterator && null !== $responseFactory && !\is_callable($responseFactory)) {
$responseFactory = (static function () use ($responseFactory) {
yield from $responseFactory;
})();

View File

@ -400,17 +400,9 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac
// Matching "no_proxy" should follow the behavior of curl
foreach ($noProxy as $rule) {
if ('*' === $rule) {
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
}
$dotRule = '.'.ltrim($rule, '.');
if ($host === $rule) {
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
}
$rule = '.'.ltrim($rule, '.');
if (substr($host, -\strlen($rule)) === $rule) {
if ('*' === $rule || $host === $rule || substr($host, -\strlen($dotRule)) === $dotRule) {
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
}
}

View File

@ -215,4 +215,42 @@ class HttpClientTraitTest extends TestCase
[, $options] = $this->prepareRequest('POST', 'http://example.com', ['auth_basic' => $arg], HttpClientInterface::OPTIONS_DEFAULTS);
$this->assertSame('Basic '.$result, $options['headers']['authorization'][0]);
}
public function provideFingerprints()
{
foreach (['md5', 'sha1', 'sha256'] as $algo) {
$hash = \hash($algo, $algo);
yield [$hash, [$algo => $hash]];
}
yield ['AAAA:BBBB:CCCC:DDDD:EEEE:FFFF:GGGG:HHHH:IIII:JJJJ:KKKK', ['pin-sha256' => ['AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKK']]];
}
/**
* @dataProvider provideFingerprints
*/
public function testNormalizePeerFingerprint($fingerprint, $expected)
{
self::assertSame($expected, $this->normalizePeerFingerprint($fingerprint));
}
/**
* @expectedException \Symfony\Component\HttpClient\Exception\InvalidArgumentException
* @expectedExceptionMessage Cannot auto-detect fingerprint algorithm for "foo".
*/
public function testNormalizePeerFingerprintException()
{
$this->normalizePeerFingerprint('foo');
}
/**
* @expectedException \Symfony\Component\HttpClient\Exception\InvalidArgumentException
* @expectedExceptionMessage Option "peer_fingerprint" must be string or array, object given.
*/
public function testNormalizePeerFingerprintTypeException()
{
$fingerprint = new \stdClass();
$this->normalizePeerFingerprint($fingerprint);
}
}