Skip to content

Commit

Permalink
Merge pull request #28 from gaobinzhan/master
Browse files Browse the repository at this point in the history
url
  • Loading branch information
kiss291323003 authored Jul 15, 2020
2 parents f38a116 + adc6603 commit ddd4c47
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use EasySwoole\HttpClient\Bean\Response;
use EasySwoole\HttpClient\Contract\ClientInterface;
use EasySwoole\HttpClient\Exception\InvalidUrl;
use Swoole\Coroutine\Http\Client;
use EasySwoole\HttpClient\Traits\UriManager;
use Swoole\WebSocket\Frame;

class HttpClient
Expand Down Expand Up @@ -45,13 +45,28 @@ class HttpClient
* HttpClient constructor.
* @param string|null $url
*/
public function __construct(string $url = null)
public function __construct(?string $url = null)
{
$this->clientHandler = new $this->clientHandler($url);
$this->setTimeout(3);
$this->setConnectTimeout(5);
}

// -------- 客户端配置设置方法 --------

public function setUrl($url): HttpClient
{
/** @see UriManager */
$this->clientHandler->setUrl($url);
return $this;
}

public function setEnableSSL(bool $enableSSL = true)
{
/** @see UriManager */
$this->clientHandler->setEnableSSL($enableSSL);
}

/**
* @return ClientInterface
*/
Expand Down
182 changes: 182 additions & 0 deletions tests/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ function testGet()
$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();
$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']);
}

function testHead()
Expand All @@ -31,6 +41,12 @@ function testHead()
$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('', $response->getBody());
}

function testDelete()
Expand All @@ -40,6 +56,13 @@ function testDelete()
$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("DELETE", $json['REQUEST_METHOD']);
}


Expand All @@ -51,6 +74,14 @@ function testPut()
$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("PUT", $json['REQUEST_METHOD']);
$this->assertEquals("testPut", $json['RAW']);
}

function testPost()
Expand All @@ -63,6 +94,16 @@ function testPost()
$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'
]);
$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']);
}

function testPatch()
Expand All @@ -73,6 +114,14 @@ function testPatch()
$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("PATCH", $json['REQUEST_METHOD']);
$this->assertEquals("testPath", $json['RAW']);
}


Expand All @@ -83,6 +132,13 @@ function testOptions()
$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']);
$json = json_decode($response->getBody(), true);
$this->assertEquals("OPTIONS", $json['REQUEST_METHOD']);
$this->assertEquals("headtest", $json['HEADER']['Head']);
}


Expand All @@ -92,6 +148,12 @@ function testPostXml()
$response = $client->postXml('<xml></xml>');
$json = json_decode($response->getBody(), true);
$this->assertEquals('<xml></xml>', $json['RAW']);

$client = new HttpClient();
$client->setUrl($this->url);
$response = $client->postXml('<xml></xml>');
$json = json_decode($response->getBody(), true);
$this->assertEquals('<xml></xml>', $json['RAW']);
}

function testPostJson()
Expand All @@ -105,6 +167,18 @@ function testPostJson()
$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(['json' => 'json1'], $raw);
}

function testDownload()
Expand All @@ -114,6 +188,13 @@ function testDownload()
$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(filesize('./test.png'), $response->getHeaders()['content-length']);
@unlink('./test.png');
}

function testPostString()
Expand All @@ -124,6 +205,14 @@ function testPostString()
$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');
$json = json_decode($response->getBody(), true);
$this->assertEquals("POST", $json['REQUEST_METHOD']);
$this->assertEquals([], $json['POST']);
$this->assertEquals('postStr', $json['RAW']);
}

function testPostFile()
Expand All @@ -138,6 +227,18 @@ function testPostFile()
$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__)
]);
$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']);
}

function testSetHeaders()
Expand All @@ -153,6 +254,18 @@ function testSetHeaders()
$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("head1", $json['HEADER']['Head1']);
$this->assertEquals("head2", $json['HEADER']['Head2']);

}

function testSetHeader()
Expand All @@ -163,6 +276,14 @@ function testSetHeader()
$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("head1", $json['HEADER']['Head1']);
}

function testAddCookies()
Expand All @@ -177,6 +298,18 @@ function testAddCookies()
$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("cookie1", $json['COOKIE']['cookie1']);
$this->assertEquals("cookie2", $json['COOKIE']['cookie2']);
}

function testAddCookie()
Expand All @@ -188,6 +321,15 @@ function testAddCookie()
$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("GET", $json['REQUEST_METHOD']);
$this->assertEquals("head", $json['HEADER']['Head']);
$this->assertEquals("cook", $json['COOKIE']['cookie1']);
}

/**
Expand All @@ -209,6 +351,18 @@ 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');
$upgradeResult = $client->upgrade('cookie1', 'cook');
$this->assertIsBool(true, $upgradeResult);
$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()
Expand All @@ -218,6 +372,13 @@ function testBasicAuth()
$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();
$this->assertEquals('success', $res);
}

function testFollowLocation()
Expand All @@ -228,6 +389,7 @@ function testFollowLocation()
$status = $response->getStatusCode();
$this->assertEquals(301, $status);


$httpClient = new HttpClient('https://www.gaobinzhan.com/blog');
$response = $httpClient->get();
$status = $response->getStatusCode();
Expand All @@ -237,5 +399,25 @@ 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);
}
}

0 comments on commit ddd4c47

Please sign in to comment.