Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 11, 2010
1 parent 3f0b598 commit 4b5f238
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
27 changes: 27 additions & 0 deletions lib/Mojo/HelloWorld.pm
Expand Up @@ -6,6 +6,7 @@ use warnings;
use base 'Mojo';

use Mojo::JSON;
use Mojo::Cookie::Response;

# 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.
Expand Down Expand Up @@ -35,6 +36,30 @@ sub handler {
$tx->resume;
}

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

# Response
my $res = $tx->res;

# Cookies
my $params = $tx->req->params->to_hash;
for my $key (sort keys %$params) {
$res->cookies(
Mojo::Cookie::Response->new(
name => $key,
value => $params->{$key}
)
);
}

# Response
$res->code(200);
$res->body('nomnomnom');

$tx->resume;
}

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

Expand Down Expand Up @@ -78,6 +103,7 @@ sub _diag {
unless $tx->res->headers->content_type;

# Dispatch
return $self->_cookies($tx) if $path =~ /^\/cookies/;
return $self->_chunked_params($tx) if $path =~ /^\/chunked_params/;
return $self->_dump_env($tx) if $path =~ /^\/dump_env/;
return $self->_dump_params($tx) if $path =~ /^\/dump_params/;
Expand All @@ -90,6 +116,7 @@ sub _diag {
<!doctype html><html>
<head><title>Mojo Diagnostics</title></head>
<body>
<a href="/diag/cookies">Cookies</a>
<a href="/diag/chunked_params">Chunked Request Parameters</a><br />
<a href="/diag/dump_env">Dump Environment Variables</a><br />
<a href="/diag/dump_params">Dump Request Parameters</a><br />
Expand Down
27 changes: 26 additions & 1 deletion t/mojo/psgi.t
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 15;
use Test::More tests => 18;

use Mojo::JSON;

Expand Down Expand Up @@ -88,3 +88,28 @@ is_deeply $params,
},
'right structure';
is $ENV{MOJO_HELLO}, 'world', 'on_finish callback';

# Cookies
$env = {
CONTENT_LENGTH => 0,
PATH_INFO => '/diag/cookies',
QUERY_STRING => 'lalala=23&bar=baz',
REQUEST_METHOD => 'GET',
SCRIPT_NAME => '/',
HTTP_HOST => 'localhost:8080',
SERVER_PROTOCOL => 'HTTP/1.1',
'psgi.version' => [1, 0],
'psgi.url_scheme' => 'http',
'psgi.input' => *STDIN,
'psgi.errors' => *STDERR,
'psgi.multithread' => 0,
'psgi.multiprocess' => 1,
'psgi.run_once' => 0
};
$app = Mojolicious::Command::Psgi->new->run;
$res = $app->($env);
is $res->[0], 200, 'right status';
is scalar @{$res->[1]}, 10, 'right number of values';
my $i = 0;
for my $header (@{$res->[1]}) { $i++ if $header eq 'Set-Cookie' }
is $i, 2, 'right number of "Set-Cookie" headers';

0 comments on commit 4b5f238

Please sign in to comment.