ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MCP/t/25_http.t
Revision: 1.2
Committed: Sat Nov 7 20:37:06 2026 UTC (2 weeks ago) by root
Content type: application/x-troff
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
State: FILE REMOVED
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 use common::sense;
2     use Test::More;
3     use AnyEvent ();
4     use AnyEvent::Socket ();
5     use AnyEvent::Handle ();
6     use AnyEvent::MCP::Client::JSONL ();
7     use AnyEvent::MCP ();
8     use JSON::XS ( );
9     use MIME::Base64 ( );
10     use File::Temp ( );
11    
12     my $port;
13     my @seen;
14     my $listener = AnyEvent::Socket::tcp_server "127.0.0.1", 0, sub {
15     my ($fh) = @_;
16    
17     my $handle;
18     $handle = AnyEvent::Handle->new (
19     fh => $fh,
20     on_error => sub { $handle->destroy; undef $handle },
21     on_eof => sub { $handle->destroy; undef $handle },
22     );
23     $handle->push_read (line => "\r\n\r\n", sub {
24     my ($h, $head) = @_;
25     my ($method, $path) = $head =~ m{\A(\S+) (\S+) HTTP/};
26     my ($length) = $head =~ /^content-length:\s*(\d+)/im;
27     my $respond = sub {
28     my ($body) = @_;
29     push @seen, [$method, $path, $head, $body];
30     my ($code, $extra, $data) = (200, "", "alpha\nbeta\nALPHA\n");
31     if ($path eq "/cookie") {
32     $extra = "Set-Cookie: sid=abc; Path=/\r\n";
33     } elsif ($path eq "/binary") {
34     $data = "\x00\xff\x80";
35     } elsif ($path eq "/redirect") {
36     $code = 302;
37     $extra = "Location: /unexpected\r\n";
38     $data = "";
39     } elsif ($path eq "/post") {
40     $data = $body;
41     } elsif ($path eq "/long") {
42     $data = "x" x 100;
43     }
44     $h->push_write ("HTTP/1.1 $code Test\r\n"
45     . "Content-Length: " . length ($data) . "\r\nConnection: close\r\n"
46     . $extra . "\r\n" . $data);
47     $h->on_drain (sub { $h->destroy; undef $handle });
48     };
49     if ($length) {
50     $h->push_read (chunk => $length, sub { $respond->($_[1]) });
51     } else {
52     $respond->("");
53     }
54     });
55     }, sub { $port = $_[2] };
56    
57     my $client = AnyEvent::MCP::Client::JSONL->new (
58     command => [$^X, "-Ilib", "bin/mcp-http", "--stdio", "--auto-allow", "request,*"],
59     );
60     sub wait_for {
61     my ($start) = @_;
62    
63     my $cv = AE::cv;
64     my $timer = AE::timer 10, 0, sub { $cv->croak ("mcp-http test timeout") };
65     $start->(sub { $cv->send ([@_]) });
66     @{$cv->recv}
67     }
68     sub call_http {
69     my ($args) = @_;
70    
71     my ($ok, $result) = wait_for (sub {
72     $client->rpc ("tools/call", { name => "request", arguments => $args }, $_[0]);
73     });
74     ok $ok, "MCP transport success";
75     $result
76     }
77    
78     my $base = "http://127.0.0.1:$port";
79     my ($ok, $tools) = wait_for (sub { $client->rpc ("tools/list", {}, $_[0]) });
80     ok $ok, "tool list transport works";
81     is_deeply [sort map $_->{name}, @{$tools->{tools}}],
82     [sort qw(request meta-instructions)], "one request tool and metadata";
83    
84     my $r = call_http ({ url => "$base/cookie", cookie_jar => {} });
85     ok !$r->{isError}, "GET succeeded";
86     is $r->{structuredContent}{status}, 200, "HTTP status returned";
87     is $r->{structuredContent}{body}, "alpha\nbeta\nALPHA\n", "text response";
88     like $seen[-1][2], qr/^user-agent:\s*mcp-http\/\Q$AnyEvent::MCP::VERSION\E\s*$/im,
89     "default User-Agent identifies versioned mcp-http";
90     is $r->{structuredContent}{cookie_jar}{version}, 2, "versioned cookie jar returned";
91     ok $r->{structuredContent}{cookie_jar}{"127.0.0.1"}, "jar contains response cookie";
92     my $jar = $r->{structuredContent}{cookie_jar};
93    
94     $r = call_http ({
95     url => "$base/post", method => "POST", cookie_jar => $jar,
96     headers => { "content-type" => "application/octet-stream" },
97     body_base64 => MIME::Base64::encode_base64 ("A\0B", ""),
98     });
99     is $r->{structuredContent}{body}, "A\0B", "binary request body preserved";
100     like $seen[-1][2], qr/^cookie:\s*sid=abc/im, "cookie sent from caller-provided jar";
101     is $seen[-1][0], "POST", "HTTP method forwarded";
102    
103     $r = call_http ({ url => "$base/page", headers => { "uSeR-aGeNt" => "custom-agent/1" } });
104     is $r->{structuredContent}{status}, 200, "custom User-Agent request succeeded";
105     like $seen[-1][2], qr/^user-agent:\s*custom-agent\/1\s*$/im,
106     "explicit User-Agent is not overridden";
107    
108     $r = call_http ({ url => "$base/page", grep => "alpha" });
109     is $r->{structuredContent}{body}, "alpha\n", "literal grep selects lines";
110     $r = call_http ({
111     url => "$base/page", regex => "(?i)^alpha",
112     substitute => { pattern => "(?i)alpha", replacement => "X" },
113     });
114     is $r->{structuredContent}{body}, "X\nX\n", "regex selection followed by global substitution";
115    
116     $r = call_http ({ url => "$base/binary" });
117     is $r->{structuredContent}{body_base64}, MIME::Base64::encode_base64 ("\x00\xff\x80", ""),
118     "non-UTF-8 response returns Base64";
119    
120     $r = call_http ({ url => "$base/long", max_bytes => 12 });
121     is length $r->{structuredContent}{body}, 12, "response size bounded";
122     ok $r->{structuredContent}{truncated}, "truncated response indicated";
123     is $r->{structuredContent}{status}, 200, "HTTP status retained after early stop";
124    
125     my $count = @seen;
126     $r = call_http ({ url => "$base/redirect" });
127     is $r->{structuredContent}{status}, 302, "redirect returned, not followed";
128     is scalar @seen, $count + 1, "no redirected request was sent";
129    
130     for my $bad (
131     { url => "http://user:pw\@example.com/" },
132     { url => "$base/page", headers => { "x-test" => "bad\nheader" } },
133     { url => "$base/page", regex => "[" },
134     { url => "$base/page", body => "x", body_base64 => "WA==" },
135     ) {
136     my $before = @seen;
137     $r = call_http ($bad);
138     ok $r->{isError}, "invalid request is rejected";
139     is scalar @seen, $before, "invalid request performed no network I/O";
140     }
141    
142     my $restricted = AnyEvent::MCP::Client::JSONL->new (
143     command => [$^X, "-Ilib", "bin/mcp-http", "--stdio", "--ask", "mcp"],
144     env => { XDG_CONFIG_HOME => File::Temp::tempdir (CLEANUP => 1) },
145     client_capabilities => { elicitation => { form => {} } },
146     );
147     my $before = @seen;
148     ($ok, $r) = wait_for (sub {
149     $restricted->rpc ("tools/call", {
150     name => "request",
151     arguments => { url => "$base/page?q=1", method => "PUT" },
152     }, $_[0]);
153     });
154     ok $ok, "permission request transport success";
155     is $r->{resultType}, "input_required", "new method and URL require approval";
156     is scalar @seen, $before, "no HTTP call before permission granted";
157     my $question = (values %{$r->{inputRequests} || {}})[0];
158     my $permission = $question->{params}{_meta}{"anyevent-mcp/permission"}{request};
159     is $permission->{action}, "PUT", "HTTP method is permission action";
160     is $permission->{dimensions}{url}{value}, "$base/page?q=1", "URL is separate permission dimension";
161     is_deeply [map $_->{label}, @{$permission->{dimensions}{url}{choices}}], [
162     "Exact URL including query", "This path, any query", "This origin (scheme, host, port)",
163     "This hostname, any scheme or port", "Any HTTP(S) URL",
164     ], "IP URL offers exact, path, origin, hostname and universal scopes";
165    
166     ($ok, $r) = wait_for (sub {
167     $restricted->rpc ("tools/call", {
168     name => "request",
169     arguments => { url => "HTTPS://Example.COM:443/path?q=1#fragment", method => "PROPFIND" },
170     }, $_[0]);
171     });
172     is $r->{resultType}, "input_required", "arbitrary HTTP method recognized";
173     $question = (values %{$r->{inputRequests} || {}})[0];
174     $permission = $question->{params}{_meta}{"anyevent-mcp/permission"}{request};
175     is $permission->{dimensions}{url}{value}, "https://example.com/path?q=1",
176     "URL normalizes origin and removes fragments";
177     is scalar @{$permission->{dimensions}{url}{choices}}, 6, "DNS hostname offers subdomain scope";
178    
179     undef $restricted;
180     undef $client;
181     undef $listener;
182     done_testing;