Last active
August 29, 2015 14:21
-
-
Save jberger/8e6bc065011a6fed9d15 to your computer and use it in GitHub Desktop.
Using Test::Mojo for purely plack apps
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 Mojolicious::Lite; | |
plugin MountPSGI => {'/' => 'test.pl'}; | |
use Test::More; | |
use Test::Mojo; | |
my $t = Test::Mojo->new; | |
$t->get_ok('/hello/joel') | |
->status_is(200) | |
->text_is('#name' => 'joel'); | |
done_testing; | |
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/perl | |
use Dancer; | |
get '/hello/:name' => sub { | |
return 'Why, hello there <span id="name">' . param('name') . '</span>'; | |
}; | |
dance; | |
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 strict; | |
use warnings; | |
use Test::More; | |
use Test::Mojo; | |
{ | |
package Mojo::PSGIRunner; | |
use Mojo::Base 'Mojo'; | |
use Mojolicious::Plugin::PlackMiddleware; | |
has app => sub { | |
require Plack::Util; | |
Plack::Util::load_psgi(shift->name); | |
}; | |
has name => sub { die 'an application name (path or class name) or instance (see app) is required' }; | |
sub handler { | |
my ($self, $tx) = @_; | |
my $req = Mojolicious::Plugin::PlackMiddleware::mojo_req_to_psgi_env($tx->req); | |
my $res = $self->app->($req); | |
$tx->res(Mojolicious::Plugin::PlackMiddleware::psgi_res_to_mojo_res($res)); | |
$tx->resume; | |
} | |
} | |
my $app = Mojo::PSGIRunner->new(name => 'test.pl'); | |
my $t = Test::Mojo->new->app($app); | |
$t->get_ok('/hello/joel') | |
->status_is(200) | |
->text_is('#name' => 'joel'); | |
done_testing; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment