Skip to content

Instantly share code, notes, and snippets.

@s1037989
Created November 23, 2022 11:04
Show Gist options
  • Save s1037989/9ac5d8826de3af059945ce3c21e50a96 to your computer and use it in GitHub Desktop.
Save s1037989/9ac5d8826de3af059945ce3c21e50a96 to your computer and use it in GitHub Desktop.
use Mojo::Base -strict, -signatures;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::Mojo;
use Mojo::Server::Daemon;
use Mojo::URL;
use Mojolicious;
use Mojolicious::Lite;
no warnings;
my $app = Mojolicious->new;
my $daemon = Mojo::Server::Daemon->new(listen => ['http://127.0.0.1'], silent => 1, app => $app);
my $port = $daemon->start->ports->[0];
my $url = Mojo::URL->new("http://127.0.0.1:$port");
my $r = $app->routes;
$r->get('/size' => sub ($c) {
my $code = $c->param('code') || 204;
my $length = $c->param('length') || 0;
warn "/size $code $length\n";
$c->res->headers->header('X-Mojo-App' => 'Size');
$c->render(data => 'x'x$length, status => $code);
});
$r->get('/redirect' => sub ($c) {
my $code = $c->param('code') || 302;
warn "/redirect $code\n";
$c->res->headers->header('X-Mojo-App' => 'Redirect');
$c->res->headers->header('Connection' => 'close') if $code != 304;
$c->res->code($code);
$c->redirect_to("/size?length=0");
});
get '/proxy/*target' => sub ($c) {
my $target = $c->stash('target');
warn "/proxy/$target\n";
$c->proxy->get_p($url->path($target)->query($c->req->url->query))->catch(sub ($err) {
$c->render(text => "Error: $err", status => 400);
});
};
my $t = Test::Mojo->new;
$t->ua->inactivity_timeout(1)->request_timeout(1)->connect_timeout(1);
warn sprintf "Size Server: %s\n", $url;
warn sprintf "Proxy Server: %s\n", $t->ua->server->url;
$ARGV[0] ||= '/size';
my $get = $ARGV[0] =~ /^\/proxy/ ? $t->ua->server->url : $url;
$get = $get->path($ARGV[0])->query(sprintf 'code=%s&length=%s', $ARGV[1]||204, $ARGV[2]||0);
warn sprintf "Get: %s\n", $get;
#say $t->ua->get($get)->result->headers->to_string;
my $tx = $t->ua->get($get);
printf "Code: %s\nLength: %s\n", $tx->result->code, $tx->result->headers->content_length;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment