Skip to content

Commit

Permalink
better proxy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 17, 2013
1 parent 6f002f6 commit 0a591b3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,8 +1,9 @@

3.91 2013-03-17
- Improved bad charset handling in Mojo::DOM::HTML.
- Improved documentation.
- Improved tests.
- Fixed HTTPS proxy support in Mojo::UserAgent.
- Fixed HTTPS proxy support for blocking requests in Mojo::UserAgent.
- Fixed support for RFC 2817 in Mojo::Message::Request.
- Fixed whitespace bug in Mojo::DOM::HTML.
- Fixed proxy detection bug in get command.
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/websocket.t
Expand Up @@ -170,7 +170,7 @@ $ua->websocket(
$loop->start;
like $result, qr!test1test2ws://localhost:\d+/!, 'right result';

# Failed websocket connection
# Failed WebSocket connection
my ($code, $body, $ws);
$ua->websocket(
'/something/else' => sub {
Expand Down
27 changes: 14 additions & 13 deletions t/mojo/websocket_proxy.t
Expand Up @@ -38,12 +38,8 @@ websocket '/test' => sub {
};

# HTTP server for testing
my $ua = Mojo::UserAgent->new;
my $daemon = Mojo::Server::Daemon->new(
app => app,
ioloop => Mojo::IOLoop->singleton,
silent => 1
);
my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton);
my $daemon = Mojo::Server::Daemon->new(app => app, silent => 1);
my $port = Mojo::IOLoop->new->generate_port;
$daemon->listen(["http://127.0.0.1:$port"])->start;

Expand Down Expand Up @@ -125,7 +121,7 @@ Mojo::IOLoop->server(
}
);

# Normal request
# Normal non-blocking request
my $result;
$ua->get(
"http://localhost:$port/" => sub {
Expand All @@ -136,7 +132,7 @@ $ua->get(
Mojo::IOLoop->start;
is $result, "Hello World! / http://localhost:$port/", 'right content';

# Normal websocket
# Normal WebSocket
$result = undef;
$ua->websocket(
"ws://localhost:$port/test" => sub {
Expand All @@ -155,12 +151,12 @@ $ua->websocket(
Mojo::IOLoop->start;
is $result, 'test1test2', 'right result';

# Proxy request
# Non-blocking proxy request
$ua->http_proxy("http://localhost:$port");
my $kept_alive;
$result = undef;
$ua->get(
"http://kraih.com/proxy" => sub {
'http://kraih.com/proxy' => sub {
my ($ua, $tx) = @_;
$kept_alive = $tx->kept_alive;
$result = $tx->success->body;
Expand All @@ -171,7 +167,7 @@ Mojo::IOLoop->start;
ok !$kept_alive, 'connection was not kept alive';
is $result, 'http://kraih.com/proxy', 'right content';

# Kept alive proxy websocket
# Kept alive proxy WebSocket
($kept_alive, $result) = ();
$ua->websocket(
"ws://localhost:$port/test" => sub {
Expand All @@ -192,7 +188,12 @@ Mojo::IOLoop->start;
ok $kept_alive, 'connection was kept alive';
is $result, 'test1test2', 'right result';

# Proxy websocket
# Blocking proxy request
my $tx = $ua->get('http://kraih.com/proxy');
is $tx->res->code, 200, 'right status';
is $tx->success->body, 'http://kraih.com/proxy', 'right content';

# Proxy WebSocket
$ua = Mojo::UserAgent->new(http_proxy => "http://localhost:$proxy");
$result = undef;
$ua->websocket(
Expand All @@ -215,7 +216,7 @@ is $result, 'test1test2', 'right result';
ok $read > 25, 'read enough';
ok $sent > 25, 'sent enough';

# Proxy websocket with bad target
# Proxy WebSocket with bad target
$ua->http_proxy("http://localhost:$proxy");
my $port2 = $port + 1;
my ($success, $err);
Expand Down
31 changes: 17 additions & 14 deletions t/mojo/websocket_proxy_tls.t
Expand Up @@ -53,11 +53,7 @@ websocket '/test' => sub {
};

# Web server with valid certificates
my $daemon = Mojo::Server::Daemon->new(
app => app,
ioloop => Mojo::IOLoop->singleton,
silent => 1
);
my $daemon = Mojo::Server::Daemon->new(app => app, silent => 1);
my $port = Mojo::IOLoop->new->generate_port;
my $listen
= "https://127.0.0.1:$port"
Expand Down Expand Up @@ -146,12 +142,13 @@ Mojo::IOLoop->server(

# User agent with valid certificates
my $ua = Mojo::UserAgent->new(
ca => 't/mojo/certs/ca.crt',
cert => 't/mojo/certs/client.crt',
key => 't/mojo/certs/client.key'
ioloop => Mojo::IOLoop->singleton,
ca => 't/mojo/certs/ca.crt',
cert => 't/mojo/certs/client.crt',
key => 't/mojo/certs/client.key'
);

# Normal request
# Normal non-blocking request
my $result;
$ua->get(
"https://localhost:$port/" => sub {
Expand Down Expand Up @@ -186,7 +183,7 @@ is $works, 'it does!', 'right header';
is $start, 2, 'redirected once';
$ua->unsubscribe('start');

# Normal websocket
# Normal WebSocket
$result = undef;
$ua->websocket(
"wss://localhost:$port/test" => sub {
Expand All @@ -205,7 +202,7 @@ $ua->websocket(
Mojo::IOLoop->start;
is $result, 'test1test2', 'right result';

# Proxy request
# Non-blocking proxy request
$ua->https_proxy("http://sri:secr3t\@localhost:$proxy");
$result = undef;
my ($auth, $kept_alive);
Expand All @@ -223,7 +220,7 @@ ok !$auth, 'no "Proxy-Authorization" header';
ok !$kept_alive, 'connection was not kept alive';
is $result, "https://localhost:$port/proxy", 'right content';

# Kept alive proxy request
# Non-blocking kept alive proxy request
($kept_alive, $result) = ();
$ua->get(
"https://localhost:$port/proxy" => sub {
Expand All @@ -237,7 +234,7 @@ Mojo::IOLoop->start;
is $result, "https://localhost:$port/proxy", 'right content';
ok $kept_alive, 'connection was kept alive';

# Kept alive proxy websocket
# Kept alive proxy WebSocket
$ua->https_proxy("http://localhost:$proxy");
($kept_alive, $result) = ();
$ua->websocket(
Expand All @@ -262,7 +259,13 @@ is $result, 'test1test2', 'right result';
ok $read > 25, 'read enough';
ok $sent > 25, 'sent enough';

# Proxy websocket with bad target
# Blocking proxy request
$ua->https_proxy("http://sri:secr3t\@localhost:$proxy");
my $tx = $ua->get("https://localhost:$port/proxy");
is $tx->res->code, 200, 'right status';
is $tx->success->body, "https://localhost:$port/proxy", 'right content';

# Proxy WebSocket with bad target
$ua->https_proxy("http://localhost:$proxy");
my $port2 = $port + 1;
my ($success, $err);
Expand Down

0 comments on commit 0a591b3

Please sign in to comment.