HttpTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Http\RequestCore;
  4. use OSS\Http\ResponseCore;
  5. use OSS\Http\RequestCore_Exception;
  6. use Symfony\Component\Config\Definition\Exception\Exception;
  7. class HttpTest extends \PHPUnit_Framework_TestCase
  8. {
  9. public function testResponseCore()
  10. {
  11. $res = new ResponseCore(null, "", 500);
  12. $this->assertFalse($res->isOK());
  13. $this->assertTrue($res->isOK(500));
  14. }
  15. public function testGet()
  16. {
  17. $httpCore = new RequestCore("http://www.baidu.com");
  18. $httpResponse = $httpCore->send_request();
  19. $this->assertNotNull($httpResponse);
  20. }
  21. public function testSetProxyAndTimeout()
  22. {
  23. $httpCore = new RequestCore("http://www.baidu.com");
  24. $httpCore->set_proxy("1.0.2.1:8888");
  25. $httpCore->connect_timeout = 1;
  26. try {
  27. $httpResponse = $httpCore->send_request();
  28. $this->assertTrue(false);
  29. } catch (RequestCore_Exception $e) {
  30. }
  31. }
  32. public function testSendMultiRequest()
  33. {
  34. $httpCore = new RequestCore("http://www.baidu.com");
  35. $ch1 = curl_init("http://www.baidu.com");
  36. curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
  37. $ch2 = curl_init("http://cn.bing.com");
  38. curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
  39. @$result = $httpCore->send_multi_request(array($ch1, $ch2));
  40. $this->assertNotNull($result);
  41. }
  42. public function testGetParseTrue()
  43. {
  44. $httpCore = new RequestCore("http://www.baidu.com");
  45. $httpCore->curlopts = array(CURLOPT_HEADER => true);
  46. $url = $httpCore->send_request(true);
  47. foreach ($httpCore->get_response_header() as $key => $value) {
  48. $this->assertEquals($httpCore->get_response_header($key), $value);
  49. }
  50. $this->assertNotNull($url);
  51. }
  52. public function testParseResponse()
  53. {
  54. $httpCore = new RequestCore("http://www.baidu.com");
  55. $response = $httpCore->send_request();
  56. $parsed = $httpCore->process_response(null, $response);
  57. $this->assertNotNull($parsed);
  58. }
  59. public function testExceptionGet()
  60. {
  61. $httpCore = null;
  62. $exception = false;
  63. try {
  64. $httpCore = new RequestCore("http://www.notexistsitexx.com");
  65. $httpCore->set_body("");
  66. $httpCore->set_method("GET");
  67. $httpCore->connect_timeout = 10;
  68. $httpCore->timeout = 10;
  69. $res = $httpCore->send_request();
  70. } catch (RequestCore_Exception $e) {
  71. $exception = true;
  72. }
  73. $this->assertTrue($exception);
  74. }
  75. }