Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 14, 2010
1 parent da9819c commit 61e39b0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 53 deletions.
83 changes: 45 additions & 38 deletions lib/Mojo/HelloWorld.pm
Expand Up @@ -25,34 +25,57 @@ sub new {
sub handler {
my ($self, $tx) = @_;

# Finished transaction
$tx->finished(sub { $ENV{MOJO_HELLO} = 'world' });
# Dispatch to diagnostics functions
return $self->_diag($tx) if defined $tx->req->url->path->parts->[0];

# Default to 200
$tx->res->code(200) unless $tx->is_websocket;
# Hello world!
my $res = $tx->res;
$res->code(200);
$res->headers->content_type('text/plain');
$res->body('Your Mojo is working!');
}

# Dispatch to diagnostics functions
return $self->_diag($tx) if index($tx->req->url->path, '/diag') == 0;
sub _chunked_params {
my ($self, $tx) = @_;

# WebSocket
return if $tx->is_websocket;
# Chunked
$tx->res->headers->transfer_encoding('chunked');

# Hello world!
$tx->res->headers->content_type('text/plain');
$tx->res->body('Congratulations, your Mojo is working!');
# Chunks
my $params = $tx->req->params->to_hash;
my $chunks = [];
for my $key (sort keys %$params) {
push @$chunks, $params->{$key};
}

# Callback
my $counter = 0;
my $chunked = Mojo::Filter::Chunked->new;
$tx->res->body(
sub {
my $self = shift;
my $chunk = $chunks->[$counter] || '';
$counter++;
return $chunked->build($chunk);
}
);
}

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

# Finished transaction
$tx->finished(sub { $ENV{MOJO_HELLO} = 'world' });

# Path
my $path = $tx->req->url->path;
$path =~ s/^\/diag//;
$path =~ s/^\/diag// or return $self->_hello($tx);

# WebSocket
return $self->_websocket($tx) if $path =~ /^\/websocket/;

# Defaults
$tx->res->code(200);
$tx->res->headers->content_type('text/plain')
unless $tx->res->headers->content_type;

Expand All @@ -78,32 +101,6 @@ sub _diag {
EOF
}

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

# Chunked
$tx->res->headers->transfer_encoding('chunked');

# Chunks
my $params = $tx->req->params->to_hash;
my $chunks = [];
for my $key (sort keys %$params) {
push @$chunks, $params->{$key};
}

# Callback
my $counter = 0;
my $chunked = Mojo::Filter::Chunked->new;
$tx->res->body(
sub {
my $self = shift;
my $chunk = $chunks->[$counter] || '';
$counter++;
return $chunked->build($chunk);
}
);
}

sub _dump_env {
my ($self, $tx) = @_;
my $res = $tx->res;
Expand All @@ -118,6 +115,16 @@ sub _dump_params {
$res->body(Mojo::JSON->new->encode($tx->req->params->to_hash));
}

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

# Hello world!
my $res = $tx->res;
$res->code(200);
$res->headers->content_type('text/plain');
$res->body('Your Mojo is working!');
}

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

Expand Down
2 changes: 1 addition & 1 deletion t/mojo/apache_cgi.t
Expand Up @@ -83,7 +83,7 @@ $client->get(
"http://127.0.0.1:$port/cgi-bin/test.cgi" => sub {
my $self = shift;
is($self->res->code, 200, 'right status');
like($self->res->body, qr/Mojo is working/, 'right content');
like($self->res->body, qr/Mojo/, 'right content');
}
)->process;

Expand Down
2 changes: 1 addition & 1 deletion t/mojo/apache_fastcgi.t
Expand Up @@ -84,7 +84,7 @@ $client->get(
"http://127.0.0.1:$port/" => sub {
my $self = shift;
is($self->res->code, 200, 'right status');
like($self->res->body, qr/Mojo is working/, 'right content');
like($self->res->body, qr/Mojo/, 'right content');
}
)->process;

Expand Down
10 changes: 5 additions & 5 deletions t/mojo/app.t
Expand Up @@ -71,11 +71,11 @@ $client->ioloop->connect(
read_cb => sub {
my ($self, $id, $chunk) = @_;
$buffer .= $chunk;
$self->drop($id) and $self->stop if $buffer =~ /working!.*working!/gs;
$self->drop($id) and $self->stop if $buffer =~ /Mojo.*Mojo/gs;
}
);
$client->ioloop->start;
like($buffer, qr/Mojo is working!/, 'transactions were pipelined');
like($buffer, qr/Mojo/, 'transactions were pipelined');

# Normal request
my $tx = Mojo::Transaction::HTTP->new;
Expand All @@ -84,7 +84,7 @@ $tx->req->url->parse('/5/');
$client->process($tx);
ok($tx->keep_alive, 'will be kept alive');
is($tx->res->code, 200, 'right status');
like($tx->res->body, qr/^Congratulations/, 'right content');
like($tx->res->body, qr/Mojo/, 'right content');

# POST request
$tx = Mojo::Transaction::HTTP->new;
Expand All @@ -94,7 +94,7 @@ $tx->req->headers->expect('fun');
$tx->req->body('foo bar baz' x 128);
$client->process($tx);
is($tx->res->code, 200, 'right status');
like($tx->res->body, qr/^Congratulations/, 'right content');
like($tx->res->body, qr/Mojo/, 'right content');

# POST request
$tx = Mojo::Transaction::HTTP->new;
Expand All @@ -105,7 +105,7 @@ $tx->req->body('bar baz foo' x 128);
$client->process($tx);
ok(defined $tx->connection, 'has connection id');
is($tx->res->code, 200, 'right status');
like($tx->res->body, qr/^Congratulations/, 'right content');
like($tx->res->body, qr/Mojo/, 'right content');

# Multiple requests
$tx = Mojo::Transaction::HTTP->new;
Expand Down
14 changes: 7 additions & 7 deletions t/mojo/daemon.t
Expand Up @@ -41,7 +41,7 @@ $client->process($tx);
ok($tx->is_done, 'transaction is done');
is($tx->res->code, 200, 'right status');
like($tx->res->headers->connection, qr/close/i, 'right "Connection" header');
like($tx->res->body, qr/Mojo is working/, 'right content');
like($tx->res->body, qr/Mojo/, 'right content');

# Multiple requests
$tx = Mojo::Transaction::HTTP->new;
Expand All @@ -67,7 +67,7 @@ is($tx->res->code, 200, 'right status');
is($tx2->res->code, 200, 'right status');
is($tx3->res->code, 200, 'right status');
is($tx4->res->code, 200, 'right status');
like($tx2->res->content->asset->slurp, qr/Mojo is working/, 'right content');
like($tx2->res->content->asset->slurp, qr/Mojo/, 'right content');

# Request
$tx = Mojo::Transaction::HTTP->new;
Expand All @@ -79,7 +79,7 @@ $client->process($tx);
is($tx->res->code, 200, 'right status');
like($tx->res->headers->connection,
qr/Keep-Alive/i, 'right "Connection" header');
like($tx->res->body, qr/Mojo is working/, 'right content');
like($tx->res->body, qr/Mojo/, 'right content');

# Second keep alive request
$tx = Mojo::Transaction::HTTP->new;
Expand All @@ -90,7 +90,7 @@ is($tx->res->code, 200, 'right status');
is($tx->kept_alive, 1, 'connection was alive');
like($tx->res->headers->connection,
qr/Keep-Alive/i, 'right "Connection" header');
like($tx->res->body, qr/Mojo is working/, 'right content');
like($tx->res->body, qr/Mojo/, 'right content');

# Third keep alive request
$tx = Mojo::Transaction::HTTP->new;
Expand All @@ -101,7 +101,7 @@ is($tx->res->code, 200, 'right status');
is($tx->kept_alive, 1, 'connection was kept alive');
like($tx->res->headers->connection,
qr/Keep-Alive/i, 'right "Connection" header');
like($tx->res->body, qr/Mojo is working/, 'right content');
like($tx->res->body, qr/Mojo/, 'right content');

# Multiple requests
$tx = Mojo::Transaction::HTTP->new;
Expand All @@ -115,7 +115,7 @@ ok($tx->is_done, 'transaction is done');
ok($tx2->is_done, 'transaction is done');
is($tx->res->code, 200, 'right status');
is($tx2->res->code, 200, 'right status');
like($tx2->res->content->asset->slurp, qr/Mojo is working/, 'right content');
like($tx2->res->content->asset->slurp, qr/Mojo/, 'right content');

# Multiple requests with a chunked response
$tx = Mojo::Transaction::HTTP->new;
Expand All @@ -142,7 +142,7 @@ is($tx->res->code, 200, 'right status');
is($tx2->res->code, 200, 'right status');
is($tx3->res->code, 200, 'right status');
is($tx4->res->code, 200, 'right status');
like($tx2->res->content->asset->slurp, qr/Mojo is working/, 'right content');
like($tx2->res->content->asset->slurp, qr/Mojo/, 'right content');
is($tx3->res->content->asset->slurp, 'foo12', 'right content');

# Stop
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/daemon_prefork.t
Expand Up @@ -31,7 +31,7 @@ $tx->req->url->parse("http://127.0.0.1:$port/");
my $client = Mojo::Client->new;
$client->process($tx);
is($tx->res->code, 200, 'right status');
like($tx->res->body, qr/Mojo is working/, 'right content');
like($tx->res->body, qr/Mojo/, 'right content');

# Stop
$server->stop_server_ok;

0 comments on commit 61e39b0

Please sign in to comment.