Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add more tests to Request class
  • Loading branch information
VicDeo committed Jul 5, 2016
commit 0c760b750c74ab5b34ca917098b5a9c99b44e189
32 changes: 18 additions & 14 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ public function getRequestUri() {
public function getServerProtocol() {
$forwardedProto = $this->server('HTTP_X_FORWARDED_PROTO');
if (!is_null($forwardedProto)) {
if (strpos($forwardedProto, ',') !== false) {
$parts = explode(',', $forwardedProto);
$proto = strtolower(trim($parts[0]));
} else {
$proto = strtolower($forwardedProto);
}

$proto = strtolower($this->getPartBeforeComma($forwardedProto));
// Verify that the protocol is always HTTP or HTTPS
// default to http if an invalid value is provided
return $proto === 'https' ? 'https' : 'http';
Expand All @@ -67,12 +61,7 @@ public function getHost(){
$host = 'localhost';
$forwardedHost = $this->server('HTTP_X_FORWARDED_HOST');
if (!is_null($forwardedHost)) {
if (strpos($forwardedHost, ',') !== false) {
$parts = explode(',', $forwardedHost);
$host = trim(current($parts));
} else {
$host = $forwardedHost;
}
$host = $this->getPartBeforeComma($forwardedHost);
} else {
$httpHost = $this->server('HTTP_HOST');
if (is_null($httpHost)) {
Expand Down Expand Up @@ -101,7 +90,7 @@ public function postParameter($name){
*/
public function header($name) {
$name = strtoupper($name);
return isset($this->vars['headers']['HTTP_'.$name]) ? $this->vars['headers']['HTTP_'.$name] : null;
return $this->server('HTTP_'.$name);
}

/**
Expand All @@ -112,5 +101,20 @@ public function server($name){
return isset($this->vars['headers'][$name]) ? $this->vars['headers'][$name] : null;
}

/**
* Return first part before comma or the string itself if there is no comma
* @param string $str
* @return string
*/
private function getPartBeforeComma($str){
if (strpos($str, ',') !== false) {
$parts = explode(',', $str);
$result = $parts[0];
} else {
$result = $str;
}
return trim($result);
}

}

59 changes: 59 additions & 0 deletions src/Tests/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,63 @@ public function testPostParameter($vars, $key, $expected){
$actual = $request->postParameter($key);
$this->assertEquals($expected, $actual);
}

public function serverProvider(){
return [
[ [], 'abcd', null ],
[ [ 'headers'=> [ 'command' => 'jump'] ], 'dummy', null ],
[ [ 'headers'=> [ 'command' => 'jump'] ], 'command', 'jump' ],
[ [ 'headers'=> [ 'testArray' => ['key' => 'value'] ] ], 'testArray', ['key' => 'value'] ],
];
}

/**
* @dataProvider serverProvider
*/
public function testServerVar($vars, $key, $expected){
$request = new Request($vars);
$actual = $request->server($key);
$this->assertEquals($expected, $actual);
}

public function headerProvider(){
return [
[ [], 'meow', null ],
[ [ 'headers'=> [ 'command' => 'jump'] ], 'dummy', null ],
[ [ 'headers'=> [ 'command' => 'jump'] ], 'command', null ],
[ [ 'headers'=> [ 'testArray' => ['key' => 'value'] ] ], 'testArray', null ],
[ [ 'headers'=> [ 'HTTP_TESTARRAY' => ['key' => 'value'] ] ], 'testArray', ['key' => 'value'] ],
];
}

/**
* @dataProvider headerProvider
*/
public function testHeaderVar($vars, $key, $expected){
$request = new Request($vars);
$actual = $request->header($key);
$this->assertEquals($expected, $actual);
}


public function hostProvider(){
return [
[ [ 'headers'=> [ 'SERVER_NAME' => 'jump' ] ], 'jump', null ],
[ [ 'headers'=> [ 'HTTP_HOST'=> 'duck', 'SERVER_NAME' => 'jump'] ], 'duck' ],
[ [ 'headers'=> [ 'HTTP_X_FORWARDED_HOST'=>'go', 'HTTP_HOST'=> 'duck', 'SERVER_NAME' => 'jump'] ], 'go' ],
[ [ 'headers'=> [ 'HTTP_X_FORWARDED_HOST'=>'go,', 'HTTP_HOST'=> 'duck', 'SERVER_NAME' => 'jump'] ], 'go' ],
[ [ 'headers'=> [ 'HTTP_X_FORWARDED_HOST'=>'run,forrest,run', 'HTTP_HOST'=> 'duck', 'SERVER_NAME' => 'jump'] ], 'run' ],
];
}

/**
* @dataProvider hostProvider
* @param $vars
* @param $expected
*/
public function testGetHost($vars, $expected){
$request = new Request($vars);
$actual = $request->getHost();
$this->assertEquals($expected, $actual);
}
}