Navigation Menu

Skip to content

Commit

Permalink
improved CGI and PSGI tests a little
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 28, 2011
1 parent 45eae00 commit 53c9456
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 186 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@
This file documents the revision history for Perl extension Mojolicious.

2.33 2011-11-27 00:00:00
2.33 2011-11-28 00:00:00
- Improved Mojo::EventEmitter performance slightly.
- Improved documentation.
- Fixed a few small inline template issues.
Expand Down
4 changes: 4 additions & 0 deletions lib/Mojo/CookieJar.pm
Expand Up @@ -61,6 +61,10 @@ sub extract {
$self->add(@cookies);
}

# "Dear Homer, IOU one emergency donut.
# Signed Homer.
# Bastard!
# He's always one step ahead."
sub find {
my ($self, $url) = @_;

Expand Down
193 changes: 28 additions & 165 deletions lib/Mojo/HelloWorld.pm
Expand Up @@ -7,190 +7,53 @@ use Mojolicious::Lite;
app->log->level('error');
app->log->path(undef);

# "Does whisky count as beer?"
under '/diag' => sub {
shift->on(finish => sub { $ENV{MOJO_HELLO} = 'world' });
};

any '/' => 'diag';

any '/chunked_params' => sub {
my $self = shift;

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

# Write with drain callback
my $cb;
$cb = sub {
my $self = shift;
my $chunk = shift @$chunks || '';
$self->write_chunk($chunk, $chunk ? $cb : undef);
};
$self->$cb();
};

any '/cookies' => sub {
my $self = shift;

# Turn parameters into cookies
my $params = $self->req->params->to_hash;
for my $key (sort keys %$params) {
$self->cookie($key, $params->{$key});
}
$self->render_text('nomnomnom');
};

any '/dump_env' => sub { shift->render_json(\%ENV) };

any '/dump_params' => sub {
my $self = shift;
$self->render_json($self->req->params->to_hash);
};

# "Dear Homer, IOU one emergency donut.
# Signed Homer.
# Bastard!
# He's always one step ahead."
any '/proxy' => sub {
my $self = shift;

# Blocking
my $res = $self->res;
if (my $blocking = $self->req->param('blocking')) {
my $res2 = $self->ua->get($blocking)->res;
$res->headers->content_type($res2->headers->content_type);
$self->render_data($res2->content->asset->slurp);
}

# Non-blocking
elsif (my $non_blocking = $self->req->param('non_blocking')) {
return $self->render_text('This is a blocking deployment environment!')
unless Mojo::IOLoop->is_running;
$self->render_later;
$self->ua->get(
$non_blocking => sub {
my $res2 = pop->res;
$res->headers->content_type($res2->headers->content_type);
$self->render_data($res2->content->asset->slurp);
}
);
}
};

any '/upload' => sub {
my $self = shift;

# Echo uploaded file
my $req = $self->req;
return unless my $file = $req->upload('file');
my $headers = $self->res->headers;
$headers->content_type($file->headers->content_type
|| 'application/octet-stream');
$headers->header('X-Upload-Limit-Exceeded' => 1)
if $req->is_limit_exceeded;
$self->render_data($file->slurp);
};

any '/websocket' => sub {
my $self = shift;
$self->on(message => sub { shift->send_message(shift) })
if $self->tx->is_websocket;
};

# "How is education supposed to make me feel smarter?
# Besides, every time I learn something new,
# it pushes some old stuff out of my brain.
# Remember when I took that home winemaking course,
# and I forgot how to drive?"
# "Does whisky count as beer?"
under->any('/*whatever' => {whatever => '', text => 'Your Mojo is working!'});

1;
__DATA__
@@ layouts/default.html.ep
@@ websocket.html.ep
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= content_for 'head' %>
<title>WebSocket</title>
% my $url = url_for->to_abs->scheme('ws');
%= javascript begin
var ws;
if ("MozWebSocket" in window) {
ws = new MozWebSocket('<%= $url %>');
}
else if ("WebSocket" in window) {
ws = new WebSocket('<%= $url %>');
}
if(typeof(ws) !== 'undefined') {
function wsmessage(event) {
data = event.data;
alert(data);
}
function wsopen(event) {
ws.send("WebSocket support works!");
}
ws.onmessage = wsmessage;
ws.onopen = wsopen;
}
else {
alert("Sorry, your browser does not support WebSockets.");
}
% end
</head>
<body>
<%= content %>
Testing WebSockets, please make sure you have JavaScript enabled.
</body>
</html>
@@ diag.html.ep
% layout 'default';
% title 'Mojo Diagnostics';
<%= link_to Cookies => '/diag/cookies' %><br>
<%= link_to 'Chunked Request Parameters' => '/diag/chunked_params' %><br>
<%= link_to 'Dump Environment Variables' => '/diag/dump_env' %><br>
<%= link_to 'Dump Request Parameters' => '/diag/dump_params' %><br>
<%= link_to Proxy => '/diag/proxy' %><br>
<%= link_to Upload => '/diag/upload' %><br>
<%= link_to WebSocket => '/diag/websocket' %><br>
@@ proxy.html.ep
% layout 'default';
% title 'Proxy';
Blocking:
%= form_for 'proxy' => begin
%= text_field 'blocking'
%= submit_button 'Fetch'
% end
<br>
Non-Blocking:
%= form_for 'proxy' => begin
%= text_field 'non_blocking'
%= submit_button 'Fetch'
% end
@@ upload.html.ep
% layout 'default';
% title 'Upload';
File:
<%= form_for 'upload', method => 'POST',
enctype => 'multipart/form-data' => begin %>
%= file_field 'file'
%= submit_button 'Upload'
<% end %>
@@ websocket.html.ep
% layout 'default';
% title 'WebSocket';
% content_for head => begin
% my $url = url_for->to_abs->scheme('ws');
%= javascript begin
var ws;
if ("MozWebSocket" in window) {
ws = new MozWebSocket('<%= $url %>');
}
else if ("WebSocket" in window) {
ws = new WebSocket('<%= $url %>');
}
if(typeof(ws) !== 'undefined') {
function wsmessage(event) {
data = event.data;
alert(data);
}
function wsopen(event) {
ws.send("WebSocket support works!");
}
ws.onmessage = wsmessage;
ws.onopen = wsopen;
}
else {
alert("Sorry, your browser does not support WebSockets.");
}
% end
% end
Testing WebSockets, please make sure you have JavaScript enabled.
__END__
=head1 NAME
Expand Down
5 changes: 5 additions & 0 deletions lib/Mojo/Path.pm
Expand Up @@ -83,6 +83,11 @@ sub to_abs_string {
return $self->leading_slash ? $self->to_string : ('/' . $self->to_string);
}

# "How is education supposed to make me feel smarter?
# Besides, every time I learn something new,
# it pushes some old stuff out of my brain.
# Remember when I took that home winemaking course,
# and I forgot how to drive?"
sub to_string {
my $self = shift;

Expand Down
44 changes: 40 additions & 4 deletions t/mojo/app.t
Expand Up @@ -19,16 +19,52 @@ use Mojo::Transaction::HTTP;
use Mojo::UserAgent;

use_ok 'Mojo';
use_ok 'Mojo::HelloWorld';
use_ok 'Mojolicious';

# Logger
my $logger = Mojo::Log->new;
my $app = Mojo->new({log => $logger});
is $app->log, $logger, 'right logger';

$app = Mojo::HelloWorld->new;
# Fresh application
$app = Mojolicious->new;
my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton)->app($app);

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

# POST /chunked
$app->routes->post(
'/chunked' => sub {
my $self = shift;

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

my $cb;
$cb = sub {
my $self = shift;
$cb = undef unless my $chunk = shift @$chunks || '';
$self->write_chunk($chunk, $cb);
};
$self->$cb();
}
);

# POST /upload
$app->routes->post(
'/upload' => sub {
my $self = shift;
$self->render_data($self->req->upload('file')->slurp);
}
);

# /*
$app->routes->any('/*whatever' => {text => 'Your Mojo is working!'});

# Continue
my $port = $ua->test_server->port;
my $buffer = '';
Expand Down Expand Up @@ -160,14 +196,14 @@ for my $i (1 .. 10) { $params->{"test$i"} = $i }
my $result = '';
for my $key (sort keys %$params) { $result .= $params->{$key} }
my ($code, $body);
$tx = $ua->post_form("http://127.0.0.1:$port/diag/chunked_params" => $params);
$tx = $ua->post_form("http://127.0.0.1:$port/chunked" => $params);
is $tx->res->code, 200, 'right status';
is $tx->res->body, $result, 'right content';

# Upload
($code, $body) = undef;
$tx = $ua->post_form(
"http://127.0.0.1:$port/diag/upload" => {file => {content => $result}});
"http://127.0.0.1:$port/upload" => {file => {content => $result}});
is $tx->res->code, 200, 'right status';
is $tx->res->body, $result, 'right content';

Expand Down

0 comments on commit 53c9456

Please sign in to comment.