Skip to content

Commit

Permalink
Add tests for http request
Browse files Browse the repository at this point in the history
(isDelete, isGet, isPatch)
  • Loading branch information
huzhifeng committed Mar 2, 2017
1 parent 2ed9f39 commit b70e218
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 0 deletions.
84 changes: 84 additions & 0 deletions tests/http_is_delete.phpt
@@ -0,0 +1,84 @@
--TEST--
Check isDelete
--SKIPIF--
<?php if (!extension_loaded("yaf")) print "skip"; ?>
--INI--

--FILE--
<?php
include 'php_cli_server.inc';


define('APPLICATION_PATH', __DIR__);

file_put_contents(APPLICATION_PATH . "/Bootstrap.php", <<<PHP
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract {};
PHP
);
$controllerDir = APPLICATION_PATH . '/controllers';
if (!is_dir($controllerDir)) {
mkdir($controllerDir, 0777);
}
file_put_contents(APPLICATION_PATH . "/controllers/Index.php", <<<PHP
<?php
class IndexController extends Yaf_Controller_Abstract {
public function indexAction() {
var_dump(\$this->getRequest()->isDelete());
return false;
}
}
PHP
);

file_put_contents(APPLICATION_PATH . '/index.php', <<<PHP
<?php
define('APPLICATION_PATH', __DIR__);
\$config = array(
"application" => array(
"directory" => APPLICATION_PATH,
),
);
\$app = new Yaf_Application(\$config);
\$app->bootstrap()->run();
PHP
);


$handle = php_cli_server_start(null, "index.php", "-c php.ini");

list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
$port = intval($port)? :80;
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
if (!$fp) {
die("connect failed");
}
$returnStr = '';
if(fwrite($fp, <<<HEADER
DELETE / HTTP/1.1
Host: {$host}
HEADER
)) {
while (!feof($fp)) {
$returnStr .= fgets($fp);
}
}

fclose($fp);
list($header, $body) = explode("\r\n\r\n", $returnStr);
echo $body;

--CLEAN--
<?php
unlink(__DIR__ . "index.php");
unlink(__DIR__ . "controllers/Index.php");
unlink(__DIR__ . 'Bootstrap.php');
rmdir(__DIR__ . 'controllers);
php_cli_server_stop($handle);

--EXPECTF--
bool(true)

84 changes: 84 additions & 0 deletions tests/http_is_get.phpt
@@ -0,0 +1,84 @@
--TEST--
Check isGet
--SKIPIF--
<?php if (!extension_loaded("yaf")) print "skip"; ?>
--INI--

--FILE--
<?php
include 'php_cli_server.inc';


define('APPLICATION_PATH', __DIR__);

file_put_contents(APPLICATION_PATH . "/Bootstrap.php", <<<PHP
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract {};
PHP
);
$controllerDir = APPLICATION_PATH . '/controllers';
if (!is_dir($controllerDir)) {
mkdir($controllerDir, 0777);
}
file_put_contents(APPLICATION_PATH . "/controllers/Index.php", <<<PHP
<?php
class IndexController extends Yaf_Controller_Abstract {
public function indexAction() {
var_dump(\$this->getRequest()->isGet());
return false;
}
}
PHP
);

file_put_contents(APPLICATION_PATH . '/index.php', <<<PHP
<?php
define('APPLICATION_PATH', __DIR__);
\$config = array(
"application" => array(
"directory" => APPLICATION_PATH,
),
);
\$app = new Yaf_Application(\$config);
\$app->bootstrap()->run();
PHP
);


$handle = php_cli_server_start(null, "index.php", "-c php.ini");

list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
$port = intval($port)? :80;
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
if (!$fp) {
die("connect failed");
}
$returnStr = '';
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
Host: {$host}
HEADER
)) {
while (!feof($fp)) {
$returnStr .= fgets($fp);
}
}

fclose($fp);
list($header, $body) = explode("\r\n\r\n", $returnStr);
echo $body;

--CLEAN--
<?php
unlink(__DIR__ . "index.php");
unlink(__DIR__ . "controllers/Index.php");
unlink(__DIR__ . 'Bootstrap.php');
rmdir(__DIR__ . 'controllers);
php_cli_server_stop($handle);

--EXPECTF--
bool(true)

84 changes: 84 additions & 0 deletions tests/http_is_patch.phpt
@@ -0,0 +1,84 @@
--TEST--
Check isPatch
--SKIPIF--
<?php if (!extension_loaded("yaf")) print "skip"; ?>
--INI--

--FILE--
<?php
include 'php_cli_server.inc';


define('APPLICATION_PATH', __DIR__);

file_put_contents(APPLICATION_PATH . "/Bootstrap.php", <<<PHP
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract {};
PHP
);
$controllerDir = APPLICATION_PATH . '/controllers';
if (!is_dir($controllerDir)) {
mkdir($controllerDir, 0777);
}
file_put_contents(APPLICATION_PATH . "/controllers/Index.php", <<<PHP
<?php
class IndexController extends Yaf_Controller_Abstract {
public function indexAction() {
var_dump(\$this->getRequest()->isPatch());
return false;
}
}
PHP
);

file_put_contents(APPLICATION_PATH . '/index.php', <<<PHP
<?php
define('APPLICATION_PATH', __DIR__);
\$config = array(
"application" => array(
"directory" => APPLICATION_PATH,
),
);
\$app = new Yaf_Application(\$config);
\$app->bootstrap()->run();
PHP
);


$handle = php_cli_server_start(null, "index.php", "-c php.ini");

list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
$port = intval($port)? :80;
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
if (!$fp) {
die("connect failed");
}
$returnStr = '';
if(fwrite($fp, <<<HEADER
PATCH / HTTP/1.1
Host: {$host}
HEADER
)) {
while (!feof($fp)) {
$returnStr .= fgets($fp);
}
}

fclose($fp);
list($header, $body) = explode("\r\n\r\n", $returnStr);
echo $body;

--CLEAN--
<?php
unlink(__DIR__ . "index.php");
unlink(__DIR__ . "controllers/Index.php");
unlink(__DIR__ . 'Bootstrap.php');
rmdir(__DIR__ . 'controllers);
php_cli_server_stop($handle);

--EXPECTF--
bool(true)

1 change: 1 addition & 0 deletions tests/php.ini
@@ -0,0 +1 @@
extension=yaf.so

0 comments on commit b70e218

Please sign in to comment.