diff --git a/src/RREST.php b/src/RREST.php index c521fe3..d8e9b33 100644 --- a/src/RREST.php +++ b/src/RREST.php @@ -107,7 +107,7 @@ public function addRoute() //accept $acceptValidator = new AcceptValidator( - $this->getHeader('Accept'), + HTTP::getHeader('Accept'), $this->apiSpec->getResponsePayloadBodyContentTypes() ); if($acceptValidator->fails()) { @@ -116,7 +116,7 @@ public function addRoute() $accept = $acceptValidator->getBestAccept(); //content-type - $contentType = $this->getHeader('Content-Type'); + $contentType = HTTP::getHeader('Content-Type'); $contentTypeValidator = new ContentTypeValidator( $contentType, $this->apiSpec->getRequestPayloadBodyContentTypes() @@ -432,25 +432,4 @@ private function cast($value, $type) return $castValue; } - - /** - * @param string $name - * - * @return string - */ - private function getHeader($name) - { - $name = strtolower($name); - if (empty($this->headers)) { - $this->headers = array_change_key_case(getallheaders(), CASE_LOWER); - if (empty($this->headers)) { - $this->headers = array_change_key_case($_SERVER, CASE_LOWER); - } - } - if (isset($this->headers[$name])) { - return $this->headers[$name]; - } - - return; - } } diff --git a/src/Util/HTTP.php b/src/Util/HTTP.php index de229c9..2062138 100644 --- a/src/Util/HTTP.php +++ b/src/Util/HTTP.php @@ -26,4 +26,23 @@ public static function getProtocol() return $isSecure ? 'HTTPS' : 'HTTP'; } + + /** + * @param string $name + * + * @return string|null + */ + public static function getHeader($name) + { + $name = strtolower($name); + $headers = array_change_key_case(\getallheaders(), CASE_LOWER); + if (empty($headers)) { + $headers = array_change_key_case($_SERVER, CASE_LOWER); + } + if (isset($headers[$name])) { + return $headers[$name]; + } + + return null; + } } \ No newline at end of file diff --git a/tests/units/Util/HTTP.php b/tests/units/Util/HTTP.php new file mode 100644 index 0000000..71a3843 --- /dev/null +++ b/tests/units/Util/HTTP.php @@ -0,0 +1,61 @@ +string(UTILHTTP::getProtocol()) + ->isEqualTo('HTTP') + ; + + $_SERVER['HTTPS'] = 'on'; + $this + ->string(UTILHTTP::getProtocol()) + ->isEqualTo('HTTPS') + ; + + $_SERVER['HTTPS'] = 'x'; + $this + ->string(UTILHTTP::getProtocol()) + ->isEqualTo('HTTP') + ; + } + + public function testGetProtocolFromHTTPX() + { + $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https'; + $this + ->string(UTILHTTP::getProtocol()) + ->isEqualTo('HTTPS') + ; + + $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'http'; + $_SERVER['HTTP_X_FORWARDED_SSL'] = 'on'; + $this + ->string(UTILHTTP::getProtocol()) + ->isEqualTo('HTTPS') + ; + } + + public function testGetHeaderFromSERVER() + { + $_SERVER['content-type'] = 'x'; + $this + ->string(UTILHTTP::getHeader('Content-Type')) + ->isEqualTo('x') + ; + + $this + ->variable(UTILHTTP::getHeader('X-HEADER')) + ->isNull() + ; + } +} \ No newline at end of file