Skip to content

Instantly share code, notes, and snippets.

@yko
Created February 11, 2011 20:39
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 yko/212aa131d04bf79b9875 to your computer and use it in GitHub Desktop.
Save yko/212aa131d04bf79b9875 to your computer and use it in GitHub Desktop.
package Test::MyApp;
use strict;
use warnings;
use base 'Test::Mojo';
require Test::More;
require MyApp;
sub new {
my $self = shift->SUPER::new(@_);
$self->app(MyApp->new) unless $self->app;
return $self;
}
sub import {
my $class = shift;
my $caller = caller;
eval "package $caller; Test::More->import(\@_);";
$ENV{MOJO_EXE} = 'script/my_app';
}
my $inited_users = 0;
sub init_users {
return if $inited_users++;
# Fake
'INSERT INTTO USERS (user, pass) VALUES ("dummy", PASSWORD("password"))'
}
sub login_ok {
my $self = shift;
$self->init_users;
# Description
my $desc = "login";
utf8::encode $desc;
# Client
my $client = $self->client;
$client->app($self->app);
$client->max_redirects($self->max_redirects);
# Request
$client->post_form(
$self->app->url_for('login'),
{l => 'dummy', p => 'password'},
sub { $self->tx($_[-1]) }
)->start;
# Test
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $logged_in_url = $self->app->url_for('/logged_in')->base($self->tx->req->url)->to_abs;
if (!$self->tx->is_done) {
Test::More::fail("$desc: tx fail");
} elsif ($self->tx->res->code != 302) {
Test::More::fail("$desc: code " . $self->tx->res->code);
} elsif ($self->tx->res->headers->header('Location') ne $logged_in_url) {
Test::More::is(
Mojo::URL->new(scalar $self->tx->res->headers->header('Location'))->path,
$logged_in_url,
"$desc: location "
);
} else {
Test::More::pass($desc);
}
return $self;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment