diff --git a/src/Traits/Request.php b/src/Traits/Request.php index 91e2e2d..aa8d172 100644 --- a/src/Traits/Request.php +++ b/src/Traits/Request.php @@ -46,6 +46,7 @@ public function getFollowLocation(): int } /** + * 重定向次数 * @param int $followLocation * @return $this */ @@ -56,6 +57,7 @@ public function setFollowLocation(int $followLocation) } /** + * 记录重定向多少次 * @return int */ public function getRedirected(): int @@ -98,6 +100,7 @@ public function getCookies(): array } /** + * 设置cookie * @param array $cookies * @return $this */ @@ -107,7 +110,12 @@ public function setCookies(array $cookies) return $this; } - + /** + * @param array $header + * @param bool $isMerge + * @param bool $strtolower + * @return $this + */ public function setHeaders(array $header, $isMerge = true, $strtolower = true) { if (empty($header)) { @@ -125,7 +133,11 @@ public function setHeaders(array $header, $isMerge = true, $strtolower = true) return $this; } - + /** + * @param string $userName + * @param string $password + * @return $this + */ public function setBasicAuth(string $userName, string $password) { $basicAuthToken = base64_encode("{$userName}:{$password}"); @@ -173,6 +185,56 @@ public function addCookies(array $cookies, $isMerge = true) return $this; } + /** + * 设置为Xml请求 + * @return $this + */ + public function setContentTypeXml() + { + $this->setContentType(HttpClient::CONTENT_TYPE_APPLICATION_XML); + return $this; + } + + /** + * 设置为FromData请求 + * @return $this + */ + public function setContentTypeFormData() + { + $this->setContentType(HttpClient::CONTENT_TYPE_FORM_DATA); + return $this; + } + + /** + * 设置为FromUrlencoded请求 + * @return $this + */ + public function setContentTypeFormUrlencoded() + { + $this->setContentType(HttpClient::CONTENT_TYPE_X_WWW_FORM_URLENCODED); + return $this; + } + + /** + * 设置为XMLHttpRequest请求 + * @return $this + */ + public function setXMLHttpRequest() + { + $this->setHeader('x-requested-with', 'xmlhttprequest'); + return $this; + } + + /** + * 设置为Json请求 + * @return $this + */ + public function setContentTypeJson() + { + $this->setContentType(HttpClient::CONTENT_TYPE_APPLICATION_JSON); + return $this; + } + /** * 协程客户端设置项 * @var array @@ -187,37 +249,67 @@ public function getClientSetting(): array return $this->clientSetting; } - + /** + * 总超时,包括连接、发送、接收所有超时 + * @param float $timeout + * @return $this + */ public function setTimeout(float $timeout) { $this->clientSetting['timeout'] = $timeout; return $this; } + /** + * 连接超时,会覆盖第一个总的 timeout + * @param float $connectTimeout + * @return $this + */ public function setConnectTimeout(float $connectTimeout) { $this->clientSetting['connect_timeout'] = $connectTimeout; return $this; } + /** + * 接收超时,会覆盖第一个总的 timeout + * @param float $readTimeout + * @return $this + */ public function setReadTimeout(float $readTimeout) { $this->clientSetting['read_timeout'] = $readTimeout; return $this; } + /** + * 发送超时,会覆盖第一个总的 timeout + * @param float $writeTimeout + * @return $this + */ public function setWriteTimeout(float $writeTimeout) { $this->clientSetting['write_timeout'] = $writeTimeout; return $this; } + /** + * 长连接 + * @param bool $keepAlive + * @return $this + */ public function setKeepAlive(bool $keepAlive = true) { $this->clientSetting['keep_alive'] = $keepAlive; return $this; } + /** + * 验证服务端证书 + * @param bool $sslVerifyPeer + * @param false $sslAllowSelfSigned 允许自签名证书 + * @return $this + */ public function setSslVerifyPeer(bool $sslVerifyPeer = true, $sslAllowSelfSigned = false) { $this->clientSetting['ssl_verify_peer'] = $sslVerifyPeer; @@ -225,36 +317,80 @@ public function setSslVerifyPeer(bool $sslVerifyPeer = true, $sslAllowSelfSigned return $this; } + /** + * 设置服务器主机名称 + * @param string $sslHostName + * @return $this + */ public function setSslHostName(string $sslHostName) { $this->clientSetting['ssl_host_name'] = $sslHostName; return $this; } + /** + * 设置 ssl_verify_peer 为 true 时,用来验证远端证书所用到的 CA 证书。本选项值为 CA 证书在本地文件系统的全路径及文件名 + * @param string $sslCafile + * @return $this + */ public function setSslCafile(string $sslCafile) { $this->clientSetting['ssl_cafile'] = $sslCafile; return $this; } + /** + * 如果未设置 ssl_cafile,或者 ssl_cafile 所指的文件不存在时,会在 ssl_capath 所指定的目录搜索适用的证书。该目录必须是已经经过哈希处理的证书目录。 + * @param string $sslCapath + * @return $this + */ public function setSslCapath(string $sslCapath) { $this->clientSetting['ssl_capath'] = $sslCapath; return $this; } + /** + * ssl cert + * @param string $sslCertFile + * @return $this + */ public function setSslCertFile(string $sslCertFile) { $this->clientSetting['ssl_cert_file'] = $sslCertFile; return $this; } + /** + * ssl key + * @param string $sslKeyFile + * @return $this + */ public function setSslKeyFile(string $sslKeyFile) { $this->clientSetting['ssl_key_file'] = $sslKeyFile; return $this; } + /** + * 本地证书 ssl_cert_file 文件的密码 + * @param $sslPassphrase + * @return $this + */ + public function setSslPassphrase($sslPassphrase) + { + $this->clientSetting['ssl_passphrase'] = $sslPassphrase; + return $this; + } + + /** + * http_proxy + * @param string $proxyHost + * @param int $proxyPort + * @param string|null $proxyUser + * @param string|null $proxyPass + * @return $this + */ public function setProxyHttp(string $proxyHost, int $proxyPort, string $proxyUser = null, string $proxyPass = null) { $this->clientSetting['http_proxy_host'] = $proxyHost; @@ -270,6 +406,14 @@ public function setProxyHttp(string $proxyHost, int $proxyPort, string $proxyUse return $this; } + /** + * socket5 代理 + * @param string $proxyHost + * @param int $proxyPort + * @param string|null $proxyUser + * @param string|null $proxyPass + * @return $this + */ public function setProxySocks5(string $proxyHost, int $proxyPort, string $proxyUser = null, string $proxyPass = null) { $this->clientSetting['socks5_host'] = $proxyHost; @@ -285,6 +429,12 @@ public function setProxySocks5(string $proxyHost, int $proxyPort, string $proxyU return $this; } + /** + * 机器有多个网卡的情况下,设置 bind_address 参数可以强制客户端 Socket 绑定某个网络地址。设置 bind_port 可以使客户端 Socket 使用固定的端口连接到外网服务器。 + * @param string $bindAddress + * @param int $bindPort + * @return $this + */ public function setSocketBind(string $bindAddress, int $bindPort) { $this->clientSetting['bind_address'] = $bindAddress; @@ -309,54 +459,4 @@ public function setClientSettings(array $settings, $isMerge = true) } return $this; } - - /** - * 设置为Xml请求 - * @return $this - */ - public function setContentTypeXml() - { - $this->setContentType(HttpClient::CONTENT_TYPE_APPLICATION_XML); - return $this; - } - - /** - * 设置为FromData请求 - * @return $this - */ - public function setContentTypeFormData() - { - $this->setContentType(HttpClient::CONTENT_TYPE_FORM_DATA); - return $this; - } - - /** - * 设置为FromUrlencoded请求 - * @return $this - */ - public function setContentTypeFormUrlencoded() - { - $this->setContentType(HttpClient::CONTENT_TYPE_X_WWW_FORM_URLENCODED); - return $this; - } - - /** - * 设置为XMLHttpRequest请求 - * @return $this - */ - public function setXMLHttpRequest() - { - $this->setHeader('x-requested-with', 'xmlhttprequest'); - return $this; - } - - /** - * 设置为Json请求 - * @return $this - */ - public function setContentTypeJson() - { - $this->setContentType(HttpClient::CONTENT_TYPE_APPLICATION_JSON); - return $this; - } } diff --git a/tests/HttpTest.php b/tests/HttpTest.php index 46802a0..ee34fc9 100644 --- a/tests/HttpTest.php +++ b/tests/HttpTest.php @@ -2,7 +2,7 @@ namespace EasySwoole\HttpClient\Test; -use EasySwoole\HttpClient\Exception\InvalidUrl; +use EasySwoole\HttpClient\Bean\CURLFile; use EasySwoole\HttpClient\HttpClient; use PHPUnit\Framework\TestCase; use Swoole\WebSocket\Frame; @@ -19,16 +19,7 @@ function testGet() $client = new HttpClient($this->url); $client->setQuery(['arg2' => 3, 'q' => 2]); $response = $client->get(); - $json = json_decode($response->getBody(), true); - $this->assertEquals("GET", $json['REQUEST_METHOD']); - $this->assertEquals([], $json['POST']); - $this->assertEquals(['arg1' => 1, 'arg2' => 3, 'q' => 2], $json['GET']); - - - $client = new HttpClient(); - $client->setUrl($this->url); - $client->setQuery(['arg2' => 3, 'q' => 2]); - $response = $client->get(); + $this->assertEquals(200, $response->getStatusCode()); $json = json_decode($response->getBody(), true); $this->assertEquals("GET", $json['REQUEST_METHOD']); $this->assertEquals([], $json['POST']); @@ -39,13 +30,7 @@ function testHead() { $client = new HttpClient($this->url); $response = $client->head(); - $this->assertEquals('200', $response->getStatusCode()); - $this->assertEquals('', $response->getBody()); - - $client = new HttpClient(); - $client->setUrl($this->url); - $response = $client->head(); - $this->assertEquals('200', $response->getStatusCode()); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('', $response->getBody()); } @@ -54,14 +39,7 @@ function testDelete() $client = new HttpClient($this->url); $response = $client->delete(); $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); - $this->assertEquals("DELETE", $json['REQUEST_METHOD']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $response = $client->delete(); - $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals("DELETE", $json['REQUEST_METHOD']); } @@ -71,15 +49,7 @@ function testPut() $client = new HttpClient($this->url); $response = $client->put('testPut'); $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); - $this->assertEquals("PUT", $json['REQUEST_METHOD']); - $this->assertEquals("testPut", $json['RAW']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $response = $client->put('testPut'); - $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals("PUT", $json['REQUEST_METHOD']); $this->assertEquals("testPut", $json['RAW']); } @@ -90,16 +60,7 @@ function testPost() $response = $client->post([ 'post1' => 'post1' ]); - $json = json_decode($response->getBody(), true); - $this->assertEquals("POST", $json['REQUEST_METHOD']); - $this->assertEquals(['post1' => 'post1'], $json['POST']); - $this->assertEquals(['arg1' => 1, 'arg2' => 2], $json['GET']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $response = $client->post([ - 'post1' => 'post1' - ]); + $this->assertEquals(200, $response->getStatusCode()); $json = json_decode($response->getBody(), true); $this->assertEquals("POST", $json['REQUEST_METHOD']); $this->assertEquals(['post1' => 'post1'], $json['POST']); @@ -111,15 +72,7 @@ function testPatch() $client = new HttpClient($this->url); $response = $client->patch('testPath'); $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); - $this->assertEquals("PATCH", $json['REQUEST_METHOD']); - $this->assertEquals("testPath", $json['RAW']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $response = $client->patch('testPath'); - $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals("PATCH", $json['REQUEST_METHOD']); $this->assertEquals("testPath", $json['RAW']); } @@ -129,13 +82,7 @@ function testOptions() { $client = new HttpClient($this->url); $response = $client->options(['op' => 'op1'], ['head' => 'headtest']); - $json = json_decode($response->getBody(), true); - $this->assertEquals("OPTIONS", $json['REQUEST_METHOD']); - $this->assertEquals("headtest", $json['HEADER']['Head']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $response = $client->options(['op' => 'op1'], ['head' => 'headtest']); + $this->assertEquals(200, $response->getStatusCode()); $json = json_decode($response->getBody(), true); $this->assertEquals("OPTIONS", $json['REQUEST_METHOD']); $this->assertEquals("headtest", $json['HEADER']['Head']); @@ -147,12 +94,7 @@ function testPostXml() $client = new HttpClient($this->url); $response = $client->postXml(''); $json = json_decode($response->getBody(), true); - $this->assertEquals('', $json['RAW']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $response = $client->postXml(''); - $json = json_decode($response->getBody(), true); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('', $json['RAW']); } @@ -166,18 +108,7 @@ function testPostJson() $this->assertEquals(['arg1' => 1, 'arg2' => 2], $json['GET']); $raw = $json["RAW"]; $raw = json_decode($raw, true); - $this->assertEquals(['json' => 'json1'], $raw); - - - $client = new HttpClient(); - $client->setUrl($this->url); - $response = $client->postJson(json_encode(['json' => 'json1'])); - $json = json_decode($response->getBody(), true); - $this->assertEquals("POST", $json['REQUEST_METHOD']); - $this->assertEquals([], $json['POST']); - $this->assertEquals(['arg1' => 1, 'arg2' => 2], $json['GET']); - $raw = $json["RAW"]; - $raw = json_decode($raw, true); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(['json' => 'json1'], $raw); } @@ -185,14 +116,7 @@ function testDownload() { $client = new HttpClient('https://www.easyswoole.com/Images/docNavLogo.png'); $response = $client->download('./test.png'); - $this->assertEquals('200', $response->getStatusCode()); - $this->assertEquals(filesize('./test.png'), $response->getHeaders()['content-length']); - @unlink('./test.png'); - - $client = new HttpClient(); - $client->setUrl('https://www.easyswoole.com/Images/docNavLogo.png'); - $response = $client->download('./test.png'); - $this->assertEquals('200', $response->getStatusCode()); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(filesize('./test.png'), $response->getHeaders()['content-length']); @unlink('./test.png'); } @@ -201,14 +125,7 @@ function testPostString() { $client = new HttpClient($this->url); $response = $client->post('postStr'); - $json = json_decode($response->getBody(), true); - $this->assertEquals("POST", $json['REQUEST_METHOD']); - $this->assertEquals([], $json['POST']); - $this->assertEquals('postStr', $json['RAW']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $response = $client->post('postStr'); + $this->assertEquals(200, $response->getStatusCode()); $json = json_decode($response->getBody(), true); $this->assertEquals("POST", $json['REQUEST_METHOD']); $this->assertEquals([], $json['POST']); @@ -220,25 +137,16 @@ function testPostFile() $client = new HttpClient($this->url); $response = $client->post([ 'post1' => 'post1', - 'file' => new \CURLFile(__FILE__) - ]); - $json = json_decode($response->getBody(), true); - $this->assertEquals("POST", $json['REQUEST_METHOD']); - $this->assertEquals(['post1' => 'post1'], $json['POST']); - $this->assertEquals(['arg1' => 1, 'arg2' => 2], $json['GET']); - $this->assertEquals('HttpTest.php', $json['FILE']['file']['name']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $response = $client->post([ - 'post1' => 'post1', - 'file' => new \CURLFile(__FILE__) + 'file' => new \CURLFile(__FILE__), + 'file1' => new CURLFile(__FILE__, 'test-file') ]); $json = json_decode($response->getBody(), true); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals("POST", $json['REQUEST_METHOD']); $this->assertEquals(['post1' => 'post1'], $json['POST']); $this->assertEquals(['arg1' => 1, 'arg2' => 2], $json['GET']); $this->assertEquals('HttpTest.php', $json['FILE']['file']['name']); + $this->assertEquals('HttpTest.php', $json['FILE']['test-file']['name']); } function testSetHeaders() @@ -250,22 +158,9 @@ function testSetHeaders() ]); $response = $client->get(); $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); - $this->assertEquals("head1", $json['HEADER']['Head1']); - $this->assertEquals("head2", $json['HEADER']['Head2']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $client->setHeaders([ - 'head1' => 'head1', - 'head2' => 'head2' - ]); - $response = $client->get(); - $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals("head1", $json['HEADER']['Head1']); $this->assertEquals("head2", $json['HEADER']['Head2']); - } function testSetHeader() @@ -274,15 +169,7 @@ function testSetHeader() $client->setHeader('head1', 'head1'); $response = $client->get(); $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); - $this->assertEquals("head1", $json['HEADER']['Head1']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $client->setHeader('head1', 'head1'); - $response = $client->get(); - $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals("head1", $json['HEADER']['Head1']); } @@ -295,19 +182,7 @@ function testAddCookies() ]); $response = $client->get(); $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); - $this->assertEquals("cookie1", $json['COOKIE']['cookie1']); - $this->assertEquals("cookie2", $json['COOKIE']['cookie2']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $client->addCookies([ - 'cookie1' => 'cookie1', - 'cookie2' => 'cookie2' - ]); - $response = $client->get(); - $json = json_decode($response->getBody(), true); - $this->assertEquals('200', $response->getStatusCode()); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals("cookie1", $json['COOKIE']['cookie1']); $this->assertEquals("cookie2", $json['COOKIE']['cookie2']); } @@ -318,15 +193,7 @@ function testAddCookie() $client->addCookie('cookie1', 'cook'); $response = $client->get(['head' => 'head']); $json = json_decode($response->getBody(), true); - $this->assertEquals("GET", $json['REQUEST_METHOD']); - $this->assertEquals("head", $json['HEADER']['Head']); - $this->assertEquals("cook", $json['COOKIE']['cookie1']); - - $client = new HttpClient(); - $client->setUrl($this->url); - $client->addCookie('cookie1', 'cook'); - $response = $client->get(['head' => 'head']); - $json = json_decode($response->getBody(), true); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals("GET", $json['REQUEST_METHOD']); $this->assertEquals("head", $json['HEADER']['Head']); $this->assertEquals("cook", $json['COOKIE']['cookie1']); @@ -342,11 +209,11 @@ function testAddCookie() function testWebsocket() { $client = new HttpClient('127.0.0.1:9510'); - $client->setHeader('aaa','bbb'); + $client->setHeader('aaa', 'bbb'); $upgradeResult = $client->upgrade(true); $this->assertIsBool(true, $upgradeResult); $recvFrame = $client->recv(); - $this->assertEquals('bbb', json_decode($recvFrame->data,true)['aaa'] ?? ''); + $this->assertEquals('bbb', json_decode($recvFrame->data, true)['aaa'] ?? ''); $frame = new Frame(); $frame->data = json_encode(['action' => 'hello', 'content' => ['a' => 1]]); @@ -356,39 +223,15 @@ function testWebsocket() $recvFrame = $client->recv(); $this->assertIsBool(true, !!$recvFrame); $this->assertEquals('call hello with arg:{"a":1}', $recvFrame->data); - - $client = new HttpClient(); - $client->setUrl('127.0.0.1:9510'); - $client->addCookie('ca-1','cookie1'); - $upgradeResult = $client->upgrade(true); - $this->assertIsBool(true, $upgradeResult); - - - $recvFrame = $client->recv(); - $this->assertEquals('cookie1', json_decode($recvFrame->data,true)['ca-1'] ?? ''); - - $frame = new Frame(); - $frame->data = json_encode(['action' => 'hello', 'content' => ['a' => 1]]); - $pushResult = $client->push($frame); - $this->assertIsBool(true, $pushResult); - $recvFrame = $client->recv(); - $this->assertIsBool(true, !!$recvFrame); - $this->assertEquals('call hello with arg:{"a":1}', $recvFrame->data); } function testBasicAuth() { $httpClient = new HttpClient('127.0.0.1:9510'); $httpClient->setBasicAuth('admin', '111111'); - $res = $httpClient->post(); - $res = $res->getBody(); - $this->assertEquals('success', $res); - - $httpClient = new HttpClient(); - $httpClient->setUrl('127.0.0.1:9510'); - $httpClient->setBasicAuth('admin', '111111'); - $res = $httpClient->post(); - $res = $res->getBody(); + $response = $httpClient->post(); + $res = $response->getBody(); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('success', $res); } @@ -399,8 +242,6 @@ function testFollowLocation() $response = $httpClient->get(); $status = $response->getStatusCode(); $this->assertEquals(301, $status); - - $httpClient = new HttpClient('https://www.gaobinzhan.com/blog'); $response = $httpClient->get(); $status = $response->getStatusCode(); @@ -410,26 +251,6 @@ function testFollowLocation() $response = $httpClient->get(); $status = $response->getStatusCode(); $this->assertEquals(200, $status); - - - $httpClient = new HttpClient(); - $httpClient->setUrl('https://www.gaobinzhan.com/blog'); - $httpClient->enableFollowLocation(0); - $response = $httpClient->get(); - $status = $response->getStatusCode(); - $this->assertEquals(301, $status); - - - $httpClient = new HttpClient(); - $httpClient->setUrl('https://www.gaobinzhan.com/blog'); - $response = $httpClient->get(); - $status = $response->getStatusCode(); - $this->assertEquals(200, $status); - - $httpClient->enableFollowLocation(0); - $response = $httpClient->get(); - $status = $response->getStatusCode(); - $this->assertEquals(200, $status); } public function testSetPath() @@ -442,23 +263,6 @@ public function testSetPath() $res = $httpClient->get(); $res = $res->getBody(); $this->assertStringContainsString('admin@fosuss.com', $res); - - - $httpClient = new HttpClient(); - $httpClient->setUrl('https://www.easyswoole.com/demo.html'); - $res = $httpClient->get(); - $res = $res->getBody(); - $this->assertStringContainsString('基于EasySwoole V3 实现的聊天室', $res); - $httpClient->setPath('/Preface/intro.html'); - $httpClient->setQuery(['a' => 2]); - $res = $httpClient->get(); - $res = $res->getBody(); - $this->assertStringContainsString('admin@fosuss.com', $res); - $httpClient->setPath(); - $res = $httpClient->get(); - $res = $res->getBody(); - $this->assertStringContainsString('一种愉悦的开发方式', $res); - } public function testSetMethod() @@ -473,4 +277,26 @@ public function testSetMethod() $httpClient->setMethod('delete'); $this->assertEquals('delete', $httpClient->getClient()->requestMethod); } + + public function testHttpProxy() + { + $httpClient = new HttpClient('https://www.google.com'); + $response = $httpClient->get(); + $this->assertEquals(-1, $response->getStatusCode()); + + $httpClient->setProxyHttp('127.0.0.1', 1087); + $response = $httpClient->get(); + $this->assertEquals(200, $response->getStatusCode()); + } + + public function testSocketProxy() + { + $httpClient = new HttpClient('https://www.google.com'); + $response = $httpClient->get(); + $this->assertEquals(-1, $response->getStatusCode()); + + $httpClient->setProxySocks5('127.0.0.1', 1086); + $response = $httpClient->get(); + $this->assertEquals(200, $response->getStatusCode()); + } }