Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Jan 24, 2025
1 parent 98e38d3 commit 1072b17
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/ApiServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function send(Response $response, TcpConnection $connection, ?Request $re
$response->withHeader('Content-Type', 'application/json');
$response->withHeader('Server', 'workbunny/webman-push-server');
$response->withHeader('Version', PushServer::$version);
$response->withHeader('SocketId', static::getConnectionProperty($connection, 'socketId'));
$response->withHeader('SocketId', static::getConnectionProperty($connection, 'socketId', ''));
static::setSendBytesStatistics($connection, (string)$response);
if ($request) {
$keepAlive = $request->header('connection');
Expand Down
5 changes: 1 addition & 4 deletions tests/ApiServerBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@
use Workbunny\WebmanPushServer\ApiRoute;
use Workerman\Worker;

/**
* @runTestsInSeparateProcesses
*/
class ApiServerBaseTest extends BaseTestCase
{


public function testApiServerOnMessageSuccessful()
{
$mockConnection = new MockTcpConnection();
$request = new Request("GET /index HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("GET /index HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");
// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);

Expand Down
25 changes: 11 additions & 14 deletions tests/ApiServerRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
use Webman\Http\Response;
use Workbunny\WebmanPushServer\ApiClient;

/**
* @runTestsInSeparateProcesses
*/
class ApiServerRouteTest extends BaseTestCase
{

public function testApiServerRouteChannels(){
// required auth_key
$mockConnection = new MockTcpConnection();
$request = new Request("GET /apps/1/channels HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("GET /apps/1/channels HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");
// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response);
Expand All @@ -37,7 +34,7 @@ public function testApiServerRouteChannels(){

// invalid auth_key
$mockConnection = new MockTcpConnection();
$request = new Request("GET /apps/1/channels?auth_key=test HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("GET /apps/1/channels?auth_key=test HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");
// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response);
Expand All @@ -47,7 +44,7 @@ public function testApiServerRouteChannels(){

// invalid auth_key
$mockConnection = new MockTcpConnection();
$request = new Request("GET /apps/1/channels?auth_key=workbunny&auth_signature=abc HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("GET /apps/1/channels?auth_key=workbunny&auth_signature=abc HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");
// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response);
Expand All @@ -60,7 +57,7 @@ public function testApiServerRouteChannels(){
$signature = ApiClient::routeAuth('workbunny', 'U2FsdGVkX1+vlfFH8Q9XdZ9t9h2bABGYAZltEYAX6UM=', 'GET', '/apps/1/channels', [
'auth_key' => 'workbunny'
]);
$request = new Request("GET /apps/1/channels?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("GET /apps/1/channels?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");
// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response);
Expand All @@ -75,7 +72,7 @@ public function testApiServerRouteChannel(){
$signature = ApiClient::routeAuth('workbunny', 'U2FsdGVkX1+vlfFH8Q9XdZ9t9h2bABGYAZltEYAX6UM=', 'GET', '/apps/1/channels/private-test', [
'auth_key' => 'workbunny'
]);
$request = new Request("GET /apps/1/channels/private-test?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("GET /apps/1/channels/private-test?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");
// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response);
Expand All @@ -88,7 +85,7 @@ public function testApiServerRouteChannel(){
'auth_key' => 'workbunny',
'info' => 'subscription_count'
]);
$request = new Request("GET /apps/1/channels/private-test?auth_key=workbunny&auth_signature=$signature&info=subscription_count HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("GET /apps/1/channels/private-test?auth_key=workbunny&auth_signature=$signature&info=subscription_count HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");
// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response);
Expand All @@ -101,7 +98,7 @@ public function testApiServerRouteChannel(){
'auth_key' => 'workbunny',
'info' => 'subscription_count,user_count'
]);
$request = new Request("GET /apps/1/channels/private-test?auth_key=workbunny&auth_signature=$signature&info=subscription_count,user_count HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("GET /apps/1/channels/private-test?auth_key=workbunny&auth_signature=$signature&info=subscription_count,user_count HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");
// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response);
Expand All @@ -115,7 +112,7 @@ public function testApiServerRouteEvents(){
$signature = ApiClient::routeAuth('workbunny', 'U2FsdGVkX1+vlfFH8Q9XdZ9t9h2bABGYAZltEYAX6UM=', 'POST', '/apps/1/events', [
'auth_key' => 'workbunny'
]);
$request = new Request("POST /apps/1/events?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("POST /apps/1/events?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");

// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);
Expand All @@ -132,7 +129,7 @@ public function testApiServerRouteBatchEvents(){
$signature = ApiClient::routeAuth('workbunny', 'U2FsdGVkX1+vlfFH8Q9XdZ9t9h2bABGYAZltEYAX6UM=', 'POST', '/apps/1/batch_events', [
'auth_key' => 'workbunny'
]);
$request = new Request("POST /apps/1/batch_events?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("POST /apps/1/batch_events?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");

// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);
Expand All @@ -149,7 +146,7 @@ public function testApiServerRouteUsers(){
$signature = ApiClient::routeAuth('workbunny', 'U2FsdGVkX1+vlfFH8Q9XdZ9t9h2bABGYAZltEYAX6UM=', 'GET', '/apps/1/channels/private-test/users', [
'auth_key' => 'workbunny'
]);
$request = new Request("GET /apps/1/channels/private-test/users?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("GET /apps/1/channels/private-test/users?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");
// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response);
Expand All @@ -164,7 +161,7 @@ public function testApiServerRouteTerminateConnections(){
$signature = ApiClient::routeAuth('workbunny', 'U2FsdGVkX1+vlfFH8Q9XdZ9t9h2bABGYAZltEYAX6UM=', 'POST', '/apps/1/users/abc/terminate_connections', [
'auth_key' => 'workbunny'
]);
$request = new Request("POST /apps/1/users/abc/terminate_connections?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n");
$request = new Request("POST /apps/1/users/abc/terminate_connections?auth_key=workbunny&auth_signature=$signature HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");

// 手动触发 onMessage 回调
$this->getApiServer()->onMessage($mockConnection, $request);
Expand Down
43 changes: 23 additions & 20 deletions tests/MockClass/MockTcpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
class MockTcpConnection extends TcpConnection
{

protected Response|string|null $_sendBuffer = null;

protected Response|string|null $_recvBuffer = null;

public function __construct($remote_address = '')
{
++self::$statistics['connection_count'];
$this->id = $this->_id = self::$_idRecorder++;
if(self::$_idRecorder === \PHP_INT_MAX){
self::$_idRecorder = 0;
$this->id = $this->realId = self::$idRecorder++;
if (self::$idRecorder === PHP_INT_MAX) {
self::$idRecorder = 0;
}
$this->maxSendBufferSize = self::$defaultMaxSendBufferSize;
$this->maxPackageSize = self::$defaultMaxPackageSize;
Expand All @@ -28,67 +32,66 @@ public function __construct($remote_address = '')
*/
public function isPaused(): bool
{
return $this->_isPaused;
return $this->isPaused;
}

/**
* @return string|Response
* @return string|Response|null
*/
public function getSendBuffer()
{
return $this->_sendBuffer;
}

/**
* @param string|Response $sendBuffer
* @param string|Response|null $sendBuffer
*/
public function setSendBuffer($sendBuffer): void
public function setSendBuffer(Response|string|null $sendBuffer): void
{
$this->_sendBuffer = $sendBuffer;
}

/**
* @return string
* @return Response|string|null
*/
public function getRecvBuffer(): string
public function getRecvBuffer(): Response|string|null
{
return $this->_recvBuffer;
}

/**
* @param string $recvBuffer
* @param Response|string|null $recvBuffer
*/
public function setRecvBuffer(string $recvBuffer): void
public function setRecvBuffer(Response|string|null $recvBuffer): void
{
$this->_recvBuffer = $recvBuffer;
}

/**
* @param mixed $send_buffer
* @param mixed $sendBuffer
* @param mixed $raw
* @return bool|null
*/
public function send($send_buffer, $raw = false)
public function send(mixed $sendBuffer, bool $raw = false): ?bool
{
$this->setSendBuffer($send_buffer);
$this->setSendBuffer($sendBuffer);
return true;
}

/**
* @param mixed $data
* @param mixed $raw
*/
public function close($data = null, $raw = false)
public function close(mixed $data = null, bool $raw = false): void
{
if ($data) {
$this->send($data, $raw);
}
(new PushServer())->onClose($this);
}

/**
* @return void
*/
public function pauseRecv()
public function pauseRecv(): void
{
$this->_isPaused = true;
$this->isPaused = true;
}
}
11 changes: 6 additions & 5 deletions tests/PushServerBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
use const Workbunny\WebmanPushServer\EVENT_TERMINATE_CONNECTION;
use const Workbunny\WebmanPushServer\EVENT_UNSUBSCRIPTION_SUCCEEDED;

/**
* @runTestsInSeparateProcesses
*/
class PushServerBaseTest extends BaseTestCase
{

Expand All @@ -38,7 +35,7 @@ public function testPushServerOnConnect()
// 初始化一个mock tcp连接
$connection = new MockTcpConnection();
// 初始化判定
$this->assertFalse(property_exists($connection, 'onWebSocketConnect'));
// $this->assertFalse(property_exists($connection, 'onWebSocketConnect'));
$this->assertFalse(property_exists($connection, 'appKey'));
$this->assertFalse(property_exists($connection, 'clientNotSendPingCount'));
$this->assertFalse(property_exists($connection, 'queryString'));
Expand Down Expand Up @@ -104,7 +101,11 @@ public function testPushServerOnConnectInvalidAppError()
// 模拟onConnect
$this->getPushServer()->onConnect($connection);
// 模拟调用$connection->onWebSocketConnect
call_user_func(PushServer::getConnectionProperty($connection, 'onWebSocketConnect'), $connection, '');
call_user_func(
PushServer::getConnectionProperty($connection, 'onWebSocketConnect'),
$connection,
"GET HTTP/1.1\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n"
);
// 断言检测心跳计数为0
$this->assertEquals(
0, PushServer::getConnectionProperty($connection, 'clientNotSendPingCount', 'has-not')
Expand Down
4 changes: 1 addition & 3 deletions tests/PushServerEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
use const Workbunny\WebmanPushServer\EVENT_UNSUBSCRIBE;
use const Workbunny\WebmanPushServer\EVENT_UNSUBSCRIPTION_SUCCEEDED;

/**
* @runTestsInSeparateProcesses
*/

class PushServerEventTest extends BaseTestCase
{

Expand Down

0 comments on commit 1072b17

Please sign in to comment.