Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tests/Message/JsonRpcMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,50 @@ public function testFromJsonWithErrorField(): void
$this->assertNull($message->params);
$this->assertNull($message->result);
}

public function testFromJsonResponseMissingId(): void
{
// This JSON represents a result response but is missing the 'id' field.
$json = '{"jsonrpc": "2.0", "result": {"foo": "bar"}}';
$this->expectException(\RuntimeException::class);
$this->expectExceptionCode(JsonRpcMessage::INVALID_REQUEST);
$this->expectExceptionMessage('Response must include ID (even if null for some error cases as per spec, this implementation expects it)');
JsonRpcMessage::fromJson($json);
}

public function testFromJsonErrorResponseMissingId(): void
{
// This JSON represents an error response but is missing the 'id' field.
$json = '{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}}';
$this->expectException(\RuntimeException::class);
$this->expectExceptionCode(JsonRpcMessage::INVALID_REQUEST);
$this->expectExceptionMessage('Response must include ID (even if null for some error cases as per spec, this implementation expects it)');
JsonRpcMessage::fromJson($json);
}

public function testFromJsonErrorFieldNotAnArray(): void
{
// This JSON represents an error response, but the 'error' field is a string, not an object/array.
$json = '{"jsonrpc": "2.0", "error": "This should be an error object", "id": "err-789"}';
$this->expectException(\RuntimeException::class);
$this->expectExceptionCode(JsonRpcMessage::INVALID_REQUEST);
$this->expectExceptionMessage('Invalid error object in JSON-RPC response');
JsonRpcMessage::fromJson($json);
}

public function testJsonSerializeRequestWithId(): void
{
$message = new JsonRpcMessage('test.request', ['foo' => 'bar'], 'req-789');
// Ensure it's treated as a request by jsonSerialize, not result/error
$message->result = null;
$message->error = null;

$serialized = $message->jsonSerialize();

$this->assertArrayHasKey('id', $serialized);
$this->assertEquals('req-789', $serialized['id']);
$this->assertEquals('test.request', $serialized['method']);
$this->assertEquals(['foo' => 'bar'], $serialized['params']);
$this->assertEquals('2.0', $serialized['jsonrpc']);
}
}