-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
349 additions
and
62 deletions.
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
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,134 @@ | ||
<?php | ||
/** | ||
* This file is part of workbunny. | ||
* | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @author chaz6chez<[email protected]> | ||
* @copyright chaz6chez<[email protected]> | ||
* @link https://github.com/workbunny/webman-push-server | ||
* @license https://github.com/workbunny/webman-push-server/blob/main/LICENSE | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Tests; | ||
|
||
use Tests\MockClass\MockTcpConnection; | ||
use Webman\Http\Request; | ||
use Webman\Http\Response; | ||
use Workbunny\WebmanPushServer\ApiRoute; | ||
|
||
/** | ||
* @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"); | ||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('Hello Workbunny!', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(200, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
} | ||
|
||
|
||
public function testApiServerOnMessageWithNormalStringData() | ||
{ | ||
$mockConnection = new MockTcpConnection(); | ||
|
||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, ''); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('Bad Request.', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(400, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
|
||
$mockConnection = new MockTcpConnection(); | ||
|
||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, 'test'); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('Bad Request.', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(400, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
} | ||
|
||
|
||
public function testApiServerOnMessageWithBoolData() | ||
{ | ||
$mockConnection = new MockTcpConnection(); | ||
|
||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, true); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('Bad Request.', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(400, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
|
||
$mockConnection = new MockTcpConnection(); | ||
|
||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, false); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('Bad Request.', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(400, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
} | ||
|
||
|
||
public function testApiServerOnMessageWithNumberData() | ||
{ | ||
$mockConnection = new MockTcpConnection(); | ||
|
||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, 1.1); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('Bad Request.', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(400, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
|
||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, 1); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('Bad Request.', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(400, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
} | ||
|
||
|
||
public function testApiServerOnMessageWithArrayData() | ||
{ | ||
$mockConnection = new MockTcpConnection(); | ||
|
||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, []); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('Bad Request.', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(400, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
|
||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, [ | ||
'test' | ||
]); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('Bad Request.', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(400, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
} | ||
} |
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,177 @@ | ||
<?php | ||
/** | ||
* This file is part of workbunny. | ||
* | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @author chaz6chez<[email protected]> | ||
* @copyright chaz6chez<[email protected]> | ||
* @link https://github.com/workbunny/webman-push-server | ||
* @license https://github.com/workbunny/webman-push-server/blob/main/LICENSE | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Tests; | ||
|
||
use Tests\MockClass\MockTcpConnection; | ||
use Webman\Http\Request; | ||
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"); | ||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('{"error":"Required auth_key"}', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(400, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
|
||
// 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"); | ||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('{"error":"Invalid auth_key"}', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(401, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
|
||
// 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"); | ||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('{"error":"Invalid signature"}', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(401, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
|
||
// successful | ||
$mockConnection = new MockTcpConnection(); | ||
$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"); | ||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertTrue(isset(json_decode($mockConnection->getSendBuffer()->rawBody(),true)['channels'])); | ||
$this->assertEquals(200, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
} | ||
|
||
|
||
public function testApiServerRouteChannel(){ | ||
$mockConnection = new MockTcpConnection(); | ||
$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"); | ||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('{"occupied":true,"type":false}', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(200, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
|
||
$mockConnection = new MockTcpConnection(); | ||
$signature = ApiClient::routeAuth('workbunny', 'U2FsdGVkX1+vlfFH8Q9XdZ9t9h2bABGYAZltEYAX6UM=', 'GET', '/apps/1/channels/private-test', [ | ||
'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"); | ||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('{"occupied":true,"type":false,"subscription_count":false}', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(200, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
|
||
$mockConnection = new MockTcpConnection(); | ||
$signature = ApiClient::routeAuth('workbunny', 'U2FsdGVkX1+vlfFH8Q9XdZ9t9h2bABGYAZltEYAX6UM=', 'GET', '/apps/1/channels/private-test', [ | ||
'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"); | ||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('{"occupied":true,"type":false,"subscription_count":false,"user_count":false}', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(200, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
} | ||
|
||
public function testApiServerRouteEvents(){ | ||
$mockConnection = new MockTcpConnection(); | ||
$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"); | ||
|
||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('{"error":"Required name"}', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(400, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
} | ||
|
||
public function testApiServerRouteBatchEvents(){ | ||
|
||
$mockConnection = new MockTcpConnection(); | ||
$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"); | ||
|
||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('{"error":"Required batch"}', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(400, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
} | ||
|
||
public function testApiServerRouteUsers(){ | ||
|
||
$mockConnection = new MockTcpConnection(); | ||
$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"); | ||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('{"error":"Not Found [private-test]"}', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(404, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
} | ||
|
||
public function testApiServerRouteTerminateConnections(){ | ||
|
||
$mockConnection = new MockTcpConnection(); | ||
$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"); | ||
|
||
// 手动触发 onMessage 回调 | ||
$this->getApiServer()->onMessage($mockConnection, $request); | ||
|
||
$this->assertTrue($mockConnection->getSendBuffer() instanceof Response); | ||
$this->assertEquals('{}', $mockConnection->getSendBuffer()->rawBody()); | ||
$this->assertEquals(200, $mockConnection->getSendBuffer()->getStatusCode()); | ||
$this->assertEquals('application/json', $mockConnection->getSendBuffer()->getHeader('Content-Type')); | ||
} | ||
} |
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
Oops, something went wrong.