Make HHBBC validate that all symbols are unique
[hiphop-php.git] / hphp / test / server / fastcgi / tests / test_base.inc
blob5a5755ec8672e9b40e800b0f1c07b696fa6cffa6
1 <?hh
3 function init() {
4   require __DIR__ . '/../../util/server_tests.inc';
5   ServerUtilServerTests::$LOG_ROOT = '/tmp/hhvm_server';
6   require __DIR__ . '/../Adoy/Client.php';
8   ServerUtilServerTests::$DOC_ROOT = dirname(__DIR__).'/server_root';
9   ServerUtilServerTests::$request = fun('fastcgi_request');
12 function runTest($testController, $customArgs = '') {
13   $pid = posix_getpid();
14   try {
15     $serverProc = $serverPort = $adminPort = null;
16     $debugPort = false;
17     $serverProc = startServer(inout $serverPort, inout $adminPort, inout $debugPort,
18                               dirname(__DIR__), ServerUtilServerTests::$DOC_ROOT, $customArgs);
19     $testController($serverPort);
20     stopServer($adminPort, $serverProc);
21   } catch (Exception $e) {
22     error_log("Caught exception, test failed, pid=$pid, exn=".$e->getMessage());
23     killChildren($pid);
24     if ($serverProc) proc_close($serverProc);
25     error_log('test failed');
26   }
29 function fastcgi_request($host, $port, $path, $post = darray[], $headers = darray[], $extra_args = darray[]) {
30   try {
31     $client = new \Adoy\FastCGI\Client($host, $port);
32     $content = http_build_query($post);
34     $args = array_merge(darray[
35       'GATEWAY_INTERFACE' => 'FastCGI/1.0',
36       'REQUEST_METHOD' => (!($post ?? false)) ? 'GET' : 'POST',
37       'SERVER_SOFTWARE' => 'php/fcgiclient',
38       'SERVER_PROTOCOL' => 'HTTP/1.1',
39       'CONTENT_LENGTH' => strlen($content)
40     ], $extra_args);
41     foreach($headers as $k => $v) {
42       $args["HTTP_" . strtoupper($k)] = $v;
43     }
44     $pos = strpos($path, '?');
45     if ($pos !== false) {
46       $args['QUERY_STRING'] = substr($path, $pos + 1);
47       $path = substr($path, 0, $pos);
48     }
49     $doc_root = ServerUtilServerTests::$DOC_ROOT;
50     $pos = strpos($path, '//');
51     if ($pos) {
52       $doc_root .= '/' . substr($path, 0, $pos);
53       $path = substr($path, $pos + 2);
54     }
55     $args['SCRIPT_FILENAME'] = $doc_root . '/' . $path;
56     $args['SCRIPT_NAME'] = $path;
57     $args['DOCUMENT_ROOT'] = $doc_root;
59     @$result = $client->request($args, $content);
61     $pos = strpos($result, "\r\n\r\n");
62     if ($pos !== false) {
63       $result = substr($result, $pos + 4);
64     }
65     return trim($result);
66   } catch (Exception $e) {
67     tlog("request exception: " . $e->getMessage() .'('.$e->getTraceAsString().')');
68     return "";
69   }