From 0f337dd56af136535157da02745b294311654bf0 Mon Sep 17 00:00:00 2001 From: Fernando Mantoan Date: Wed, 5 Dec 2012 12:22:24 -0200 Subject: [PATCH] Solving the issue #1387 --- src/Composer/Util/StreamContextFactory.php | 11 +++++++++-- .../Test/Util/StreamContextFactoryTest.php | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Composer/Util/StreamContextFactory.php b/src/Composer/Util/StreamContextFactory.php index 5c335ce9c..590bed581 100644 --- a/src/Composer/Util/StreamContextFactory.php +++ b/src/Composer/Util/StreamContextFactory.php @@ -67,12 +67,19 @@ final class StreamContextFactory $auth .= ':' . $proxy['pass']; } $auth = base64_encode($auth); + $authHeader = "Proxy-Authorization: Basic {$auth}"; // Preserve headers if already set in default options if (isset($defaultOptions['http']['header'])) { - $defaultOptions['http']['header'][] = "Proxy-Authorization: Basic {$auth}"; + if (is_array($defaultOptions['http']['header'])) + $defaultOptions['http']['header'][] = $authHeader; + else + $defaultOptions['http']['header'] = array( + $defaultOptions['http']['header'], + $authHeader + ); } else { - $options['http']['header'] = array("Proxy-Authorization: Basic {$auth}"); + $options['http']['header'] = array($authHeader); } } } diff --git a/tests/Composer/Test/Util/StreamContextFactoryTest.php b/tests/Composer/Test/Util/StreamContextFactoryTest.php index 3d728ffeb..5d7969660 100644 --- a/tests/Composer/Test/Util/StreamContextFactoryTest.php +++ b/tests/Composer/Test/Util/StreamContextFactoryTest.php @@ -126,6 +126,20 @@ class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase } } + public function testHttpProxyWithContentTypeHeader() { + $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net'; + + $context = StreamContextFactory::getContext(array('http' => array('method' => 'GET', 'header' => 'Content-Type: application/json'))); + $options = stream_context_get_options($context); + + $this->assertEquals(array('http' => array( + 'proxy' => 'tcp://proxyserver.net:80', + 'request_fulluri' => true, + 'method' => 'GET', + 'header' => array("Content-Type: application/json", "Proxy-Authorization: Basic " . base64_encode('username:password')) + )), $options); + } + public function dataSSLProxy() { return array(