Skip to content

Commit

Permalink
added experimental method test_server to Mojo::Client
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 24, 2010
1 parent 7fbfcef commit 0bd359b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -5,6 +5,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Added EXPERIMENTAL method detour to MojoX::Dispatcher::Routes.
- Added EXPERIMENTAL attribute partial to MojoX::Routes.
- Added EXPERIMENTAL CSS3 selector tests to Test::Mojo.
- Added EXPERIMENTAL method test_server to Mojo::Client.
- Added graceful shutdown support to Mojo::Server::Daemon.
- Added routing guide.
- Added relative path support to Mojo::URL. (marcus)
Expand Down
58 changes: 32 additions & 26 deletions lib/Mojo/Client.pm
Expand Up @@ -443,6 +443,30 @@ sub send_message {
$tx->send_message(@_);
}

# It's like my dad always said: eventually, everybody gets shot.
sub test_server {
my $self = shift;

# Server
unless ($self->{_port}) {
my $server = $self->{_server} =
Mojo::Server::Daemon->new(ioloop => $self->ioloop, silent => 1);
my $port = $self->{_port} = $self->ioloop->generate_port;
die "Couldn't find a free TCP port for testing.\n" unless $port;
$server->listen("http://*:$port");
$server->prepare_ioloop;
}

# Application
my $server = $self->{_server};
delete $server->{app};
my $app = $self->app;
ref $app ? $server->app($app) : $server->app_class($app);
$self->log($server->app->log);

return $self->{_port};
}

# Are we there yet?
# No
# Are we there yet?
Expand Down Expand Up @@ -820,9 +844,6 @@ sub _pipeline_info {
sub _prepare_pipeline {
my ($self, $p, $cb) = @_;

# Embedded server
$self->_prepare_server if $self->app;

# Prepare all transactions
for my $tx (@$p) {

Expand All @@ -832,7 +853,7 @@ sub _prepare_pipeline {
next if $url->host;
$url->scheme('http');
$url->host('localhost');
$url->port($self->{_port});
$url->port($self->test_server);
$tx->req->url($url);
}

Expand Down Expand Up @@ -890,28 +911,6 @@ sub _prepare_pipeline {
return $id;
}

# It's like my dad always said: eventually, everybody gets shot.
sub _prepare_server {
my $self = shift;

# Server
unless ($self->{_port}) {
my $server = $self->{_server} =
Mojo::Server::Daemon->new(ioloop => $self->ioloop, silent => 1);
my $port = $self->{_port} = $self->ioloop->generate_port;
die "Couldn't find a free TCP port for testing.\n" unless $port;
$server->listen("http://*:$port");
$server->prepare_ioloop;
}

# Application
my $server = $self->{_server};
delete $server->{app};
my $app = $self->app;
ref $app ? $server->app($app) : $server->app_class($app);
$self->log($server->app->log);
}

# Hey, Weener Boy... where do you think you'e going?
sub _proxy_connect {
my ($self, $p, $cb) = @_;
Expand Down Expand Up @@ -1712,6 +1711,13 @@ everywhere inside the process.
Send a message via WebSocket, only available from callbacks.
=head2 C<test_server>
my $port = $client->test_server;
Starts a test server for C<app> if neccessary and returns the port number.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<websocket>
$client = $client->websocket('ws://localhost:3000' => sub {...});
Expand Down
10 changes: 6 additions & 4 deletions t/mojolicious/lite_app.t
Expand Up @@ -62,7 +62,7 @@ get '/null/:null' => sub {
get '/stream' => sub {
my $self = shift;
my $counter = 0;
my $chunks = ['foo', 'bar'];
my $chunks = ['foo', 'bar', $self->req->url->to_abs->userinfo];
my $chunked = Mojo::Filter::Chunked->new;
$self->res->code(200);
$self->res->headers->content_type('text/plain');
Expand Down Expand Up @@ -469,10 +469,12 @@ $t->get_ok('/null/0')->status_is(200)
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/layouted 0/);

# GET /stream
$t->get_ok('/stream')->status_is(200)
# GET /stream (with basic auth)
my $port = $t->client->test_server;
$t->get_ok("http://sri:foo\@localhost:$port/stream")->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('foobar');
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('foobarsri:foo');

# GET / (IRI)
$t->get_ok('/привет/мир')->status_is(200)
Expand Down

0 comments on commit 0bd359b

Please sign in to comment.