Skip to content

Commit

Permalink
renamed test_server method in Mojo::UserAgent to app_url
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 31, 2011
1 parent 6ba33b6 commit c578195
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 36 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,10 +1,11 @@
This file documents the revision history for Perl extension Mojolicious.

2.42 2011-12-31 00:00:00
2.42 2012-01-01 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.
- Renamed test_server method in Mojo::UserAgent to app_url.
- Improved RFC 6265 compliance of generated request headers.
- Improved documentation.
- Fixed bug that prevented sessions without expiration.
Expand Down
47 changes: 24 additions & 23 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -52,6 +52,18 @@ sub app {
return $self->{app};
}

sub app_url {
my $self = shift;

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

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

sub build_form_tx { shift->transactor->form(@_) }
sub build_tx { shift->transactor->tx(@_) }
sub build_websocket_tx { shift->transactor->websocket(@_) }
Expand Down Expand Up @@ -130,18 +142,6 @@ sub start {
return $tx;
}

sub test_server {
my $self = shift;

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

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

sub websocket {
my $self = shift;
$self->start($self->build_websocket_tx(@_));
Expand Down Expand Up @@ -466,7 +466,7 @@ sub _start {
if ($self->app) {
my $req = $tx->req;
my $url = $req->url->to_abs;
$req->url($url->base($self->test_server)->to_abs) unless $url->host;
$req->url($url->base($self->app_url)->to_abs) unless $url->host;
}

# Proxy
Expand Down Expand Up @@ -807,6 +807,17 @@ L<Mojolicious> object.
$ua->app->log->level('fatal');
$ua->app->defaults(testing => 'oh yea!');
=head2 C<app_url>
my $url = $ua->app_url;
my $url = $ua->app_url('http');
my $url = $ua->app_url('https');
Get absolute L<Mojo::URL> object for C<app> and switch protocol if necessary.
Note that this method is EXPERIMENTAL and might change without warning!
say $ua->app_url->port;
=head2 C<build_form_tx>
my $tx = $ua->build_form_tx('http://kraih.com/foo' => {test => 123});
Expand Down Expand Up @@ -948,16 +959,6 @@ transactions non-blocking.
});
Mojo::IOLoop->start;
=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
change without warning!
=head2 C<websocket>
$ua->websocket('ws://localhost:3000' => sub {...});
Expand Down
2 changes: 1 addition & 1 deletion lib/Test/Mojo.pm
Expand Up @@ -430,7 +430,7 @@ 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'));
$t->get_ok($t->ua->app_url->userinfo('sri:secr3t')->path('/secrets'));
=head1 METHODS
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/app.t
Expand Up @@ -65,7 +65,7 @@ $app->routes->post(
$app->routes->any('/*whatever' => {text => 'Your Mojo is working!'});

# Continue
my $port = $ua->test_server->port;
my $port = $ua->app_url->port;
my $buffer = '';
my $id;
$id = Mojo::IOLoop->client(
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/websocket.t
Expand Up @@ -200,7 +200,7 @@ ok $body =~ /^(\d+)failed!$/, 'right content';
ok $1 < 100, 'right timeout';

# WebSocket /socket (using an already prepared socket)
my $port = $ua->test_server->port;
my $port = $ua->app_url->port;
my $tx = $ua->build_websocket_tx('ws://lalala/socket');
my $finished = 0;
$tx->on(finish => sub { $finished++ });
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->ua->test_server;
my $url = $t->ua->app_url;
$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->ua->test_server;
$url = $t->ua->app_url;
$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->ua->test_server->userinfo('sri:foo')->path('/stream')
->query(foo => 'bar'))->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
$t->get_ok(
$t->ua->app_url->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->ua->test_server->port;
my $port = $t->ua->app_url->port;
Mojo::IOLoop->client(
{port => $port} => sub {
my ($loop, $err, $stream) = @_;
Expand Down
6 changes: 3 additions & 3 deletions t/mojolicious/tls_lite_app.t
Expand Up @@ -54,7 +54,7 @@ get '/logout' => sub {
# Use HTTPS
my $t = Test::Mojo->new;
$t->ua->max_redirects(5);
$t->reset_session->ua->test_server('https');
$t->reset_session->ua->app_url('https');

# GET /login
$t->get_ok('/login?name=sri' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
Expand All @@ -79,7 +79,7 @@ $t->get_ok('/logout' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_is('Welcome anonymous!');

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

# GET /login
$t->reset_session->get_ok('/login?name=sri')->status_is(200)
Expand All @@ -89,7 +89,7 @@ $t->reset_session->get_ok('/login?name=sri')->status_is(200)
$t->get_ok('/again')->status_is(200)->content_is('Welcome back anonymous!');

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

# GET /login
Expand Down

0 comments on commit c578195

Please sign in to comment.