# from PSGI.pod
my $app = sub {
my $env = shift;
return [
'200',
[ 'Content-Type' => 'text/plain' ],
[ "Hello World" ], # or IO::Handle-like object
];
};
use HTTP::Message::PSGI; use HTTP::Request; use HTTP::Response; use MyApp; # is a Plack::Component my $app = MyApp->to_app; my $request = HTTP::Request->new; my $output = $app->( $request->to_psgi ); my $response = HTTP::Response->from_psgi( $output );
use Test::More;
use Test::WWW::Mechanize::PSGI;
use MyApp;
my $mech = Test::WWW::Mechanize::PSGI->new(
app => MyApp->to_app, );
$mech->get_ok( '/' );
done_testing;
package MyApp;
use Moose;
use Template::Simple;
use Plack::Request;
use Plack::Response;
extends 'Plack::Component';
has posts => (
is => 'rw',
lazy => 1,
default => sub { [] },
);
has tmpl => (
is => 'ro', lazy => 1,
default => sub {
Template::Simple->new(
templates => {
view => <<'ENDTMPL',
<form method="POST"><label>Name:<input name="name"/></label>
<label>Comment:<textarea name="text"></textarea></label>
</form>
[% START posts %]
<p>[% name %] -- [% text %]</p>
[% END posts %]
ENDTMPL
},
);
}
);
sub call {
my ( $self, $env ) = @_;
my $req = Plack::Request->new( $env );
if ( $req->method eq 'POST' ) {
$self->add_post( $req->parameters );
}
my $res = Plack::Response->new(200);
$res->content_type('text/html');
$res->body( $self->posts_html );
return $res->finalize;
}
sub add_post {
my ( $self, $row ) = @_;
push @{$self->posts}, $row;
}
sub posts_html {
my ( $self ) = @_;
my $output = $self->tmpl->render( 'view', $posts );
return ${ $output };
}
use Test::More;
use Test::WWW::Mechanize::PSGI;
use MyApp;
my $mech = Test::WWW::Mechanize::PSGI->new(
app => MyApp->new->to_app,
);
$mech->get_ok( '/', 'get the form' );
$mech->submit_form_ok(
{
fields => {
name => 'Scrappy Doo',
text => 'Puppy power!',
},
}
"Add a guestbook entry",
);
$mech->content_contains( "Scrappy Doo" ); $mech->content_contains( "Puppy power!" );
follow_link_ok - Follow a linkhtml_lint_ok - Lint check your HTMLtext_contains - Without HTMLpage_links_ok - Check for broken linksSlides are licensed under a CC-BY-SA 3.0 license.
Code is licensed under the Artistic License or GNU GPL v1.0 or later, at your discretion (the same terms as Perl 5 itself).