Skip to content

Commit

Permalink
slightly more consistent use of closures in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 15, 2015
1 parent 03a10ea commit a326162
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 94 deletions.
7 changes: 1 addition & 6 deletions t/mojo/user_agent_socks.t
Expand Up @@ -141,12 +141,7 @@ $ua->websocket(
'/echo' => sub {
my ($ua, $tx) = @_;
$id = $tx->connection;
$tx->on(
message => sub {
$result = pop;
Mojo::IOLoop->stop;
}
);
$tx->on(message => sub { $result = pop; Mojo::IOLoop->stop });
$tx->send('test');
}
);
Expand Down
39 changes: 5 additions & 34 deletions t/mojo/websocket.t
Expand Up @@ -60,12 +60,7 @@ websocket '/socket' => sub {
websocket '/early_start' => sub {
my $c = shift;
$c->send('test1');
$c->on(
message => sub {
my ($c, $msg) = @_;
$c->send("${msg}test2")->finish;
}
);
$c->on(message => sub { shift->send(shift . 'test2')->finish });
};

websocket '/denied' => sub {
Expand Down Expand Up @@ -145,13 +140,7 @@ $ua->websocket(
'/' => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result = $msg;
$tx->finish;
}
);
$tx->on(message => sub { $result = pop; $tx->finish });
$tx->send('test1');
}
);
Expand Down Expand Up @@ -352,13 +341,7 @@ $ua->websocket(
'/squish' => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result = $msg;
$tx->finish;
}
);
$tx->on(message => sub { $result = pop; $tx->finish });
$tx->send(b(' foo bar '));
}
);
Expand Down Expand Up @@ -404,13 +387,7 @@ $status = undef;
$ua->websocket(
'/close' => sub {
my ($ua, $tx) = @_;
$tx->on(
finish => sub {
my ($tx, $code) = @_;
$status = $code;
Mojo::IOLoop->stop;
}
);
$tx->on(finish => sub { $status = pop; Mojo::IOLoop->stop });
$tx->send('test1');
}
);
Expand All @@ -423,13 +400,7 @@ $ua->websocket(
'/echo' => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result = $msg;
$tx->finish;
}
);
$tx->on(message => sub { $result = pop; $tx->finish });
$tx->send('hi!' x 100);
}
);
Expand Down
34 changes: 6 additions & 28 deletions t/mojo/websocket_proxy.t
Expand Up @@ -25,12 +25,7 @@ get '/proxy' => sub {

websocket '/test' => sub {
my $c = shift;
$c->on(
message => sub {
my ($c, $msg) = @_;
$c->send("${msg}test2");
}
);
$c->on(message => sub { shift->send(shift . 'test2') });
};

# HTTP server for testing
Expand Down Expand Up @@ -121,7 +116,8 @@ my $proxy = Mojo::IOLoop->acceptor($id)->port;
my $result;
$ua->get(
"http://127.0.0.1:$port/" => sub {
$result = pop->res->body;
my ($ua, $tx) = @_;
$result = $tx->res->body;
Mojo::IOLoop->stop;
}
);
Expand All @@ -134,13 +130,7 @@ $ua->websocket(
"ws://127.0.0.1:$port/test" => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result = $msg;
$tx->finish;
}
);
$tx->on(message => sub { $result = pop; $tx->finish });
$tx->send('test1');
}
);
Expand Down Expand Up @@ -170,13 +160,7 @@ $ua->websocket(
my ($ua, $tx) = @_;
$kept_alive = $tx->kept_alive;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result = $msg;
$tx->finish;
}
);
$tx->on(message => sub { $result = pop; $tx->finish });
$tx->send('test1');
}
);
Expand All @@ -197,13 +181,7 @@ $ua->websocket(
"ws://127.0.0.1:$port/test" => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result = $msg;
$tx->finish;
}
);
$tx->on(message => sub { $result = pop; $tx->finish });
$tx->send('test1');
}
);
Expand Down
32 changes: 6 additions & 26 deletions t/mojo/websocket_proxy_tls.t
Expand Up @@ -40,12 +40,7 @@ get '/proxy' => sub {

websocket '/test' => sub {
my $c = shift;
$c->on(
message => sub {
my ($c, $msg) = @_;
$c->send("${msg}test2");
}
);
$c->on(message => sub { shift->send(shift . 'test2') });
};

# Web server with valid certificates
Expand Down Expand Up @@ -161,7 +156,8 @@ my $ua = Mojo::UserAgent->new(
my $result;
$ua->get(
"https://127.0.0.1:$port/" => sub {
$result = pop->res->body;
my ($ua, $tx) = @_;
$result = $tx->res->body;
Mojo::IOLoop->stop;
}
);
Expand All @@ -171,11 +167,7 @@ is $result, "Hello World! / https://127.0.0.1:$port/", 'right content';
# Broken redirect
my $start;
$ua->on(
start => sub {
$start++;
pop->req->headers->header('X-Works', 'it does!');
}
);
start => sub { $start++; pop->req->headers->header('X-Works', 'it does!') });
$result = undef;
my $works;
$ua->max_redirects(3)->get(
Expand All @@ -198,13 +190,7 @@ $ua->websocket(
"wss://127.0.0.1:$port/test" => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result = $msg;
$tx->finish;
}
);
$tx->on(message => sub { $result = pop; $tx->finish });
$tx->send('test1');
}
);
Expand Down Expand Up @@ -251,13 +237,7 @@ $ua->websocket(
my ($ua, $tx) = @_;
$kept_alive = $tx->kept_alive;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result = $msg;
$tx->finish;
}
);
$tx->on(message => sub { $result = pop; $tx->finish });
$tx->send('test1');
}
);
Expand Down

0 comments on commit a326162

Please sign in to comment.