HTTP.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. class RequestsTest_Proxy_HTTP extends PHPUnit_Framework_TestCase {
  3. protected function checkProxyAvailable($type = '') {
  4. switch ($type) {
  5. case 'auth':
  6. $has_proxy = defined('REQUESTS_HTTP_PROXY_AUTH') && REQUESTS_HTTP_PROXY_AUTH;
  7. break;
  8. default:
  9. $has_proxy = defined('REQUESTS_HTTP_PROXY') && REQUESTS_HTTP_PROXY;
  10. break;
  11. }
  12. if (!$has_proxy) {
  13. $this->markTestSkipped('Proxy not available');
  14. }
  15. }
  16. public function transportProvider() {
  17. return array(
  18. array('Requests_Transport_cURL'),
  19. array('Requests_Transport_fsockopen'),
  20. );
  21. }
  22. /**
  23. * @dataProvider transportProvider
  24. */
  25. public function testConnectWithString($transport) {
  26. $this->checkProxyAvailable();
  27. $options = array(
  28. 'proxy' => REQUESTS_HTTP_PROXY,
  29. 'transport' => $transport,
  30. );
  31. $response = Requests::get(httpbin('/get'), array(), $options);
  32. $this->assertEquals('http', $response->headers['x-requests-proxied']);
  33. $data = json_decode($response->body, true);
  34. $this->assertEquals('http', $data['headers']['x-requests-proxy']);
  35. }
  36. /**
  37. * @dataProvider transportProvider
  38. */
  39. public function testConnectWithArray($transport) {
  40. $this->checkProxyAvailable();
  41. $options = array(
  42. 'proxy' => array(REQUESTS_HTTP_PROXY),
  43. 'transport' => $transport,
  44. );
  45. $response = Requests::get(httpbin('/get'), array(), $options);
  46. $this->assertEquals('http', $response->headers['x-requests-proxied']);
  47. $data = json_decode($response->body, true);
  48. $this->assertEquals('http', $data['headers']['x-requests-proxy']);
  49. }
  50. /**
  51. * @dataProvider transportProvider
  52. * @expectedException Requests_Exception
  53. */
  54. public function testConnectInvalidParameters($transport) {
  55. $this->checkProxyAvailable();
  56. $options = array(
  57. 'proxy' => array(REQUESTS_HTTP_PROXY, 'testuser', 'password', 'something'),
  58. 'transport' => $transport,
  59. );
  60. $response = Requests::get(httpbin('/get'), array(), $options);
  61. }
  62. /**
  63. * @dataProvider transportProvider
  64. */
  65. public function testConnectWithInstance($transport) {
  66. $this->checkProxyAvailable();
  67. $options = array(
  68. 'proxy' => new Requests_Proxy_HTTP(REQUESTS_HTTP_PROXY),
  69. 'transport' => $transport,
  70. );
  71. $response = Requests::get(httpbin('/get'), array(), $options);
  72. $this->assertEquals('http', $response->headers['x-requests-proxied']);
  73. $data = json_decode($response->body, true);
  74. $this->assertEquals('http', $data['headers']['x-requests-proxy']);
  75. }
  76. /**
  77. * @dataProvider transportProvider
  78. */
  79. public function testConnectWithAuth($transport) {
  80. $this->checkProxyAvailable('auth');
  81. $options = array(
  82. 'proxy' => array(
  83. REQUESTS_HTTP_PROXY_AUTH,
  84. REQUESTS_HTTP_PROXY_AUTH_USER,
  85. REQUESTS_HTTP_PROXY_AUTH_PASS
  86. ),
  87. 'transport' => $transport,
  88. );
  89. $response = Requests::get(httpbin('/get'), array(), $options);
  90. $this->assertEquals(200, $response->status_code);
  91. $this->assertEquals('http', $response->headers['x-requests-proxied']);
  92. $data = json_decode($response->body, true);
  93. $this->assertEquals('http', $data['headers']['x-requests-proxy']);
  94. }
  95. /**
  96. * @dataProvider transportProvider
  97. */
  98. public function testConnectWithInvalidAuth($transport) {
  99. $this->checkProxyAvailable('auth');
  100. $options = array(
  101. 'proxy' => array(
  102. REQUESTS_HTTP_PROXY_AUTH,
  103. REQUESTS_HTTP_PROXY_AUTH_USER . '!',
  104. REQUESTS_HTTP_PROXY_AUTH_PASS . '!'
  105. ),
  106. 'transport' => $transport,
  107. );
  108. $response = Requests::get(httpbin('/get'), array(), $options);
  109. $this->assertEquals(407, $response->status_code);
  110. }
  111. }