yep, moved there. ugh
authorMichael Wallner <mike@php.net>
Thu, 12 Feb 2015 10:14:04 +0000 (11:14 +0100)
committerMichael Wallner <mike@php.net>
Thu, 12 Feb 2015 10:14:04 +0000 (11:14 +0100)
tests/helper/cookie.inc [new file with mode: 0644]
tests/helper/pipeline.inc [new file with mode: 0644]
tests/helper/proxy.inc [new file with mode: 0644]
tests/helper/server.inc [new file with mode: 0644]

diff --git a/tests/helper/cookie.inc b/tests/helper/cookie.inc
new file mode 100644 (file)
index 0000000..d33666b
--- /dev/null
@@ -0,0 +1,11 @@
+<?php 
+
+include "server.inc";
+
+serve(function($client) {
+       $request = new http\Message($client, false);
+       $cookies = new http\Cookie($request->getHeader("cookie"));
+       $response = new http\Env\Response;
+       $response->setCookie($cookies->setCookie("counter", $cookies->getCookie("counter")+1));
+       $response->send($client);
+});
diff --git a/tests/helper/pipeline.inc b/tests/helper/pipeline.inc
new file mode 100644 (file)
index 0000000..815b463
--- /dev/null
@@ -0,0 +1,25 @@
+<?php 
+
+include "server.inc";
+
+function respond($client, $msg) {
+       (new http\Env\Response)->setEnvRequest($msg)
+               ->setHeader("X-Req", $msg->getRequestUrl())
+               ->send($client);
+}
+
+serve(function($client) {
+       $count = trim(fgets(STDIN));
+       
+       /* the peek message */
+       respond($client, new http\Message($client, false));
+       
+       /* pipelined messages */
+       $req = array();
+       for ($i=0; $i < $count; ++ $i) {
+               $req[] = new http\Message($client, false);
+       }
+       foreach ($req as $msg) {
+               respond($client, $msg);
+       }
+});
diff --git a/tests/helper/proxy.inc b/tests/helper/proxy.inc
new file mode 100644 (file)
index 0000000..61b20dd
--- /dev/null
@@ -0,0 +1,21 @@
+<?php 
+
+include "server.inc";
+
+serve(function($client) {
+       /* this might be a proxy connect or a standard request */
+       $request = new http\Message($client, false);
+       
+       if ($request->getHeader("Proxy-Connection")) {
+               $response = new http\Env\Response;
+               $response->send($client);
+               
+               /* soak up the request following the connect */
+               new http\Message($client, false);
+       }
+       
+       /* return the initial message as response body */
+       $response = new http\Env\Response;
+       $response->getBody()->append($request);
+       $response->send($client);
+});
diff --git a/tests/helper/server.inc b/tests/helper/server.inc
new file mode 100644 (file)
index 0000000..70fb7cf
--- /dev/null
@@ -0,0 +1,41 @@
+<?php 
+
+function serve(callable $cb) {
+       foreach (range(8000, 9000) as $port) {
+               if (($server = stream_socket_server("tcp://localhost:$port"))) {
+                       fprintf(STDERR, "%s\n", $port);
+                       do {
+                               $R = [$server]; $W = []; $E = [];
+                               $select = stream_select($R, $E, $E, 0, 10000);
+                               if ($select && ($client = stream_socket_accept($server, 1))) {
+                                       if (getenv("PHP_HTTP_TEST_SSL")) {
+                                               stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
+                                       }
+                                       while (!feof($client)) {
+                                               $cb($client);
+                                       }
+                               }
+                       } while ($select !== false);
+                       return;
+               }
+       }
+}
+
+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]));
+               
+               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);
+       }
+}