Skip to content

Commit

Permalink
removed test_server method from Test::Mojo and scheme argument from t…
Browse files Browse the repository at this point in the history
…est_server method in Mojo::UserAgent
  • Loading branch information
kraih committed Dec 31, 2011
1 parent 9733a6d commit 3a1d0b7
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 110 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,8 +1,10 @@
This file documents the revision history for Perl extension Mojolicious.

2.42 2011-12-30 00:00:00
2.42 2011-12-31 00:00:00
- 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
11 changes: 7 additions & 4 deletions lib/Mojo/CookieJar.pm
Expand Up @@ -112,9 +112,11 @@ sub inject {

# Take delicious cookies from the jar
return unless keys %{$self->{jar}};
my $req = $tx->req;
my $url = $req->url->clone;
if (my $host = $req->headers->host) { $url->host($host) }
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) }
$req->cookies($self->find($url));
}

Expand Down Expand Up @@ -180,7 +182,8 @@ Find L<Mojo::Cookie::Request> objects in the jar for L<Mojo::URL> object.
$jar = $jar->inject($tx);
Inject request cookies into transaction.
Inject request cookies into transaction, the C<X-Forwarded-HTTPS> header can
be used to emulate a secure connection.
=head1 SEE ALSO
Expand Down
49 changes: 20 additions & 29 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -45,8 +45,7 @@ sub app {
# Try to detect application
$self->{app} ||= $ENV{MOJO_APP} if ref $ENV{MOJO_APP};
if ($app) {
$self->{app} =
ref $app ? $app : $self->_test_server->app_class($app)->app;
$self->{app} = ref $app ? $app : $self->_server->app_class($app)->app;
return $self;
}

Expand Down Expand Up @@ -135,13 +134,12 @@ sub test_server {
my $self = shift;

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

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

sub websocket {
Expand Down Expand Up @@ -440,6 +438,23 @@ sub _redirect {
return 1;
}

sub _server {
my $self = shift;

# Start test server
return $self->{server} if $self->{port};
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"]);
$server->prepare_ioloop;
warn "TEST SERVER STARTED (http://*:$port)\n" if DEBUG;

return $server;
}

sub _start {
my ($self, $tx, $cb) = @_;

Expand Down Expand Up @@ -483,28 +498,6 @@ sub _start {
return $id;
}

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

# Fresh start
delete $self->{port} if $scheme;

# Start test server
unless ($self->{port}) {
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;
$self->{scheme} = $scheme ||= 'http';
$server->listen(["$scheme://*:$port"]);
$server->prepare_ioloop;
warn "TEST SERVER STARTED ($scheme://*:$port)\n" if DEBUG;
}

return $self->{server};
}

sub _upgrade {
my ($self, $id) = @_;

Expand Down Expand Up @@ -954,8 +947,6 @@ 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
15 changes: 2 additions & 13 deletions lib/Test/Mojo.pm
Expand Up @@ -286,8 +286,6 @@ sub status_isnt {
return $self;
}

sub test_server { shift->ua->test_server(@_) }

sub text_is {
my ($self, $selector, $value, $desc) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
Expand Down Expand Up @@ -430,6 +428,8 @@ Current transaction, usually a L<Mojo::Transaction::HTTP> object.
User agent used for testing, defaults to a L<Mojo::UserAgent> object.
$t->get_ok($t->ua->test_server->userinfo('sri:secr3t')->path('/secrets'));
=head1 METHODS
L<Test::Mojo> inherits all methods from L<Mojo::Base> and implements the
Expand Down Expand Up @@ -699,17 +699,6 @@ Check response status for exact match.
Opposite of C<status_is>.
=head2 C<test_server>
my $url = $t->test_server;
my $url = $t->test_server('http');
my $url = $t->test_server('https');
Alias for L<Mojo::UserAgent/"test_server">. Note that this method is
EXPERIMENTAL and might change without warning!
$t->get_ok($t->test_server->userinfo('sri:secr3t')->path('/protected'));
=head2 C<text_is>
$t = $t->text_is('div.foo[x=y]' => 'Hello!');
Expand Down
4 changes: 2 additions & 2 deletions t/mojolicious/app.t
Expand Up @@ -29,7 +29,7 @@ is $t->app->sessions->cookie_domain, '.example.com', 'right domain';
is $t->app->sessions->cookie_path, '/bar', 'right path';

# Foo::fun
my $url = $t->test_server;
my $url = $t->ua->test_server;
$url->path('/fun/time');
$t->get_ok($url, {'X-Test' => 'Hi there!'})->status_isnt(404)->status_is(200)
->header_isnt('X-Bender' => 'Bite my shiny metal ass!')
Expand Down Expand Up @@ -98,7 +98,7 @@ $t->get_ok('/fun/time', {'X-Test' => 'Hi there!'})->status_is(200)
->content_is('Have fun!');

# Foo::fun
$url = $t->test_server;
$url = $t->ua->test_server;
$url->path('/fun/time');
$t->get_ok($url, {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => undef)->header_is(Server => 'Mojolicious (Perl)')
Expand Down
6 changes: 3 additions & 3 deletions t/mojolicious/lite_app.t
Expand Up @@ -833,9 +833,9 @@ $t->get_ok('/regex/in/template')->status_is(200)
->content_is("test(test)(\\Qtest\\E)(\n");

# GET /stream (with basic auth)
$t->get_ok(
$t->test_server->userinfo('sri:foo')->path('/stream')->query(foo => 'bar'))
->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
$t->get_ok($t->ua->test_server->userinfo('sri:foo')->path('/stream')
->query(foo => 'bar'))->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr#^foobarsri\:foohttp://localhost\:\d+/stream$#);

Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/longpolling_lite_app.t
Expand Up @@ -300,7 +300,7 @@ is $longpoll, 'finished!', 'finished';

# GET /longpoll (interrupted)
$longpoll = undef;
my $port = $t->test_server->port;
my $port = $t->ua->test_server->port;
Mojo::IOLoop->client(
{port => $port} => sub {
my ($loop, $err, $stream) = @_;
Expand Down
122 changes: 65 additions & 57 deletions t/mojolicious/tls_lite_app.t
Expand Up @@ -6,13 +6,7 @@ BEGIN {
$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 => 38;
use Test::More tests => 40;

# "Look at these low, low prices on famous brand-name electronics!
# Don't be a sap, Dad. These are just crappy knockoffs.
Expand Down Expand Up @@ -52,53 +46,67 @@ get '/logout' => sub {
$self->render_text("Bye $name!");
};

my $t = Test::Mojo->new;

# Use HTTPS
$t->reset_session->test_server('https');

# GET /login
$t->get_ok('/login?name=sri')->status_is(200)->content_is('Welcome sri!');
ok $t->tx->res->cookie('mojolicious')->expires, 'session cookie expires';

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

# GET /logout
$t->get_ok('/logout')->status_is(200)->content_is('Bye sri!');

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

# GET /logout (no session)
$t->get_ok('/logout')->status_is(200)->content_is('Bye anonymous!');

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

# GET /login
$t->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->test_server('https');
app->sessions->default_expiration(0);

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

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

# GET /logout
$t->get_ok('/logout')->status_is(200)->content_is('Bye sri!');

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

# GET /logout (no session)
$t->get_ok('/logout')->status_is(200)->content_is('Bye anonymous!');
# Emulate HTTPS
{
my $t = Test::Mojo->new;
local $ENV{MOJO_REVERSE_PROXY} = 1;

# GET /login (HTTPS)
$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 (HTTPS)
$t->get_ok('/again' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome back sri!');

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

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

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

# Use HTTP
$t->reset_session;

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

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

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

# GET /login (HTTPS)
$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 (HTTPS)
$t->get_ok('/again' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome back sri!');

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

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

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

0 comments on commit 3a1d0b7

Please sign in to comment.