ProxyAdapterTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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-2010 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-2010 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. }