| 1 |
root |
1.1 |
use common::sense; |
| 2 |
|
|
|
| 3 |
|
|
use Test::More; |
| 4 |
|
|
use AnyEvent; |
| 5 |
|
|
use AnyEvent::Handle; |
| 6 |
|
|
use AnyEvent::MCP::Server::HTTP (); |
| 7 |
|
|
use JSON::XS (); |
| 8 |
|
|
use Socket qw(AF_UNIX SOCK_STREAM PF_UNSPEC); |
| 9 |
|
|
|
| 10 |
|
|
sub read_response { |
| 11 |
|
|
my ($handle, $cb) = @_; |
| 12 |
|
|
|
| 13 |
|
|
$handle->push_read (line => "\r\n\r\n", sub { |
| 14 |
|
|
my ($handle, $headers) = @_; |
| 15 |
|
|
my ($len) = $headers =~ /Content-Length: ([0-9]+)/i; |
| 16 |
|
|
defined $len or die "Missing Content-Length\n"; |
| 17 |
|
|
$handle->push_read (chunk => $len, sub { $cb->($headers, $_[1]) }); |
| 18 |
|
|
}); |
| 19 |
|
|
} |
| 20 |
|
|
|
| 21 |
|
|
sub post { |
| 22 |
|
|
my ($uri, $content, $headers) = @_; |
| 23 |
|
|
"POST $uri HTTP/1.1\r\nContent-Length: " . length ($content) |
| 24 |
|
|
. "\r\n" . ($headers || "") . "\r\n$content"; |
| 25 |
|
|
} |
| 26 |
|
|
|
| 27 |
|
|
my @requests; |
| 28 |
|
|
my $delay; |
| 29 |
|
|
my $server = AnyEvent::MCP::Server::HTTP->new ( |
| 30 |
|
|
tokens => ["test-token"], |
| 31 |
|
|
on_http_request => sub { |
| 32 |
|
|
my ($server, $req, $reply) = @_; |
| 33 |
|
|
push @requests, [$req->{method}, $req->{uri}, $req->{body}]; |
| 34 |
|
|
|
| 35 |
|
|
return 0 unless $req->{uri} eq "/review"; |
| 36 |
|
|
|
| 37 |
|
|
$delay = AE::timer 0.02, 0, sub { |
| 38 |
|
|
$reply->(200, "OK", { |
| 39 |
|
|
"Content-Type" => "application/octet-stream", |
| 40 |
|
|
"X-Review" => "yes", |
| 41 |
|
|
}, "\0\xff"); |
| 42 |
|
|
$reply->(200, "OK", {}, "duplicate"); |
| 43 |
|
|
}; |
| 44 |
|
|
|
| 45 |
|
|
1 |
| 46 |
|
|
}, |
| 47 |
|
|
); |
| 48 |
|
|
|
| 49 |
|
|
socketpair my $a, my $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!; |
| 50 |
|
|
$server->accept ($a); |
| 51 |
|
|
my $client = AnyEvent::Handle->new (fh => $b, on_error => sub { die $_[2] }); |
| 52 |
|
|
my $cv = AE::cv; |
| 53 |
|
|
my $timeout = AE::timer 5, 0, sub { $cv->croak ("HTTP hook timeout") }; |
| 54 |
|
|
|
| 55 |
|
|
my $request = post ("/review", "A\0B", "Content-Type: application/octet-stream\r\n") |
| 56 |
|
|
. post ("/mcp/", JSON::XS::encode_json ({ |
| 57 |
|
|
jsonrpc => "2.0", id => 41, method => "ping", params => {}, |
| 58 |
|
|
}), "Content-Type: application/json\r\nAuthorization: Bearer test-token\r\n"); |
| 59 |
|
|
|
| 60 |
|
|
$client->push_write ($request); |
| 61 |
|
|
read_response ($client, sub { |
| 62 |
|
|
my ($header, $body) = @_; |
| 63 |
|
|
like $header, qr/^HTTP\/1\.1 200 OK\r?$/m, "hook sends successful HTTP response"; |
| 64 |
|
|
like $header, qr/^Content-Type: application\/octet-stream\r?$/m, "hook sets content type"; |
| 65 |
|
|
like $header, qr/^X-Review: yes\r?$/m, "hook supplies additional headers"; |
| 66 |
|
|
is length $body, 2, "binary response is two bytes"; |
| 67 |
|
|
is unpack ("H*", $body), "00ff", "binary reply content is preserved"; |
| 68 |
|
|
|
| 69 |
|
|
read_response ($client, sub { |
| 70 |
|
|
my ($header, $body) = @_; |
| 71 |
|
|
like $header, qr/^Content-Type: application\/json\r?$/m, "MCP retains JSON default"; |
| 72 |
|
|
is JSON::XS::decode_json ($body)->{id}, 41, "unhandled request falls through to MCP"; |
| 73 |
|
|
$cv->send; |
| 74 |
|
|
}); |
| 75 |
|
|
}); |
| 76 |
|
|
$cv->recv; |
| 77 |
|
|
undef $timeout; |
| 78 |
|
|
undef $delay; |
| 79 |
|
|
is_deeply [map [$_->[0], $_->[1]], @requests], |
| 80 |
|
|
[["POST", "/review"], ["POST", "/mcp/"]], |
| 81 |
|
|
"hook sees both request methods and paths in order"; |
| 82 |
|
|
is $requests[0][2], "A\0B", "hook receives the raw binary body"; |
| 83 |
|
|
is_deeply JSON::XS::decode_json ($requests[1][2]), |
| 84 |
|
|
{ jsonrpc => "2.0", id => 41, method => "ping", params => {} }, |
| 85 |
|
|
"hook receives complete MCP JSON before normal dispatch"; |
| 86 |
|
|
$server->close; |
| 87 |
|
|
$client->destroy; |
| 88 |
|
|
|
| 89 |
|
|
# Custom endpoints do not weaken authentication on normal MCP routes. |
| 90 |
|
|
socketpair $a, $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!; |
| 91 |
|
|
$server->accept ($a); |
| 92 |
|
|
$client = AnyEvent::Handle->new (fh => $b, on_error => sub { die $_[2] }); |
| 93 |
|
|
$cv = AE::cv; |
| 94 |
|
|
$timeout = AE::timer 5, 0, sub { $cv->croak ("HTTP auth timeout") }; |
| 95 |
|
|
$client->push_write (post ("/mcp/", '{"jsonrpc":"2.0","id":42,"method":"ping","params":{}}', |
| 96 |
|
|
"Content-Type: application/json\r\n")); |
| 97 |
|
|
read_response ($client, sub { |
| 98 |
|
|
like $_[0], qr/^HTTP\/1\.1 401 Unauthorized\r?$/m, "MCP still requires authentication"; |
| 99 |
|
|
$cv->send; |
| 100 |
|
|
}); |
| 101 |
|
|
$cv->recv; |
| 102 |
|
|
undef $timeout; |
| 103 |
|
|
$server->close; |
| 104 |
|
|
$client->destroy; |
| 105 |
|
|
|
| 106 |
|
|
# A hook can handle arbitrary non-MCP paths and add ordinary HTTP headers. |
| 107 |
|
|
my $seen = 0; |
| 108 |
|
|
$server = AnyEvent::MCP::Server::HTTP->new ( |
| 109 |
|
|
auth => "none", |
| 110 |
|
|
on_http_request => sub { |
| 111 |
|
|
my ($server, $req, $reply) = @_; |
| 112 |
|
|
++$seen; |
| 113 |
|
|
return 0 unless $req->{uri} eq "/home"; |
| 114 |
|
|
$reply->(200, "OK", { "Content-Type" => "text/html; charset=utf-8", |
| 115 |
|
|
"Cache-Control" => "no-store" }, "<h1>ready</h1>"); |
| 116 |
|
|
1; |
| 117 |
|
|
}, |
| 118 |
|
|
); |
| 119 |
|
|
socketpair $a, $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!; |
| 120 |
|
|
$server->accept ($a); |
| 121 |
|
|
$client = AnyEvent::Handle->new (fh => $b, on_error => sub { die $_[2] }); |
| 122 |
|
|
$cv = AE::cv; |
| 123 |
|
|
$timeout = AE::timer 5, 0, sub { $cv->croak ("HTTP HTML timeout") }; |
| 124 |
|
|
$client->push_write ("GET /home HTTP/1.1\r\nHost: local\r\n\r\n"); |
| 125 |
|
|
read_response ($client, sub { |
| 126 |
|
|
like $_[0], qr/^Content-Type: text\/html; charset=utf-8\r?$/m, "HTML content type"; |
| 127 |
|
|
like $_[0], qr/^Cache-Control: no-store\r?$/m, "Cache-Control header"; |
| 128 |
|
|
is $_[1], "<h1>ready</h1>", "HTML response body"; |
| 129 |
|
|
$cv->send; |
| 130 |
|
|
}); |
| 131 |
|
|
$cv->recv; |
| 132 |
|
|
is $seen, 1, "one HTTP callback"; |
| 133 |
|
|
undef $timeout; |
| 134 |
|
|
$server->close; |
| 135 |
|
|
$client->destroy; |
| 136 |
|
|
|
| 137 |
|
|
done_testing; |