ProxyAdapterTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http_Client
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once dirname(__FILE__) . '/SocketTest.php';
  23. require_once 'Zend/Http/Client/Adapter/Proxy.php';
  24. /**
  25. * Zend_Http_Client_Adapter_Proxy test suite.
  26. *
  27. * In order to run, TESTS_ZEND_HTTP_CLIENT_HTTP_PROXY must point to a working
  28. * proxy server, which can access TESTS_ZEND_HTTP_CLIENT_BASEURI.
  29. *
  30. * See TestConfiguration.php.dist for more information.
  31. *
  32. * @category Zend
  33. * @package Zend_Http_Client
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Http
  38. * @group Zend_Http_Client
  39. */
  40. class Zend_Http_Client_ProxyAdapterTest extends Zend_Http_Client_SocketTest
  41. {
  42. /**
  43. * Configuration array
  44. *
  45. * @var array
  46. */
  47. protected function setUp()
  48. {
  49. if (defined('TESTS_ZEND_HTTP_CLIENT_HTTP_PROXY') &&
  50. TESTS_ZEND_HTTP_CLIENT_HTTP_PROXY) {
  51. list($host, $port) = explode(':', TESTS_ZEND_HTTP_CLIENT_HTTP_PROXY, 2);
  52. if (! $host)
  53. $this->markTestSkipped("No valid proxy host name or address specified.");
  54. $port = (int) $port;
  55. if ($port == 0) {
  56. $port = 8080;
  57. } else {
  58. if (($port < 1 || $port > 65535))
  59. $this->markTestSkipped("$port is not a valid proxy port number. Should be between 1 and 65535.");
  60. }
  61. $user = '';
  62. $pass = '';
  63. if (defined('TESTS_ZEND_HTTP_CLIENT_HTTP_PROXY_USER') &&
  64. TESTS_ZEND_HTTP_CLIENT_HTTP_PROXY_USER)
  65. $user = TESTS_ZEND_HTTP_CLIENT_HTTP_PROXY_USER;
  66. if (defined('TESTS_ZEND_HTTP_CLIENT_HTTP_PROXY_PASS') &&
  67. TESTS_ZEND_HTTP_CLIENT_HTTP_PROXY_PASS)
  68. $pass = TESTS_ZEND_HTTP_CLIENT_HTTP_PROXY_PASS;
  69. $this->config = array(
  70. 'adapter' => 'Zend_Http_Client_Adapter_Proxy',
  71. 'proxy_host' => $host,
  72. 'proxy_port' => $port,
  73. 'proxy_user' => $user,
  74. 'proxy_pass' => $pass,
  75. );
  76. parent::setUp();
  77. } else {
  78. $this->markTestSkipped("Zend_Http_Client proxy server tests are not enabled in TestConfiguration.php");
  79. }
  80. }
  81. /**
  82. * Test that when no proxy is set the adapter falls back to direct connection
  83. *
  84. */
  85. public function testFallbackToSocket()
  86. {
  87. $this->_adapter->setConfig(array(
  88. 'proxy_host' => null,
  89. ));
  90. $this->client->setUri($this->baseuri . 'testGetLastRequest.php');
  91. $res = $this->client->request(Zend_Http_Client::TRACE);
  92. if ($res->getStatus() == 405 || $res->getStatus() == 501) {
  93. $this->markTestSkipped("Server does not allow the TRACE method");
  94. }
  95. $this->assertEquals($this->client->getLastRequest(), $res->getBody(), 'Response body should be exactly like the last request');
  96. }
  97. public function testGetLastRequest()
  98. {
  99. /**
  100. * This test will never work for the proxy adapter (and shouldn't!)
  101. * because the proxy server modifies the request which is sent back in
  102. * the TRACE response
  103. */
  104. }
  105. /**
  106. * @group ZF-3189
  107. */
  108. public function testConnectHandshakeSendsCustomUserAgentHeader()
  109. {
  110. // Change the adapter
  111. $this->config['adapter'] = 'ZF3189_ProxyAdapter';
  112. $this->config['useragent'] = 'ZendTest';
  113. parent::setUp();
  114. $base = preg_replace("/^http:/", "https:", $this->baseuri);
  115. $this->client->setUri($base . 'testSimpleRequests.php');
  116. // Ensure we're proxying a HTTPS request
  117. $this->assertEquals('https', $this->client->getUri()->getScheme());
  118. // Perform the request
  119. $this->client->request();
  120. $this->assertRegExp(
  121. "/\r\nUser-Agent: {$this->config['useragent']}\r\n/i",
  122. $this->client->getAdapter()->getLastConnectHandshakeRequest()
  123. );
  124. }
  125. /**
  126. * @group ZF-3189
  127. */
  128. public function testConnectHandshakeSendsCustomUserAgentHeaderWhenSetInHeaders()
  129. {
  130. // Change the adapter
  131. $this->config['adapter'] = 'ZF3189_ProxyAdapter';
  132. parent::setUp();
  133. $base = preg_replace("/^http:/", "https:", $this->baseuri);
  134. $this->client->setUri($base . 'testSimpleRequests.php');
  135. $this->client->setHeaders('User-Agent', 'ZendTest');
  136. // Ensure we're proxying a HTTPS request
  137. $this->assertEquals('https', $this->client->getUri()->getScheme());
  138. // Perform the request
  139. $this->client->request();
  140. print_r($this->client->getAdapter()->getLastConnectHandshakeRequest());
  141. $this->assertRegExp(
  142. "/\r\nUser-Agent: ZendTest\r\n/i",
  143. $this->client->getAdapter()->getLastConnectHandshakeRequest()
  144. );
  145. }
  146. /**
  147. * @group ZF-3189
  148. */
  149. public function testProxyAdapterDoesNotOverwriteExistingProxyAuthorizationHeader()
  150. {
  151. // Change the adapter
  152. $this->config['adapter'] = 'ZF3189_ProxyAdapter';
  153. parent::setUp();
  154. $base = preg_replace("/^http:/", "https:", $this->baseuri);
  155. $this->client->setUri($base . 'testSimpleRequests.php');
  156. $this->client->setHeaders('Proxy-Authorization', 'FooBarBaz');
  157. // Ensure we're proxying a HTTPS request
  158. $this->assertEquals('https', $this->client->getUri()->getScheme());
  159. // Perform the request
  160. $this->client->request();
  161. print_r($this->client->getAdapter()->getLastConnectHandshakeRequest());
  162. $resp = $this->client->getAdapter()->getLastConnectHandshakeRequest();
  163. $this->assertEquals(1, preg_match_all('/\r\nProxy-Authorization: ([^\r\n]+)\r\n/i', $resp, $matches));
  164. $this->assertEquals('FooBarBaz', $matches[1][0]);
  165. }
  166. }
  167. /**
  168. * Exposes internal variable connectHandshakeRequest for test purposes
  169. * @see ZF-3189
  170. */
  171. class ZF3189_ProxyAdapter extends Zend_Http_Client_Adapter_Proxy
  172. {
  173. /**
  174. * Retrieve the request data from last CONNECT handshake
  175. * @return string
  176. */
  177. public function getLastConnectHandshakeRequest()
  178. {
  179. return $this->connectHandshakeRequest;
  180. }
  181. }