diff --git a/composer.json b/composer.json index 904c34d..0a52715 100644 --- a/composer.json +++ b/composer.json @@ -18,5 +18,8 @@ "Yangqi\\Htmldom": "src/" } }, - "minimum-stability": "dev" -} \ No newline at end of file + "minimum-stability": "dev", + "require-dev": { + "phpunit/phpunit": "^6.5" + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..8c13310 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,8 @@ + + + + + ./tests/ + + + \ No newline at end of file diff --git a/src/Yangqi/Htmldom/Htmldom.php b/src/Yangqi/Htmldom/Htmldom.php index 10a375e..70208e3 100644 --- a/src/Yangqi/Htmldom/Htmldom.php +++ b/src/Yangqi/Htmldom/Htmldom.php @@ -89,6 +89,7 @@ class Htmldom { public $_target_charset = ''; protected $default_br_text = ""; public $default_span_text = ""; + public $response_code = null; // use isset instead of in_array, performance boost about 30%... protected $self_closing_tags = array('img'=>1, 'br'=>1, 'input'=>1, 'meta'=>1, 'link'=>1, 'hr'=>1, 'base'=>1, 'embed'=>1, 'spacer'=>1); @@ -176,6 +177,7 @@ public function load_file() { $args = func_get_args(); $this->load(call_user_func_array('file_get_contents', $args), true); + $this->set_response_code($http_response_header[0]); // Throw an error if we can't properly load the dom. if (($error=error_get_last())!==null) { $this->clear(); @@ -418,6 +420,13 @@ protected function parse_charset() return $this->_charset = $charset; } + // search response code in $http_response_header variable. + protected function set_response_code($response_string) + { + $response_parts = explode(' ', $response_string); + $this->response_code = $response_parts[1]; + } + // read tag info protected function read_tag() { diff --git a/tests/ResponseCodeTest.php b/tests/ResponseCodeTest.php new file mode 100644 index 0000000..cd4948d --- /dev/null +++ b/tests/ResponseCodeTest.php @@ -0,0 +1,44 @@ +assertEquals(100, $htmldom->response_code); + } + + public function testOKCode() + { + $htmldom = new Htmldom('https://httpstat.us/200'); + + $this->assertEquals(200, $htmldom->response_code); + } + + public function testMultipleChoicesCode() + { + $htmldom = new Htmldom('https://httpstat.us/300'); + + $this->assertEquals(300, $htmldom->response_code); + } + + public function testBadRequestCode() + { + $htmldom = new Htmldom('https://httpstat.us/400'); + + $this->assertEquals(400, $htmldom->response_code); + } + + public function testInternalServerErrorCode() + { + $htmldom = new Htmldom('https://httpstat.us/500'); + + $this->assertEquals(500, $htmldom->response_code); + } + +} \ No newline at end of file