http2 test
authorMichael Wallner <mike@php.net>
Thu, 12 Feb 2015 18:21:46 +0000 (19:21 +0100)
committerMichael Wallner <mike@php.net>
Thu, 12 Feb 2015 18:27:26 +0000 (19:27 +0100)
tests/client022.phpt [new file with mode: 0644]
tests/helper/html/index.html [new file with mode: 0644]
tests/helper/http2.crt [new file with mode: 0644]
tests/helper/http2.key [new file with mode: 0644]
tests/helper/server.inc
tests/skipif.inc

diff --git a/tests/client022.phpt b/tests/client022.phpt
new file mode 100644 (file)
index 0000000..255de08
--- /dev/null
@@ -0,0 +1,45 @@
+--TEST--
+client http2
+--SKIPIF--
+<?php 
+include "skipif.inc";
+skip_client_test();
+skip_http2_test();
+?>
+--FILE--
+<?php 
+
+include "helper/server.inc";
+
+echo "Test\n";
+
+nghttpd(function($port) {
+       $client = new http\Client;
+       $client->setOptions([
+               "protocol" => http\Client\Curl\HTTP_VERSION_2_0,
+               "ssl" => [
+                       "cainfo" => __DIR__."/helper/http2.crt",
+               ]
+       ]);
+       $client->enqueue(new http\Client\Request("GET", "https://localhost:$port"));
+       echo $client->send()->getResponse();
+});
+
+?>
+===DONE===
+--EXPECTF--
+Test
+HTTP/2.0 200
+%a
+
+<!doctype html>
+<html>
+       <head>
+               <meta charset="utf-8">
+               <title>HTTP2</title>
+       </head>
+       <body>
+               Nothing to see here.
+       </body>
+</html>
+===DONE===
diff --git a/tests/helper/html/index.html b/tests/helper/html/index.html
new file mode 100644 (file)
index 0000000..11280ba
--- /dev/null
@@ -0,0 +1,10 @@
+<!doctype html>
+<html>
+       <head>
+               <meta charset="utf-8">
+               <title>HTTP2</title>
+       </head>
+       <body>
+               Nothing to see here.
+       </body>
+</html>
diff --git a/tests/helper/http2.crt b/tests/helper/http2.crt
new file mode 100644 (file)
index 0000000..c297162
--- /dev/null
@@ -0,0 +1,20 @@
+-----BEGIN CERTIFICATE-----
+MIIDNzCCAh+gAwIBAgIJAKOw1awbt7aIMA0GCSqGSIb3DQEBCwUAMDIxCzAJBgNV
+BAYT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Xmo=
+-----END CERTIFICATE-----
diff --git a/tests/helper/http2.key b/tests/helper/http2.key
new file mode 100644 (file)
index 0000000..bcf4f3f
--- /dev/null
@@ -0,0 +1,27 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEpQIBAAKCAQEAzFr4DrEQpA5iZ7nmdDK7Sqm+8YHhzjAYE1rpfpgATMQdzX01
+uLHTZRlZCAczV2YGFtUNbey07jYJCZxWIgp8DLlHUD1RmNRyf2dClrEv+yn0uusI
+A3KWMhJdLjRBwG+ELOZxS8NYFGFVMaNX5BNpiaZoz7yEkkkzpbsHIg/bZm+MegCf
+NPH2yOz44zJaaNhP2la/poT67mJILxufcHQaC18/Cg+oMXx4/EgwGma1uTsmpcXM
+fFNXLCTKmewXfhcr3hCOm9FwUZKD8NzVOikkbZZ+ryHHC2gp6tKUvm3IrLRmuW3E
+KIuR9VmLHA1kSqSDuM9JS8tLb32YGvDL6KSavwIDAQABAoIBAQCzUdAB9FYJ36Vy
+J6qVpD69EZ7ABZzDdWhq84eY0oDQ2/ba7lhJraE2QbviU481zgzh1CponyFVNo1P
+paPfUxvvflWZj3Ueiq2+JjpESU81MmfR7ZOmktJBNeQWOzzHRBPT4pLgTJXprE85
+s3/YX0BozWGDiIU8aIetkiR8OzXm97+BrJWiPFl733dGnHvfpHAZu/PwKZc2+8ft
+CnQw4GHRhTBWCVNj29HLwm+CA66zQqiAXItgijWMKzs+9ciPn+aCsCnZDNF+o1zs
+4pWt60CtIJQtD3r3rSRy7nBaCKcTrr/JU3FvwqKdunuUBUsuYeSaMBokW67kpVzS
+dO9L9p6BAoGBAP+pvcAd8qfC1WIrn9vka3aK25ulbYSCae3YaWmABc93INJPBMvO
+GrcUuaLKiQC1oou+E64vGyJ9MeEFwxh2zbvU75a9ezeKAajgaq92ciMX2QqREh0N
+IntNOc4w7eB6BK8NpaDXNvTtxNWMvlYyhVN0M7FVQRmYJfCJdnTZAkzvAoGBAMyf
+6rvWuc/wmIcAtBVe+jIeQ69EJJ/Idcjk0JUm6lFECAAraPpsCglha+wTHWWRQZ9u
+pPqBnb7QPjevU+3bZHnMvGoEzd5F4Rw74J+p5EZeMUJpuKmon7Ekzemcb0DV+qX9
+NorB781D2Z0MG9SCptKyKpN5TPHTjGz4BB3mLC8xAoGAdq99/ynn9ClmleRameI4
+YRelS2RIqzM/qcLFbMyZ5e4PtpIoT9SmYkekxgXwA/xOMUFUMZB8sE4eUbAzGbBN
+Yd1APGJKSUYv7w3/eOUrp07y2wzts77dOxBmvWnJhGQguINFWJ2QTbPzpI9p7OoX
+Kt7PAIvrZM5VDo1CCIyVnNECgYEAgLmpZXlzcwicK3GZ2EfjhVvcoIlxsMLetf6b
+6PiON4lgrxqf88m7lqMezWhI+fgjHDTyvFSF89/1A/rcBaoazzSo4tka2VWEg8p3
+SHoMDOh8fJcdgD2AGGRa1TeAFX2HLJzajvfp72tbnpxbdZircah7eEK60PaQRIzR
+qi1+ZkECgYEAi7GkO7Ey98DppLnt7567veQoEj0u8ixTlCyJ4V278nHR5+6eAZP5
+PfdZVC3MtKGLnxrrPTVUy5wU0hR6Gk9EVDmrAF8TgJdnZFlBK7X23eWZ0q4qO/eO
+3xIi+UrNwLag8BjYOr32nP/i+F+TLikgRIFR4oiZjk867+ofkTXmNFA=
+-----END RSA PRIVATE KEY-----
index 70fb7cf..26f0c50 100644 (file)
@@ -2,7 +2,7 @@
 
 function serve(callable $cb) {
        foreach (range(8000, 9000) as $port) {
-               if (($server = stream_socket_server("tcp://localhost:$port"))) {
+               if (($server = @stream_socket_server("tcp://localhost:$port"))) {
                        fprintf(STDERR, "%s\n", $port);
                        do {
                                $R = [$server]; $W = []; $E = [];
@@ -22,20 +22,65 @@ function serve(callable $cb) {
 }
 
 function server($handler, callable $cb) {
-       $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
-       $comm = sprintf("%s %s/%s", PHP_BINARY, __DIR__, $handler);
-       if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
-               $port = trim(fgets($pipes[2]));
+       proc(PHP_BINARY, [__DIR__."/$handler"], $cb);
+}
+
+function nghttpd(callable $cb) {
+       $spec = [["pipe","r"], ["pipe","w"], ["pipe","w"]];
+       foreach (range(8000, 9000) as $port) {
+               $comm = "exec nghttpd -d html $port http2.key http2.crt";
+               if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
+                       $stdin = $pipes[0];
+                       $stdout = $pipes[1];
+                       $stderr = $pipes[2];
+                       
+                       usleep(50000);
+                       $status = proc_get_status($proc);
+                       
+                       if (!$status["running"]) {
+                               continue;
+                       }
+                       
+                       try {
+                               $cb($port, $stdin, $stdout, $stderr);
+                       } catch (Exception $e) {
+                               echo $e,"\n";
+                       }
                
-               try {
-                       $cb($port, $stdin = $pipes[0], $stdout = $pipes[1], $stderr = $pipes[2]);
-               } catch (Exception $e) {
-                       echo $e,"\n";
+                       proc_terminate($proc);
+               
+                       fpassthru($stderr);
+                       fpassthru($stdout);
+                       return;
                }
+       }
+                       
+}
+
+function proc($bin, $args, callable $cb) {
+       $spec = [["pipe","r"], ["pipe","w"], ["pipe","w"]];
+       $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
+       if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
+               $stdin = $pipes[0];
+               $stdout = $pipes[1];
+               $stderr = $pipes[2];
                
+               do {
+                       $port = trim(fgets($stderr));
+                       $R = [$stderr]; $W = []; $E = [];
+               } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
+       
+               if (is_numeric($port)) {
+                       try {
+                               $cb($port, $stdin, $stdout, $stderr);
+                       } catch (Exception $e) {
+                               echo $e,"\n";
+                       }
+               }
+       
                proc_terminate($proc);
-               
+       
                fpassthru($stderr);
                fpassthru($stdout);
        }
-}
+}
\ No newline at end of file
index 4b2627a..cceeaf0 100644 (file)
@@ -19,3 +19,12 @@ function skip_client_test($message = "skip need a client driver\n") {
                die($message);
        }
 }
+
+function skip_http2_test($message = "skip need http2 support (nghttpd in PATH)\n") {
+       foreach (explode(":", $_ENV["PATH"]) as $path) {
+               if (is_executable($path . "/nghttpd")) {
+                       return;
+               }
+       }
+       die($message);
+}
\ No newline at end of file