-
-
Save FROGGS/0af77883e64e5764894d26d3692ae2c7 to your computer and use it in GitHub Desktop.
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
package MyService; | |
use IO::Socket::SSL; | |
use Mojo::Base 'Mojolicious'; | |
use feature ':5.24'; | |
use experimental qw(signatures refaliasing); | |
no warnings qw(experimental); | |
sub startup($app) { | |
my $authorized = $app->routes->under('/')->to(cb => sub ($self) { | |
if (my $basic = $self->req->headers->authorization) { | |
# again, do somethind | |
return 1; | |
} | |
$self->res->headers->www_authenticate("Basic realm=\"Authentication required\""); | |
$self->res->code(401); | |
$self->rendered; | |
return; | |
}); | |
$authorized->_generate_route(EVENT => '/myobject/:id')->to(cb => sub($self) { | |
my $url = $ENV{VERY_ENTERPRISE_HOST} . '/tasks/' . $self->param('id'); | |
my $headers = { | |
Authorization => 'Basic ' . encode_base64('foo:bar', ''), | |
'Content-Type' => 'application/json;charset=utf-8', | |
}; | |
eval { my $tx = $self->app->ua->post($url => $headers => json => { foo => 42 }); }; | |
say $@; | |
$self->render(json => {}); | |
}); | |
} | |
1; |
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
use feature ':5.24'; | |
use Mojo::Base -strict; | |
use Test::More; | |
use Mojo::Server::Daemon; | |
use Mojolicious::Lite; | |
use Test::Mojo; | |
use experimental qw(signatures refaliasing); | |
no warnings qw(experimental); | |
use lib 'lib'; | |
use MIME::Base64; | |
# mock the 3rdparty service | |
my $authorized = app->routes->under('/very/enterprise')->to(cb => sub ($self) { | |
if (my $basic = $self->req->headers->authorization) { | |
# do something | |
return 1; | |
} | |
$self->res->headers->www_authenticate("Basic realm=\"Authentication required\""); | |
$self->res->code(401); | |
$self->rendered; | |
return; | |
}); | |
$authorized->any('/:route/:id')->to(cb => sub ($c) { | |
$c->res->headers->append('X-ROUTE' => $c->param('route')); | |
$c->res->headers->append('X-METHOD' => $c->method); | |
$c->res->headers->append('X-TASK-ID' => $c->param('id')); | |
$c->render(json => $c->req->json); | |
}); | |
my $daemon = Mojo::Server::Daemon->new(app => app, listen => ['http://127.0.0.1']) | |
->start; | |
my $port = Mojo::IOLoop->acceptor($daemon->acceptors->[0])->port; | |
$ENV{VERY_ENTERPRISE_HOST} = "http://127.0.0.1:$port/very/enterprise"; | |
# now use our own service and start testing | |
my $t = Test::Mojo->new('MyService'); | |
$t->app->ua->ioloop(Mojo::IOLoop->singleton); | |
say my $url = $t->ua->server->url->userinfo('very:secret')->path('/myobject/1'); | |
# acknowledge | |
my $tx = $t->ua->build_tx(EVENT => $url => json => { payload => 42 }); | |
$t->request_ok($tx)->status_is(200); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment