Skip to content

Commit

Permalink
Fixing tests for stream options
Browse files Browse the repository at this point in the history
Refactored class Process:
	1. Deleted unnecessary function run_cmd().
	2. Fixed the definition of the process id using the lsof call relative to the listening port.
	3. Added exception throwing and corrected syntax.
Added a test for the CURLOPT_WRITEHEADER option:
	1. It works by analogy with the test for CURLOPT_FILE.
	2. The response headers are written to the file, but kphp returns more information => only the beginning of the file is checked.
All tests now take 60 ms to start the server.
  • Loading branch information
mt-omarov committed Jan 12, 2024
1 parent 81e6c73 commit ad70313
Showing 1 changed file with 67 additions and 38 deletions.
105 changes: 67 additions & 38 deletions tests/phpt/curl/14_curl_setopt_streams.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,45 @@

class Process {
private $pid;
private $command;
private $outputs;

public function __construct($cl=false) {
if ($cl != false) {
$this->command = $cl;
$this->runcmd();
private $cmd;
private $port;

public function __construct($cmd, $port) {
if ($cmd) {
$this->cmd = 'nohup ' . $cmd . ' > /dev/null 2>&1 & echo $!';
$this->port = $port;
exec($this->cmd);
} else {
throw new Exception("The command for running a server on the port $port is empty!\n");
}
$this->outputs = array();
}

private function runcmd() {
$command = 'nohup ' . $this->command .' > /dev/null 2>&1 & echo $!';
exec($command, $this->outputs);
$this->pid = (int)$this->outputs[0];
}

public function getpid() {
exec("(ps aux | grep '[p]hp -S localhost' | awk '{print $2}')", $op);
$this->pid = $op[0];
return $this->pid;
public function update_pid() {
exec("lsof -i:$this->port | awk 'NR==2' | awk '{print $2}'", $op);
if (!isset($op[0])) {
throw new Exception("Couldn't define the pid of running server on the port $this->port");
}
return $this->pid = $op[0];
}

public function status() {
$command = 'ps -p ' . $this->pid;
exec($command, $op);
if (!isset($op[1]))
return false;
return true;
$cmd = 'ps -p ' . $this->pid;
exec($cmd, $op);
return (isset($op[1]));
}

public function start() {
if ($this->command != '')
$this->runcmd();
else
return true;
$code = false;
if ($this->cmd)
exec($this->cmd, $op, $code);
return $code;
}

public function stop() {
$this->getpid();
$command = 'kill '. $this->pid;
exec($command);
if ($this->status() == false)
return true;
return false;
$this->update_pid();
$cmd = 'kill '. $this->pid;
exec($cmd);
return $this->status() == false;
}
}

Expand All @@ -80,8 +74,8 @@ function test_file_option() {

var_dump(curl_setopt($c, CURLOPT_FILE, $fh_out));
var_dump(curl_setopt($c, CURLOPT_VERBOSE, 1)); // get all information about connections
$server = new Process($cmd);
usleep(1000 * 30); // sleep for 30 ms
$server = new Process($cmd, $port);
usleep(1000 * 60); // sleep for 60 ms
var_dump(curl_exec($c)); // true, if the connection to the localhost is successfull

var_dump(rewind($fh_out));
Expand Down Expand Up @@ -121,10 +115,44 @@ function test_infile_option() {
var_dump(curl_setopt($c, CURLOPT_INFILE, $fh_in));
var_dump(curl_setopt($c, CURLOPT_VERBOSE, 1));

$server = new Process($cmd);
usleep(1000 * 30);
$server = new Process($cmd, $port);
usleep(1000 * 60);
var_dump(curl_exec($c));

var_dump(fclose($fh_in));
var_dump(fclose($fh_out));
curl_close($c);
exec("rm ./$filename_in ./$filename_out");
var_dump($server->stop());
}

function test_writeheader_option() {
global $is_kphp;
$filename_in = "server_file.php";
$filename_out = "test_file.txt";

$fh_in = fopen("$filename_in", "w+");
$fh_out = fopen("$filename_out", "w+");
var_dump(fwrite($fh_in, "<?php\necho 'hello world\n';"));

// listen different ports for php and kphp
$port = 8080;
if ($is_kphp) {
$port = 8081;
rewind($fh_in);
}

$c = curl_init("http://localhost:$port");
$cmd = "php -S localhost:$port ./$filename_in";
var_dump(curl_setopt($c, CURLOPT_WRITEHEADER, $fh_out));
var_dump(curl_setopt($c, CURLOPT_VERBOSE, 1));

$server = new Process($cmd, $port);
usleep(1000 * 60);
var_dump(curl_exec($c));

var_dump(rewind($fh_out));
var_dump(fread($fh_out, 17));
var_dump(fclose($fh_in));
var_dump(fclose($fh_out));
curl_close($c);
Expand All @@ -133,4 +161,5 @@ function test_infile_option() {
}

test_file_option();
test_infile_option();
test_infile_option();
test_writeheader_option();

0 comments on commit ad70313

Please sign in to comment.