Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature test of http server #920

Merged
merged 2 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
},
"require-dev": {
"pestphp/pest": "2.x-dev",
"mockery/mockery": "2.0.x-dev"
"mockery/mockery": "2.0.x-dev",
"guzzlehttp/guzzle": "^7.0"
},
"config": {
"allow-plugins": {
Expand Down
116 changes: 116 additions & 0 deletions tests/Feature/HttpConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Psr7\Utils;
use Symfony\Component\Process\PhpProcess;

$process = null;
beforeAll(function () use (&$process) {
$code = file_get_contents(__DIR__ . '/Stub/HttpServer.php');
$process = new PhpProcess($code);
$process->start();
sleep(1);
});

afterAll(function () use (&$process) {
$process->stop();
});
Comment on lines +8 to +18
Copy link
Contributor

@joanhey joanhey Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we don't need to start the HttpServer with processes.
In reality is only used to start and stop the server. But it's OK.
This will be more useful to run more future tests concurrently to the server.

Good work.


it('tests http connection', function () {
$client = new Client([
'base_uri' => 'http://127.0.0.1:8080',
'cookies' => true,
'http_errors' => false,
]);

$response = $client->get('/');
expect($response->getStatusCode())
->toBe(200)
->and($response->getHeaderLine('Server'))
->tobe('workerman')
->and($response->getHeaderLine('Content-Length'))
->tobe('12')
->and($response->getBody()->getContents())
->toBe('Hello Chance');

$data = [
'foo' => 'bar',
'key' => ['hello', 'chance']
];
$response = $client->get('/get', [
'query' => $data
]);
expect($response->getBody()->getContents())
->toBeJson()
->json()
->toBe($data);

$response = $client->post('/post', [
'json' => $data
]);
expect($response->getBody()->getContents())
->toBeJson()
->json()
->toBe($data);

$response = $client->post('/header', [
'headers' => [
'foo' => 'bar'
]
]);
expect($response->getBody()->getContents())
->toBe('bar');

$cookie = new CookieJar();
$client->get('/setSession', [
'cookies' => $cookie
]);
$response = $client->get('/session', [
'cookies' => $cookie
]);
expect($response->getBody()->getContents())
->toBe('bar');
$response = $client->get('/session', [
'cookies' => $cookie
]);
expect($response->getBody()->getContents())
->toBe('');

$response = $client->get('/sse', [
'stream' => true,
]);
$stream = $response->getBody();
$i = 0;
while (!$stream->eof()) {
if ($i >= 5) {
expect($stream->read(1024))->toBeEmpty();
continue;
}
$i++;
expect($stream->read(1024))->toBe("data: hello$i\n\n");
}

$file = Utils::tryFopen(__DIR__ . '/Stub/HttpServer.php', 'r');
$response = $client->post('/file', [
'multipart' => [
[
'name' => 'file',
'contents' => $file
]
]
]);
expect($response->getBody()->getContents())
->toBeJson()
->json()
->toMatchArray([
'name' => 'HttpServer.php',
'error' => 0,
]);

$response = $client->get('/404');
expect($response->getStatusCode())
->toBe(404)
->and($response->getBody()->getContents())
->toBe('404 not found');
});
52 changes: 52 additions & 0 deletions tests/Feature/Stub/HttpServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
use Workerman\Protocols\Http\ServerSentEvents;
use Workerman\Timer;
use Workerman\Worker;

require_once __DIR__ . '/vendor/autoload.php';

if (!defined('STDIN')) define('STDIN', fopen('php://stdin', 'r'));
if (!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'w'));
if (!defined('STDERR')) define('STDERR', fopen('php://stderr', 'w'));

$worker = new Worker('http://0.0.0.0:8080');

$worker->onMessage = function (TcpConnection $connection, Request $request) {
match ($request->path()) {
'/' => $connection->send('Hello Chance'),
'/get' => $connection->send(json_encode($request->get())),
'/post' => $connection->send(json_encode($request->post())),
'/header' => $connection->send($request->header('foo')),
'/setSession' => (function () use ($connection, $request) {
$request->session()->set('foo', 'bar');
$connection->send('');
})(),
'/session' => $connection->send($request->session()->pull('foo')),
'/sse' => (function () use ($connection) {
$connection->send(new Response(200, ['Content-Type' => 'text/event-stream'], "\r\n"));
$i = 0;
$timer_id = Timer::add(0.001, function () use ($connection, &$timer_id, &$i) {
if ($connection->getStatus() !== TcpConnection::STATUS_ESTABLISHED) {
Timer::del($timer_id);
return;
}
if ($i >= 5) {
Timer::del($timer_id);
$connection->close();
return;
}
$i++;
$connection->send(new ServerSentEvents(['data' => "hello$i"]));
});
})(),
'/file' => $connection->send(json_encode($request->file('file'))),
default => $connection->send(new Response(404, [], '404 not found'))
};
};

Worker::$command = 'start';
Worker::runAll();