extend lua websocket testing



git-svn-id: https://svn.apache.org/repos/asf/httpd/test/framework/trunk@1901516 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/t/conf/ssl/proxyssl.conf.in b/t/conf/ssl/proxyssl.conf.in
index 3c86c13..161385b 100644
--- a/t/conf/ssl/proxyssl.conf.in
+++ b/t/conf/ssl/proxyssl.conf.in
@@ -51,6 +51,8 @@
 
         ProxyPass        / https://@proxyssl_url@/
         ProxyPassReverse / https://@proxyssl_url@/
+
+        ProxyPass /proxy/wsoc wss://localhost:@proxy_https_https_port@/modules/lua/websockets.lua  
     </VirtualHost>
 
     #here we can test http <-> https using SSLProxyMachine* inside <Proxy>
@@ -118,7 +120,6 @@
         ProxyPass        / http://@servername@:@port@/
         ProxyPassReverse / http://@servername@:@port@/
     </VirtualHost>
-
 </IfModule>
 
 </IfModule>
diff --git a/t/htdocs/modules/lua/websockets.lua b/t/htdocs/modules/lua/websockets.lua
index 1acd91b..6e6d5ba 100644
--- a/t/htdocs/modules/lua/websockets.lua
+++ b/t/htdocs/modules/lua/websockets.lua
@@ -2,6 +2,11 @@
 if r:wsupgrade() then -- if we can upgrade:
     while true do
       local line, isFinal = r:wsread() 
+      local len = string.len(line);
+      r:debug(string.format("writing line of len %d: %s", len, line))
+      if len >= 1024  then
+        r:debug("writing line ending in '" .. string.sub(line, -127, -1) .. "'")
+      end
       r:wswrite(line)
       if line == "quit" then
         r:wsclose()  -- goodbye!
diff --git a/t/modules/proxy_websockets.t b/t/modules/proxy_websockets.t
index ed7ea97..116a5a9 100644
--- a/t/modules/proxy_websockets.t
+++ b/t/modules/proxy_websockets.t
@@ -6,7 +6,8 @@
 use Apache::TestUtil;
 use Apache::TestConfig ();
 
-my $total_tests = 1;
+my @test_cases = ( "ping0", "ping1" x 10, "ping2" x 100, "ping3" x 1024, "ping4" x 4096,  "sendquit");
+my $total_tests = 2;
 
 plan tests => $total_tests, need 'AnyEvent::WebSocket::Client',
     need_module('proxy_http', 'lua'), need_min_apache_version('2.4.47');
@@ -21,7 +22,8 @@
 
 my $quit_program = AnyEvent->condvar;
 
-my $pingok = 0;
+my $responses = 0;
+my $surprised = 0;
 
 $client->connect("ws://$hostport/proxy/wsoc")->cb(sub {
   our $connection = eval { shift->recv };
@@ -33,21 +35,45 @@
     return;
   }
 
-  $connection->send('ping');
 
+  # AnyEvent::WebSocket::Connection does not pass the PONG message down to the callback
+  # my $actualpingmsg = AnyEvent::WebSocket::Message->new(opcode => 0x09, body => "xxx");
+  # $connection->send($actualpingmsg);
+
+  foreach (@test_cases){ 
+    $connection->send($_);
+  }
+
+  $connection->on(finish => sub {
+    t_debug("finish");
+  });
+  
   # recieve message from the websocket...
   $connection->on(each_message => sub {
     # $connection is the same connection object
     # $message isa AnyEvent::WebSocket::Message
     my($connection, $message) = @_;
-    t_debug("wsoc msg received: " . $message->body);
-    if ("ping" eq $message->body) { 
-      $pingok = 1;
+    $responses++;
+    t_debug("wsoc msg received: " . substr($message->body, 0, 5). " opcode " . $message->opcode);
+    if ("sendquit" eq $message->body) { 
+      $connection->send('quit');
+      t_debug("closing");
+      $connection->close; # doesn't seem to close TCP.
+      $quit_program->send();
     }
-    $connection->send('quit');
-    $quit_program->send();
+    elsif ($message->body =~ /^ping(\d)/) { 
+      my $offset = $1;
+      if ($message->body ne $test_cases[$offset]) { 
+          $surprised++;
+      }
+    }
+    else { 
+        $surprised++;
+    }
   });
+
 });
 
 $quit_program->recv;
-ok t_cmp($pingok, 1);
+ok t_cmp($surprised, 0);
+ok t_cmp($responses, scalar(@test_cases) );
diff --git a/t/modules/proxy_websockets_ssl.t b/t/modules/proxy_websockets_ssl.t
new file mode 100644
index 0000000..5f27e05
--- /dev/null
+++ b/t/modules/proxy_websockets_ssl.t
@@ -0,0 +1,85 @@
+use strict;
+use warnings FATAL => 'all';
+
+use Apache::Test;
+use Apache::TestRequest;
+use Apache::TestUtil;
+use Apache::TestConfig ();
+
+my @test_cases = ( "ping0", "ping1" x 10, "ping2" x 100, "ping3" x 1024, "ping4" x 4000,  "sendquit");
+my $total_tests = 2;
+
+plan tests => $total_tests, need 'AnyEvent::WebSocket::Client',
+    need_module('ssl', 'proxy_http', 'lua'), need_min_apache_version('2.4.47');
+
+require AnyEvent;
+require AnyEvent::WebSocket::Client;
+
+my $config = Apache::Test::config();
+#my $hostport = $config->{vhosts}->{proxy_https_https}->{hostport};
+my $hostport = $config->{vhosts}->{$config->{vars}->{ssl_module_name}}->{hostport};
+my $client = AnyEvent::WebSocket::Client->new(timeout => 5, ssl_ca_file => $config->{vars}->{sslca} . "/" . $config->{vars}->{sslcaorg}  . "/certs/ca.crt");
+
+my $quit_program = AnyEvent->condvar;
+
+my $responses = 0;
+my $surprised = 0;
+
+t_debug("wss://$hostport/modules/lua/websockets.lua");
+
+# $client->connect("wss://$hostport/proxy/wsoc")->cb(sub {
+$client->connect("wss://$hostport/modules/lua/websockets.lua")->cb(sub {
+  our $connection = eval { shift->recv };
+  t_debug("wsoc connected");
+  if($@) {
+    # handle error...
+    warn $@;
+    $quit_program->send();
+    return;
+  }
+
+
+  # AnyEvent::WebSocket::Connection does not pass the PONG message down to the callback
+  # my $actualpingmsg = AnyEvent::WebSocket::Message->new(opcode => 0x09, body => "xxx");
+  # $connection->send($actualpingmsg);
+
+  foreach (@test_cases){ 
+    $connection->send($_);
+  }
+
+  $connection->on(finish => sub {
+    t_debug("finish");
+    $quit_program->send();
+  });
+  
+  # recieve message from the websocket...
+  $connection->on(each_message => sub {
+    # $connection is the same connection object
+    # $message isa AnyEvent::WebSocket::Message
+    my($connection, $message) = @_;
+    $responses++;
+    t_debug("wsoc msg received: " . substr($message->body, 0, 5). " opcode " . $message->opcode);
+    if ("sendquit" eq $message->body) { 
+      $connection->send('quit');
+      t_debug("closing");
+      $connection->close; # doesn't seem to close TCP.
+      $quit_program->send();
+    }
+    elsif ($message->body =~ /^ping(\d)/) { 
+      my $offset = $1;
+      if ($message->body ne $test_cases[$offset]) { 
+          t_debug("wrong data");
+          $surprised++;
+      }
+    }
+    else { 
+        $surprised++;
+    }
+  });
+
+});
+
+$quit_program->recv;
+ok t_cmp($surprised, 0);
+# We don't expect the 20k over SSL to work, and we won't read the "sendquit" echoed back either.
+ok t_cmp($responses, scalar(@test_cases)-2 );