Skip to content

Commit d2009a7

Browse files
author
Kyra Farrow
committed
[HttpClient] Add tests - update code style nits.
1 parent c5de051 commit d2009a7

File tree

7 files changed

+49
-15
lines changed

7 files changed

+49
-15
lines changed

CachingHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function request(string $method, string $url, array $options = []): Respo
7171
$url = implode('', $url);
7272
$options['extra']['no_cache'] = $options['extra']['no_cache'] ?? !$options['buffer'];
7373

74-
if ($options['extra']['no_cache'] || !empty($options['body']) || !\in_array($method, ['GET', 'HEAD', 'OPTIONS'])) {
74+
if (!empty($options['body']) || $options['extra']['no_cache'] || !\in_array($method, ['GET', 'HEAD', 'OPTIONS'])) {
7575
return $this->client->request($method, $url, $options);
7676
}
7777

CurlHttpClient.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ final class CurlHttpClient implements HttpClientInterface, LoggerAwareInterface
4848
*/
4949
public function __construct(array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50)
5050
{
51+
if (!\extension_loaded('curl')) {
52+
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\CurlHttpClient" as the "curl" extension is not installed.');
53+
}
54+
5155
if ($defaultOptions) {
5256
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, self::OPTIONS_DEFAULTS);
5357
}
@@ -109,7 +113,7 @@ public function request(string $method, string $url, array $options = []): Respo
109113
$options['headers']['range'] ?? null,
110114
];
111115

112-
if ('GET' === $method && !$options['body'] && $expectedHeaders === $pushedHeaders) {
116+
if ('GET' === $method && $expectedHeaders === $pushedHeaders && !$options['body']) {
113117
$this->logger && $this->logger->debug(sprintf('Connecting request to pushed response: "%s %s"', $method, $url));
114118

115119
// Reinitialize the pushed response with request's options

HttpClientTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\HttpClient;
1313

1414
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
15-
use Symfony\Contracts\HttpClient\HttpClientInterface;
1615

1716
/**
1817
* Provides the common logic from writing HttpClientInterface implementations.
@@ -278,7 +277,7 @@ private static function normalizePeerFingerprint($fingerprint): array
278277
$fingerprint[$algo] = 'pin-sha256' === $algo ? (array) $hash : str_replace(':', '', $hash);
279278
}
280279
} else {
281-
throw new InvalidArgumentException(sprintf('Option "peer_fingerprint" must be string or array, %s given.', \gettype($body)));
280+
throw new InvalidArgumentException(sprintf('Option "peer_fingerprint" must be string or array, %s given.', \gettype($fingerprint)));
282281
}
283282

284283
return $fingerprint;

HttpOptions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function toArray(): array
3737
public function setAuthBasic(string $user, string $password = '')
3838
{
3939
$this->options['auth_basic'] = $user;
40+
4041
if ('' !== $password) {
4142
$this->options['auth_basic'] .= ':'.$password;
4243
}

MockHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct($responseFactory = null, string $baseUri = null)
3939
$responseFactory = [$responseFactory];
4040
}
4141

42-
if (null !== $responseFactory && !\is_callable($responseFactory) && !$responseFactory instanceof \Iterator) {
42+
if (!$responseFactory instanceof \Iterator && null !== $responseFactory && !\is_callable($responseFactory)) {
4343
$responseFactory = (static function () use ($responseFactory) {
4444
yield from $responseFactory;
4545
})();

NativeHttpClient.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -400,17 +400,9 @@ private static function configureHeadersAndProxy($context, string $host, array $
400400
// Matching "no_proxy" should follow the behavior of curl
401401

402402
foreach ($noProxy as $rule) {
403-
if ('*' === $rule) {
404-
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
405-
}
406-
407-
if ($host === $rule) {
408-
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
409-
}
410-
411-
$rule = '.'.ltrim($rule, '.');
403+
$dotRule = '.'.ltrim($rule, '.');
412404

413-
if (substr($host, -\strlen($rule)) === $rule) {
405+
if ('*' === $rule || $host === $rule || substr($host, -\strlen($dotRule)) === $dotRule) {
414406
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
415407
}
416408
}

Tests/HttpClientTraitTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,42 @@ public function testPrepareAuthBasic($arg, $result)
215215
[, $options] = $this->prepareRequest('POST', 'http://example.com', ['auth_basic' => $arg], HttpClientInterface::OPTIONS_DEFAULTS);
216216
$this->assertSame('Basic '.$result, $options['headers']['authorization'][0]);
217217
}
218+
219+
public function provideFingerprints()
220+
{
221+
foreach (['md5', 'sha1', 'sha256'] as $algo) {
222+
$hash = \hash($algo, $algo);
223+
yield [$hash, [$algo => $hash]];
224+
}
225+
226+
yield ['AAAA:BBBB:CCCC:DDDD:EEEE:FFFF:GGGG:HHHH:IIII:JJJJ:KKKK', ['pin-sha256' => ['AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKK']]];
227+
}
228+
229+
/**
230+
* @dataProvider provideFingerprints
231+
*/
232+
public function testNormalizePeerFingerprint($fingerprint, $expected)
233+
{
234+
self::assertSame($expected, $this->normalizePeerFingerprint($fingerprint));
235+
}
236+
237+
/**
238+
* @expectedException \Symfony\Component\HttpClient\Exception\InvalidArgumentException
239+
* @expectedExceptionMessage Cannot auto-detect fingerprint algorithm for "foo".
240+
*/
241+
public function testNormalizePeerFingerprintException()
242+
{
243+
$this->normalizePeerFingerprint('foo');
244+
}
245+
246+
/**
247+
* @expectedException \Symfony\Component\HttpClient\Exception\InvalidArgumentException
248+
* @expectedExceptionMessage Option "peer_fingerprint" must be string or array, object given.
249+
*/
250+
public function testNormalizePeerFingerprintTypeException()
251+
{
252+
$fingerprint = new \stdClass();
253+
254+
$this->normalizePeerFingerprint($fingerprint);
255+
}
218256
}

0 commit comments

Comments
 (0)