-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.