Skip to content

Commit

Permalink
add http functions (#97)
Browse files Browse the repository at this point in the history
* add http functions

* fix tests

* fix tests [2]

* enhancements on type declarations

* coding style fix

Co-authored-by: Demin Yin <[email protected]>
  • Loading branch information
matyhtf and deminy authored Feb 22, 2021
1 parent 2d8eaab commit c05d599
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
},
"require": {
"php": ">=7.2",
"ext-swoole": ">=4.6"
"ext-swoole": ">=4.6",
"friendsofphp/php-cs-fixer": "^2.18"
},
"require-dev": {
"ext-curl": "*",
Expand All @@ -34,6 +35,7 @@
"files": [
"src/constants.php",
"src/core/Coroutine/functions.php",
"src/core/Coroutine/Http/functions.php",
"src/std/exec.php",
"src/ext/curl.php",
"src/ext/sockets.php",
Expand Down
71 changes: 71 additions & 0 deletions src/core/Coroutine/Http/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

namespace Swoole\Coroutine\Http;

use Swoole\Coroutine\Http\Client\Exception;

/**
* @param mixed $data
* @throws Exception
* @return mixed
*/
function request(string $url, string $method, $data = null, array $options = null, array $headers = null, array $cookies = null)
{
$info = parse_url($url);
if ($info['scheme'] == 'http') {
$client = new Client($info['host'], swoole_array_default_value($info, 'port', 80), false);
} elseif ($info['scheme'] == 'https') {
$client = new Client($info['host'], swoole_array_default_value($info, 'port', 443), true);
} else {
throw new Exception('unknown scheme "' . $info['scheme'] . '"');
}
$client->setMethod($method);
if ($data) {
$client->setData($data);
}
if (is_array($options)) {
$client->set($options);
}
if (is_array($headers)) {
$client->setHeaders($options);
}
if (is_array($cookies)) {
$client->setCookies($options);
}
$request_url = $info['path'];
if (!empty($info['query'])) {
$request_url .= '?' . $info['query'];
}
if ($client->execute($request_url)) {
return $client;
}
return false;
}

/**
* @param mixed $data
* @throws Exception
* @return Client|false|mixed
*/
function post(string $url, $data, array $options = null, array $headers = null, array $cookies = null)
{
return request($url, 'POST', $data, $options, $headers, $cookies);
}

/**
* @throws Exception
* @return Client|false|mixed
*/
function get(string $url, array $options = null, array $headers = null, array $cookies = null)
{
return request($url, 'GET', null, $options, $headers, $cookies);
}
45 changes: 45 additions & 0 deletions tests/unit/Coroutine/HttpFunctionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

namespace Swoole\Coroutine;

use PHPUnit\Framework\TestCase;
use function Swoole\Coroutine\Http\get;
use function Swoole\Coroutine\Http\post;

/**
* @internal
* @coversNothing
*/
class HttpFunctionTest extends TestCase
{
public function testGet()
{
run(function () {
$data = get('http://httpbin.org/get?hello=world');
$body = json_decode($data->getBody());
self::assertSame($body->headers->Host, 'httpbin.org');
self::assertSame($body->args->hello, 'world');
});
}

public function testPost()
{
run(function () {
$random_data = base64_encode(random_bytes(128));
$data = post('http://httpbin.org/post?hello=world', ['random_data' => $random_data]);
$body = json_decode($data->getBody());
self::assertSame($body->headers->Host, 'httpbin.org');
self::assertSame($body->args->hello, 'world');
self::assertSame($body->form->random_data, $random_data);
});
}
}

0 comments on commit c05d599

Please sign in to comment.