Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/8e6bc065011a6fed9d15 to your computer and use it in GitHub Desktop.
Save jberger/8e6bc065011a6fed9d15 to your computer and use it in GitHub Desktop.
Using Test::Mojo for purely plack apps
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;
#!/usr/bin/perl
use Dancer;
get '/hello/:name' => sub {
return 'Why, hello there <span id="name">' . param('name') . '</span>';
};
dance;
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