-
-
Save Akron/542a532395b4ef55317a to your computer and use it in GitHub Desktop.
non-blocking nested ... well ... how? [working!]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Test::More tests => 4; | |
use Test::Mojo; | |
use Mojolicious::Lite; | |
use Mojo::UserAgent; | |
my $t = Test::Mojo->new; | |
# See https://github.com/kraih/mango/blob/master/t/connection.t#L65 | |
my $hm = '/.well-known/host-meta'; | |
# Simple route | |
get '/' => sub { | |
shift->render(text => 'Hello World'); | |
}; | |
# Helper with an async request inside | |
app->helper( | |
get_hm => sub { | |
my ($c, $path, $cb) = @_; | |
# Build new Mojo::UserAgent | |
my $ua = Mojo::UserAgent->new; | |
# Create delay for https without redirects | |
my $delay = Mojo::IOLoop->delay( | |
sub { | |
my $delay = shift; | |
# Get with https without redirects | |
$ua->get('https://' . $path . $hm => $delay->begin); | |
}, | |
sub { | |
my ($delay, $tx) = @_; | |
my $res = $tx->success; | |
if ($res->is_status_class(200)) { | |
$cb->($res->body); | |
return; | |
} | |
else { | |
$delay->steps( | |
sub { | |
my $delay = shift; | |
$ua->max_redirects(3); | |
# Get with http with redirects | |
$ua->get('http://' . $path . $hm => $delay->begin); | |
}, | |
sub { | |
my ($delay, $tx) = @_; | |
my $res = $tx->success; | |
$cb->($tx->success->body) if $res->is_status_class(200); | |
}); | |
} | |
}); | |
# Wait if IOLoop is not running | |
$delay->wait unless $delay->ioloop->is_running; | |
} | |
); | |
# Use async helper again - works kind of fine | |
app->get_hm( | |
'e14n.com' => sub { | |
like(shift, qr{<XRD}, 'This is an XRD 1'); | |
}); | |
$t->get_ok('/'); # Get Resource - works fine | |
# Use async helper - works fine | |
app->get_hm( | |
'yahoo.com' => sub { | |
like(shift, qr{<XRD}, 'This is an XRD 2'); | |
}); | |
$t->get_ok('/'); # Get resource - Not reached ever |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment