Skip to content

Commit

Permalink
added scheme argument to test_server again and improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 31, 2011
1 parent 05139be commit 6ba33b6
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 135 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,10 +1,10 @@
This file documents the revision history for Perl extension Mojolicious.

2.42 2011-12-31 00:00:00
- Deprecated Test::Mojo->max_redirects.
- Removed exprimental status from respond_to method in
Mojolicious::Controller.
- Removed test_server method from Test::Mojo.
- Removed scheme argument from test_server method in Mojo::UserAgent.
- Improved RFC 6265 compliance of generated request headers.
- Improved documentation.
- Fixed bug that prevented sessions without expiration.
Expand Down
8 changes: 3 additions & 5 deletions lib/Mojo/CookieJar.pm
Expand Up @@ -112,11 +112,9 @@ sub inject {

# Take delicious cookies from the jar
return unless keys %{$self->{jar}};
my $req = $tx->req;
my $url = $req->url->clone;
my $headers = $req->headers;
$url->scheme('https') if $headers->header('X-Forwarded-HTTPS');
if (my $host = $headers->host) { $url->host($host) }
my $req = $tx->req;
my $url = $req->url->clone;
if (my $host = $req->headers->host) { $url->host($host) }
$req->cookies($self->find($url));
}

Expand Down
18 changes: 12 additions & 6 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -134,12 +134,12 @@ sub test_server {
my $self = shift;

# Prepare application for testing
my $server = $self->_server;
my $server = $self->_server(@_);
delete $server->{app};
$server->app($self->app);

# Build absolute URL for test server
return Mojo::URL->new("http://localhost:$self->{port}/");
return Mojo::URL->new("$self->{scheme}://localhost:$self->{port}/");
}

sub websocket {
Expand Down Expand Up @@ -439,18 +439,22 @@ sub _redirect {
}

sub _server {
my $self = shift;
my ($self, $scheme) = @_;

# Start test server
# Restart with different scheme
delete $self->{port} if $scheme;
return $self->{server} if $self->{port};

# Start test server
my $loop = $self->_loop;
my $server = $self->{server} =
Mojo::Server::Daemon->new(ioloop => $loop, silent => 1);
my $port = $self->{port} = $loop->generate_port;
die "Couldn't find a free TCP port for testing.\n" unless $port;
$server->listen(["http://*:$port"]);
$self->{scheme} = $scheme ||= 'http';
$server->listen(["$scheme://*:$port"]);
$server->prepare_ioloop;
warn "TEST SERVER STARTED (http://*:$port)\n" if DEBUG;
warn "TEST SERVER STARTED ($scheme://*:$port)\n" if DEBUG;

return $server;
}
Expand Down Expand Up @@ -947,6 +951,8 @@ transactions non-blocking.
=head2 C<test_server>
my $url = $ua->test_server;
my $url = $ua->test_server('http');
my $url = $ua->test_server('https');
Starts a test server for C<app> if necessary and returns absolute
L<Mojo::URL> object for it. Note that this method is EXPERIMENTAL and might
Expand Down
9 changes: 2 additions & 7 deletions lib/Test/Mojo.pm
Expand Up @@ -199,7 +199,9 @@ sub json_hasnt {
return $self;
}

# DEPRECATED in Leaf Fluttering In Wind!
sub max_redirects {
warn "Test::Mojo->max_redirects is DEPRECATED!\n";
my $self = shift;
return $self->ua->max_redirects unless @_;
$self->ua->max_redirects(@_);
Expand Down Expand Up @@ -613,13 +615,6 @@ EXPERIMENTAL and might change without warning!
Opposite of C<json_has>. Note that this method is EXPERIMENTAL and might
change without warning!
=head2 C<max_redirects>
my $max_redirects = $t->max_redirects;
$t = $t->max_redirects(3);
Alias for the L<Mojo::UserAgent/"max_redirects">.
=head2 C<message_is>
$t = $t->message_is('working!');
Expand Down
4 changes: 2 additions & 2 deletions t/mojolicious/lite_app.t
Expand Up @@ -1270,15 +1270,15 @@ $t->get_ok('/static_render')->status_is(200)
->content_is('Hello Mojo from a static file!');

# GET /redirect_named (with redirecting enabled in user agent)
$t->max_redirects(3);
$t->ua->max_redirects(3);
$t->get_ok('/redirect_named')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is(Location => undef)->element_exists('#foo')
->element_exists_not('#bar')->text_isnt('div' => 'Redirect')
->text_is('div' => 'Redirect works!')->text_unlike('[id="foo"]' => qr/Foo/)
->text_like('[id="foo"]' => qr/^Redirect/);
$t->max_redirects(0);
$t->ua->max_redirects(0);
Test::Mojo->new->tx($t->tx->previous)->status_is(302)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
Expand Down
112 changes: 0 additions & 112 deletions t/mojolicious/session_lite_app.t

This file was deleted.

116 changes: 116 additions & 0 deletions t/mojolicious/tls_lite_app.t
@@ -0,0 +1,116 @@
use Mojo::Base -strict;

# Disable Bonjour, IPv6 and libev
BEGIN {
$ENV{MOJO_NO_BONJOUR} = $ENV{MOJO_NO_IPV6} = 1;
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More;
use Mojo::IOLoop::Server;
plan skip_all => 'set TEST_TLS to enable this test (developer only!)'
unless $ENV{TEST_TLS};
plan skip_all => 'IO::Socket::SSL 1.37 required for this test!'
unless Mojo::IOLoop::Server::TLS;
plan tests => 40;

# "Look at these low, low prices on famous brand-name electronics!
# Don't be a sap, Dad. These are just crappy knockoffs.
# Pfft. I know a genuine Panaphonics when I see it.
# And look, there's a Magnetbox and Sorny."
use Mojo::IOLoop;
use Mojo::UserAgent;
use Mojolicious::Lite;
use Test::Mojo;

# Silence
app->log->level('fatal');

# Secure sessions
app->sessions->secure(1);

# GET /login
get '/login' => sub {
my $self = shift;
my $name = $self->param('name') || 'anonymous';
$self->session(name => $name);
$self->render_text("Welcome $name!");
};

# GET /again
get '/again' => sub {
my $self = shift;
my $name = $self->session('name') || 'anonymous';
$self->render_text("Welcome back $name!");
};

# GET /logout
get '/logout' => sub {
my $self = shift;
$self->session(expires => 1);
$self->redirect_to('login');
};

# Use HTTPS
my $t = Test::Mojo->new;
$t->ua->max_redirects(5);
$t->reset_session->ua->test_server('https');

# GET /login
$t->get_ok('/login?name=sri' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome sri!');
ok $t->tx->res->cookie('mojolicious')->expires, 'session cookie expires';
ok $t->tx->res->cookie('mojolicious')->secure, 'session cookie is secure';

# GET /again
$t->get_ok('/again' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome back sri!');

# GET /logout
$t->get_ok('/logout' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome anonymous!');

# GET /again (expired session)
$t->get_ok('/again' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome back anonymous!');

# GET /logout (no session)
$t->get_ok('/logout' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome anonymous!');

# Use HTTP
$t->reset_session->ua->test_server('http');

# GET /login
$t->reset_session->get_ok('/login?name=sri')->status_is(200)
->content_is('Welcome sri!');

# GET /again
$t->get_ok('/again')->status_is(200)->content_is('Welcome back anonymous!');

# Use HTTPS again (without expiration)
$t->reset_session->ua->test_server('https');
app->sessions->default_expiration(0);

# GET /login
$t->get_ok('/login?name=sri' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome sri!');
ok !$t->tx->res->cookie('mojolicious')->expires,
'session cookie does not expire';
ok $t->tx->res->cookie('mojolicious')->secure, 'session cookie is secure';

# GET /again
$t->get_ok('/again' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome back sri!');

# GET /logout
$t->get_ok('/logout' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome anonymous!');

# GET /again (expired session)
$t->get_ok('/again' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome back anonymous!');

# GET /logout (no session)
$t->get_ok('/logout' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome anonymous!');
4 changes: 2 additions & 2 deletions t/pod_coverage.t
Expand Up @@ -13,8 +13,8 @@ my @sunglasses = (qw/on_progress on_read on_request on_resume on_start/);
# DEPRECATED in Leaf Fluttering In Wind!
my @leaf = (
qw/add_hook comment connect connection_timeout is_done keep_alive_timeout/,
qw/listen on_close on_error on_finish on_lock on_process on_read/,
qw/on_unlock port run_hook run_hook_reverse timeout version write/
qw/listen max_redirects on_close on_error on_finish on_lock on_process/,
qw/on_read on_unlock port run_hook run_hook_reverse timeout version write/
);

# "Marge, I'm going to miss you so much. And it's not just the sex.
Expand Down

0 comments on commit 6ba33b6

Please sign in to comment.