AnyEvent-MCP

 view release on metacpan or  search on metacpan

t/c15_getoptions.t  view on Meta::CPAN

like $err, qr/vcs-read workdir=1 backend=git operation=diff\n/,
   "VCS diff logs backend and omits an empty filename list";
like $err, qr/vcs-read workdir=2 backend=hg operation=log files="quote\\\"name"\n/,
   "VCS log quotes special characters in filenames";
like $err, qr/grep workdir=1 path=t pattern="foo\\ny{156}"\.\.\.\n/,
   "grep logs the bounded search pattern with escaped newlines";
like $err, qr/request method=GET url=https:\/\/example.org\/page\n/,
   "applications can add sanitized context to the shared request logger";
like $err, qr/\d\d \d\d:\d\d:\d\d\.\d\d [^\s]+\/example\[\d+\] error main: diagnostic failure\n/,
   "ordinary diagnostics use the same process-wide formatter";

my $error_probe = q{
   mcp_http auth => "none", port => 0, on_start => sub {
      my ($server) = @_;
      $server->event (connection_error => bless ({
         mcp_remote => "127.0.0.1:12345",
         mcp_peer => { client_info => { name => "two\nlines" } },
      }, "AnyEvent::Handle"), "Broken pipe");
      $server->event (connection_error => bless ({
         mcp_peer => {},
      }, "AnyEvent::Handle"), "Broken pipe");
      $server->close;
   };
};
{
   local $ENV{PERL_ANYEVENT_LOG} = "filter=debug";
   ($status, $out, $err) = command ($error_probe);
}
is $status, 0, "connection error log probe exits cleanly";
unlike $err, qr/MCP connection/, "connection errors are silent at debug level";
{
   local $ENV{PERL_ANYEVENT_LOG} = "filter=trace";
   ($status, $out, $err) = command ($error_probe);
}
is $status, 0, "trace connection error log probe exits cleanly";
like $err, qr/MCP connection id=[a-f0-9]+ remote=127\.0\.0\.1:12345 client="two\\nlines": Broken pipe\n/,
   "trace connection errors identify the remote endpoint and escaped client name";
like $err, qr/MCP connection id=[a-f0-9]+: Broken pipe\n/,
   "unidentified connections still receive a connection id";

my $http_probe = q{
   use AnyEvent;
   use AnyEvent::Handle;
   use AnyEvent::MCP::Server::HTTP;
   use Socket qw(AF_UNIX SOCK_STREAM PF_UNSPEC);

   my $server = AnyEvent::MCP::Server::HTTP->new (
      tokens => ["real-token"],
      on_http_request => sub {
         my ($server, $req, $reply) = @_;
         return 0 unless $req->{uri} eq "/health";
         $reply->(200, "OK", {}, "ok");
         1
      },
   );
   my $timeout = AE::timer 5, 0, sub { die "HTTP probe timed out\n" };

   for my $request (
      "GET / HTTP/1.1\r\nContent-Length: 0\r\n\r\n",
      "GET /guess-token/mcp/ HTTP/1.1\r\nContent-Length: 0\r\n\r\n",
      "GET /robots.txt?password=not-for-logs HTTP/1.1\r\nContent-Length: 0\r\n\r\n",
      "GET /real-token/not-mcp/ HTTP/1.1\r\nContent-Length: 0\r\n\r\n",
      "GET http://example.org/mcp/ HTTP/1.1\r\nContent-Length: 0\r\n\r\n",
      "POST /mcp/ HTTP/1.1\r\nContent-Length: 0\r\nAuthorization: Bearer wrong-token\r\n\r\n",
      "POST /real-token/mcp/ HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: 1\r\n\r\n!",
      "GET /health HTTP/1.1\r\nContent-Length: 0\r\n\r\n",
      "malformed\r\n\r\n",
   ) {
      socketpair my $a, my $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!;
      my $handle = $server->accept ($a);
      $handle->{mcp_remote} = "192.0.2.1:1234";
      my $client = AnyEvent::Handle->new (fh => $b, on_error => sub { die $_[2] });
      my $done = AE::cv;

      $client->push_write ($request);
      $client->push_read (line => "\r\n\r\n", sub {
         my ($handle, $header) = @_;
         die "GET / must return 404\n"
            if $request =~ m{\AGET / HTTP/} && $header !~ m{\AHTTP/1\.1 404 };
         my ($length) = $header =~ /Content-Length: ([0-9]+)/i;
         defined $length or die "HTTP response missing content length\n";
         $handle->push_read (chunk => $length, sub { $done->send });
      });

      $done->recv;
      $client->destroy;
   }

   $server->close;

   # / is a legitimate MCP POST endpoint; a casual GET must still be 404.
   $server = AnyEvent::MCP::Server::HTTP->new (
      path => "/", tokens => ["real-token"],
   );
   my $body = '{"jsonrpc":"2.0","id":1,"method":"ping","params":{}}';
   for my $request (
      "GET / HTTP/1.1\r\nContent-Length: 0\r\n\r\n",
      "POST / HTTP/1.1\r\nContent-Type: application/json\r\n"
         . "Authorization: Bearer real-token\r\nContent-Length: " . length ($body) . "\r\n\r\n$body",
   ) {
      socketpair my $a, my $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!;
      my $handle = $server->accept ($a);
      $handle->{mcp_remote} = "192.0.2.1:1234";
      my $client = AnyEvent::Handle->new (fh => $b, on_error => sub { die $_[2] });
      my $done = AE::cv;
      $client->push_write ($request);
      $client->push_read (line => "\r\n\r\n", sub {
         my ($handle, $header) = @_;
         my $expected = $request =~ /\AGET/ ? 404 : 200;
         die "Root endpoint: expected $expected\n" unless $header =~ /\AHTTP\/1\.1 $expected /;
         my ($length) = $header =~ /Content-Length: ([0-9]+)/i;
         defined $length or die "HTTP response missing content length\n";
         $handle->push_read (chunk => $length, sub { $done->send });
      });
      $done->recv;
      $client->destroy;
   }
   $server->close;
};
{
   local $ENV{PERL_ANYEVENT_LOG} = "filter=warn";



( run in 1.360 second using v1.01-cache-2.11-cpan-f5f21f1076c )