From 1a7f60b679afbea96d57a45e9eb7389422ee8247 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Mon, 18 Aug 2014 11:45:53 +0200 Subject: [PATCH] [SoapClient] Added proxy authentication option in SoapClientBuilder Fixed #47 --- src/BeSimple/SoapClient/Curl.php | 4 ++++ src/BeSimple/SoapClient/SoapClientBuilder.php | 11 ++++++++++- .../SoapClient/Tests/SoapClientBuilderTest.php | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/BeSimple/SoapClient/Curl.php b/src/BeSimple/SoapClient/Curl.php index 272f717..e777fc8 100644 --- a/src/BeSimple/SoapClient/Curl.php +++ b/src/BeSimple/SoapClient/Curl.php @@ -92,6 +92,10 @@ class Curl if (false !== $proxyHost && isset($options['proxy_login'])) { curl_setopt($this->ch, CURLOPT_PROXYUSERPWD, $options['proxy_login'].':'.$options['proxy_password']); + + if (isset($options['proxy_auth'])) { + curl_setopt($this->ch, CURLOPT_PROXYAUTH, $options['proxy_auth']); + } } } diff --git a/src/BeSimple/SoapClient/SoapClientBuilder.php b/src/BeSimple/SoapClient/SoapClientBuilder.php index 910d718..59b5bba 100644 --- a/src/BeSimple/SoapClient/SoapClientBuilder.php +++ b/src/BeSimple/SoapClient/SoapClientBuilder.php @@ -177,10 +177,11 @@ class SoapClientBuilder extends AbstractSoapBuilder * @param int $port Port * @param string $login Login * @param string $password Password + * @param int $auth Authentication method * * @return \BeSimple\SoapClient\SoapClientBuilder */ - public function withProxy($host, $port, $login = null, $password = null) + public function withProxy($host, $port, $login = null, $password = null, $auth = null) { $this->soapOptions['proxy_host'] = $host; $this->soapOptions['proxy_port'] = $port; @@ -188,6 +189,14 @@ class SoapClientBuilder extends AbstractSoapBuilder if ($login) { $this->soapOptions['proxy_login'] = $login; $this->soapOptions['proxy_password'] = $password; + + if ($auth) { + if (!in_array($auth, array(\CURLAUTH_BASIC, \CURLAUTH_NTLM), true)) { + throw new \InvalidArgumentException('Invalid authentication method: CURLAUTH_BASIC or CURLAUTH_NTLM constants are availables.'); + } + + $this->soapOptions['proxy_auth'] = $auth; + } } return $this; diff --git a/src/BeSimple/SoapClient/Tests/SoapClientBuilderTest.php b/src/BeSimple/SoapClient/Tests/SoapClientBuilderTest.php index a0ebfeb..e9aecfb 100644 --- a/src/BeSimple/SoapClient/Tests/SoapClientBuilderTest.php +++ b/src/BeSimple/SoapClient/Tests/SoapClientBuilderTest.php @@ -96,6 +96,20 @@ class SoapClientBuilderTest extends \PHPUnit_Framework_TestCase $builder->withProxy('127.0.0.1', 8585, 'foo', 'bar'); $this->assertEquals($this->mergeOptions(array('proxy_host' => '127.0.0.1', 'proxy_port' => 8585, 'proxy_login' => 'foo', 'proxy_password' => 'bar')), $builder->getSoapOptions()); + + $builder->withProxy('127.0.0.1', 8585, 'foo', 'bar', \CURLAUTH_BASIC); + $this->assertEquals($this->mergeOptions(array('proxy_host' => '127.0.0.1', 'proxy_port' => 8585, 'proxy_login' => 'foo', 'proxy_password' => 'bar', 'proxy_auth' => \CURLAUTH_BASIC)), $builder->getSoapOptions()); + + $builder->withProxy('127.0.0.1', 8585, 'foo', 'bar', \CURLAUTH_NTLM); + $this->assertEquals($this->mergeOptions(array('proxy_host' => '127.0.0.1', 'proxy_port' => 8585, 'proxy_login' => 'foo', 'proxy_password' => 'bar', 'proxy_auth' => \CURLAUTH_NTLM)), $builder->getSoapOptions()); + + try { + $builder->withProxy('127.0.0.1', 8585, 'foo', 'bar', -100); + + $this->fail('An expected exception has not been raised.'); + } catch (\Exception $e) { + $this->assertInstanceOf('InvalidArgumentException', $e); + } } public function testCreateWithDefaults()