Update tests

This commit is contained in:
Pascal Birchler 2025-04-30 12:29:15 +02:00
parent 50a93ecb47
commit f46751c216
No known key found for this signature in database
GPG key ID: 0DECE73DD74E8B2F
2 changed files with 118 additions and 1 deletions

View file

@ -2,6 +2,7 @@

namespace McpWp\Tests;

use Mcp\Types\JsonRpcMessage;
use McpWp\RestController;
use McpWp\Tests_Includes\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
@ -128,6 +129,7 @@ class RestControllerCreateItemTest extends TestCase {
'jsonrpc' => '2.0',
'id' => '0',
'method' => 'tools/list',
'params' => [],
],
JSON_THROW_ON_ERROR
)
@ -142,6 +144,41 @@ class RestControllerCreateItemTest extends TestCase {
$this->assertSame( 'mcp_invalid_session', $error->get_error_code(), 'The expected error code does not match.' );
}

public function test_allows_a_valid_session(): void {
wp_set_current_user( self::$admin );

wp_insert_post(
[
'post_type' => 'mcp_session',
'post_status' => 'publish',
'post_title' => 'FooBar',
'post_name' => 'FooBar',
]
);

$request = new WP_REST_Request( 'POST', '/mcp/v1/mcp' );
$request->add_header( 'Content-Type', 'application/json' );
$request->add_header( 'Mcp-Session-Id', 'FooBar' );
$request->set_body(
json_encode(
[
'jsonrpc' => '2.0',
'id' => '0',
'method' => 'tools/list',
'params' => [],
],
JSON_THROW_ON_ERROR
)
);

$response = rest_get_server()->dispatch( $request );

$data = $response->get_data();

$this->assertEquals( 200, $response->get_status() );
$this->assertInstanceOf( JsonRpcMessage::class, $data, 'Response is not a JSON-RPC message' );
}

public function filter_rest_url_for_leading_slash( $url, $path ) {
if ( is_multisite() || get_option( 'permalink_structure' ) ) {
return $url;

View file

@ -42,7 +42,7 @@ class RestControllerGetItemTest extends TestCase {
parent::tear_down();
}

public function test_disallows_get_requests(): void {
public function test_requires_authentication(): void {
$request = new WP_REST_Request( 'GET', '/mcp/v1/mcp' );
$request->add_header( 'Content-Type', 'application/json' );
$request->set_body(
@ -58,6 +58,86 @@ class RestControllerGetItemTest extends TestCase {

$response = rest_get_server()->dispatch( $request );

$error = $response->as_error();
$this->assertWPError( $error );
$this->assertSame( 'rest_not_logged_in', $error->get_error_code(), 'The expected error code does not match.' );
}

public function test_requires_a_session(): void {
wp_set_current_user( self::$admin );

$request = new WP_REST_Request( 'GET', '/mcp/v1/mcp' );
$request->add_header( 'Content-Type', 'application/json' );
$request->set_body(
json_encode(
[
'jsonrpc' => '2.0',
'id' => '0',
'method' => 'initialize',
],
JSON_THROW_ON_ERROR
)
);

$response = rest_get_server()->dispatch( $request );

$error = $response->as_error();
$this->assertWPError( $error );
$this->assertSame( 'mcp_missing_session', $error->get_error_code(), 'The expected error code does not match.' );
}

public function test_rejects_invalid_session(): void {
wp_set_current_user( self::$admin );

$request = new WP_REST_Request( 'GET', '/mcp/v1/mcp' );
$request->add_header( 'Content-Type', 'application/json' );
$request->add_header( 'Mcp-Session-Id', 'Foo' );
$request->set_body(
json_encode(
[
'jsonrpc' => '2.0',
'id' => '0',
'method' => 'initialize',
],
JSON_THROW_ON_ERROR
)
);

$response = rest_get_server()->dispatch( $request );

$error = $response->as_error();
$this->assertWPError( $error );
$this->assertSame( 'mcp_invalid_session', $error->get_error_code(), 'The expected error code does not match.' );
}

public function test_disallows_sse_requests(): void {
wp_set_current_user( self::$admin );

wp_insert_post(
[
'post_type' => 'mcp_session',
'post_status' => 'publish',
'post_title' => 'FooBar',
'post_name' => 'FooBar',
]
);

$request = new WP_REST_Request( 'GET', '/mcp/v1/mcp' );
$request->add_header( 'Content-Type', 'application/json' );
$request->add_header( 'Mcp-Session-Id', 'FooBar' );
$request->set_body(
json_encode(
[
'jsonrpc' => '2.0',
'id' => '0',
'method' => 'initialize',
],
JSON_THROW_ON_ERROR
)
);

$response = rest_get_server()->dispatch( $request );

$error = $response->as_error();
$this->assertWPError( $error );
$this->assertSame( 'mcp_sse_not_supported', $error->get_error_code(), 'The expected error code does not match.' );