Skip to content
This repository has been archived by the owner on Dec 20, 2021. It is now read-only.

Commit

Permalink
refactor: add Util\HTTP::getHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
RETFU committed Nov 23, 2017
1 parent 775c725 commit bcfb052
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 23 deletions.
25 changes: 2 additions & 23 deletions src/RREST.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function addRoute()

//accept
$acceptValidator = new AcceptValidator(
$this->getHeader('Accept'),
HTTP::getHeader('Accept'),
$this->apiSpec->getResponsePayloadBodyContentTypes()
);
if($acceptValidator->fails()) {
Expand All @@ -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()
Expand Down Expand Up @@ -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;
}
}
19 changes: 19 additions & 0 deletions src/Util/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
61 changes: 61 additions & 0 deletions tests/units/Util/HTTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace RREST\tests\units\Util;

require_once __DIR__ . '/../boostrap.php';

use atoum;
use RREST\Util\HTTP as UTILHTTP;

class HTTP extends atoum
{
public function testGetProtocolFromSERVER()
{
$this
->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()
;
}
}

0 comments on commit bcfb052

Please sign in to comment.