-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #920 from Chance-fyi/feature-test-http-server
Feature test of http server
- Loading branch information
Showing
3 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
|
||
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |