Skip to content

Commit

Permalink
Update domain validation ignoring ports.
Browse files Browse the repository at this point in the history
fixes dingo#754
  • Loading branch information
mbacodes committed Dec 8, 2015
1 parent 02dd0e9 commit d55f08c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Http/Validation/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class Domain implements Validator
{
const PATTERN_STRIP_PROTOCOL = '/:\d*$/';

/**
* API domain.
*
Expand Down Expand Up @@ -35,7 +37,7 @@ public function __construct($domain)
*/
public function validate(Request $request)
{
return ! is_null($this->domain) && $request->header('host') == $this->stripProtocol($this->domain);
return ! is_null($this->domain) && $request->getHost() == $this->getStrippedDomain();
}

/**
Expand All @@ -53,4 +55,32 @@ protected function stripProtocol($domain)

return $domain;
}

/**
* Strip the port from a domain.
*
* @param $domain
*
* @return mixed
*/
protected function stripPort($domain)
{
$domainStripped = preg_replace(self::PATTERN_STRIP_PROTOCOL, '', $domain);

if ($domainStripped === null) {
return $domain;
}

return $domainStripped;
}

/**
* Get the domain stripped from protocol and port.
*
* @return mixed
*/
protected function getStrippedDomain()
{
return $this->stripPort($this->stripProtocol($this->domain));
}
}
18 changes: 18 additions & 0 deletions tests/Http/Validation/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,22 @@ public function testValidationPassesWithDifferentProtocols()
$validator = new Domain('https://foo.bar');
$this->assertTrue($validator->validate(Request::create('http://foo.bar', 'GET')), 'Validation failed when it should have passed with a valid domain.');
}

public function testValidationPassesWithPortOnDomain()
{
$validator = new Domain('http://foo.bar:8888');
$this->assertTrue($validator->validate(Request::create('http://foo.bar', 'GET')), 'Validation failed when it should have passed with a valid domain.');
}

public function testValidationPassesWithPortOnRequest()
{
$validator = new Domain('http://foo.bar');
$this->assertTrue($validator->validate(Request::create('http://foo.bar:8888', 'GET')), 'Validation failed when it should have passed with a valid domain.');
}

public function testValidationPassesWithPortOnDomainAndRequest()
{
$validator = new Domain('http://foo.bar:8888');
$this->assertTrue($validator->validate(Request::create('http://foo.bar:8888', 'GET')), 'Validation failed when it should have passed with a valid domain.');
}
}

0 comments on commit d55f08c

Please sign in to comment.