AnyEvent-MCP

 view release on metacpan or  search on metacpan

t/c06_multibroker.t  view on Meta::CPAN

      XDG_CONFIG_HOME => "$dir/stdio_config",
   },
);
($success, $result) = wait_for (sub { $pure_stdio->tools ($_[0]) });
ok $success && @$result == 9, "broker with only stdio children";
ok !-e "$dir/stdio_config", "pure stdio broker reads no global configuration";
$pure_stdio->close;


# Test named bind configuration and command-line precedence with ephemeral ports.
for my $cli (1, 0) {
   my $probe = IO::Socket::INET->new (
      LocalAddr => "127.0.0.1", LocalPort => 0, Proto => "tcp", Listen => 1,
   ) or die "allocate test port: $!\n";
   my $port = $probe->sockport;
   close $probe;
   my @auth_options = $cli ? ("--token", "upstream-first", "--token", "upstream-second") : ();
   File::Path::mkpath ("$dir/http_config/$cli/anyevent-mcp", 0, 0700);
   write_file "$dir/http_config/$cli/anyevent-mcp/global.ini",
      "[instance default]\nbind = " . ($cli ? "127.0.0.2:$port" : "127.0.0.1:$port") . "\n";
   chmod 0600, "$dir/http_config/$cli/anyevent-mcp/global.ini" or die "chmod: $!";
   my ($url, $url_error);
   {
      local $ENV{XDG_CONFIG_HOME} = "$dir/http_config/$cli";
      my $cv = AnyEvent::Util::run_cmd (
         [$^X, "-Ilib", "bin/mcp-multibroker", @auth_options, ($cli ? ("--bind=127.0.0.1:$port") : ()), "--print-url"],
         ">" => \$url, "2>" => \$url_error,
      );
      $cv->recv == 0 or die "multibroker --print-url: $url_error\n";
   }
   chomp $url;
   $url =~ s/\s+# user [^\n]*\z//;
   my $pid;
   END { kill TERM => $pid if $pid }
   my $server_cv;
   my ($stdout, $stderr);
   {
      local %ENV = (
         %ENV,
         PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL => "",
         XDG_CONFIG_HOME => "$dir/http_config/$cli",
      );
      $server_cv = AnyEvent::Util::run_cmd (
         [$^X, "-Ilib", "bin/mcp-multibroker", @auth_options, ($cli ? ("--bind=127.0.0.1:$port") : ()), child => $command[-1]],
         "<" => "/dev/null", ">" => \$stdout, "2>" => \$stderr, '$$' => \$pid, close_all => 1,
      );
   }
   $server_cv->cb (sub { undef $pid });
   my $http_failures = 0;
   my $http_diag = sub {
      my ($step, $value) = @_;
      ++$http_failures;
      print STDERR "multibroker HTTP cli=$cli $step: ", JSON::XS::encode_json ($value), "\n";
   };
   ok $url =~ m{\Ahttp://127\.0\.0\.1:}, "--bind overrides configured address"
      if $cli;
   my ($printed_port, $path) = $url =~ m{\Ahttp://127\.0\.0\.1:([0-9]+)(/.*)\z};
   $printed_port == $port or die "--print-url returned wrong port\n";
   my $socket;
   for (1 .. 1500) {
      $socket = IO::Socket::INET->new (PeerAddr => "127.0.0.1", PeerPort => $port, Proto => "tcp", Timeout => 1);
      last if $socket;
      Time::HiRes::sleep (0.02);
   }
   $socket or die "CORS test socket: $!\n";
   print {$socket}
      "OPTIONS $path HTTP/1.1\r\nHost: localhost\r\n"
      . "Origin: http://localhost:5001\r\n"
      . "Access-Control-Request-Method: POST\r\n"
      . "Access-Control-Request-Headers: content-type,authorization\r\n\r\n";
   my $preflight;
   {
      local $/ = "\r\n\r\n";
      $preflight = <$socket>;
   }
   close $socket;
   my $allowed = "*";
   my $preflight_ok = $preflight =~ /HTTP\/1\.1 204/
      && $preflight =~ /^Access-Control-Allow-Origin: \Q$allowed\E\r?$/m;
   ok $preflight_ok, "CORS preflight allows any origin by default";
   $http_diag->("preflight", $preflight) unless $preflight_ok;
   my $web = AnyEvent::MCP::Client::HTTP->new (url => $url);
   ($success, $result) = wait_for (sub { $web->call ("child.echo", { web => 1 }, $_[0]) });
   my $http_ok = $success && $result->{structuredContent}{web};
   ok $http_ok, ($cli ? "CLI bind overrides named bind; cookie URL works" : "named bind works; cookie URL works");
   $http_diag->("cookie", [$success, $result]) unless $http_ok;
   $web->close;
   my ($clean, $token) = $url =~ m{\A(https?://[^/]+/)([^/]+)/\z};
   $web = AnyEvent::MCP::Client::HTTP->new (url => $clean, bearer => $token);
   ($success, $result) = wait_for (sub { $web->call ("child.echo", { bearer => 1 }, $_[0]) });
   $http_ok = $success && $result->{structuredContent}{bearer};
   ok $http_ok, "same token authenticates clean URL as bearer";
   $http_diag->("bearer", [$success, $result]) unless $http_ok;
   $web->close;
   if ($cli) {
      $web = AnyEvent::MCP::Client::HTTP->new (url => $clean, bearer => "upstream-second");
      ($success, $result) = wait_for (sub { $web->call ("child.echo", { alternate => 1 }, $_[0]) });
      $http_ok = $success && $result->{structuredContent}{alternate};
      ok $http_ok, "second upstream --token authenticates";
      $http_diag->("second token", [$success, $result]) unless $http_ok;
      $web->close;
   }
   ok -f "$dir/http_config/$cli/anyevent-mcp/global.ini", "default HTTP startup stores its master secret";
   kill TERM => $pid if $pid;
   my ($ended, $exit) = wait_for (sub {
      my ($done) = @_;
      $server_cv->cb (sub { undef $pid; $done->(1, $_[0]->recv) });
   });
   ok !$stdout, "HTTP broker startup keeps stdout empty";
   print STDERR "multibroker HTTP cli=$cli server exit=$exit stderr: ",
      JSON::XS::encode_json (substr ($stderr // "", 0, 4096)), "\n" if $http_failures;
}



( run in 1.087 second using v1.01-cache-2.11-cpan-234fa220139 )