Skip to content

Commit

Permalink
PrefixMatcher URI validation fix and tests. Fixes #72
Browse files Browse the repository at this point in the history
  • Loading branch information
mbonneau committed Feb 7, 2015
1 parent a8b2121 commit 10d9949
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Thruway/Subscription/PrefixMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function getMatchHash($uri, $options)
*/
public function matches($eventUri, $subscriptionUri, $subscriptionOptions)
{
$subscriptionUri = $this->fixupUri($subscriptionUri);
$matchingPart = substr($eventUri, 0, strlen($subscriptionUri));

return $matchingPart == $subscriptionUri;
Expand All @@ -50,10 +51,26 @@ public function matches($eventUri, $subscriptionUri, $subscriptionOptions)
*/
public function uriIsValid($uri, $options)
{
$uri = $this->fixupUri($uri);

// if the uri is empty - then match everything
if ($uri == "") return true;

// if there is a trailing . then remove it and run it through the
// regular validator
if (substr($uri, strlen($uri) - 1) == ".") $uri = substr($uri, 0, strlen($uri) - 1);

// allow matches to a normal URI or one with a trailing dot
return Utils::uriIsValid($uri) || Utils::uriIsValid($uri . ".");
}

private function fixupUri($uri) {
// a single "." matches everything
if ($uri == ".") return "";

return $uri;
}

/**
* @param $parentUri
* @param $parentOptions
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Role/BrokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ public function testDoNotExcludeMe()
$broker->onMessage($session, $publishMsg);
}

public function testPrefixMatcherValidUris() {

$prefixMatcher = new Thruway\Subscription\PrefixMatcher();
$options = (object)[];

$this->assertTrue($prefixMatcher->uriIsValid('', $options));
$this->assertTrue($prefixMatcher->uriIsValid('.', $options));
$this->assertTrue($prefixMatcher->uriIsValid('one', $options));
$this->assertTrue($prefixMatcher->uriIsValid('one.', $options));
$this->assertTrue($prefixMatcher->uriIsValid('one.two', $options));
$this->assertTrue($prefixMatcher->uriIsValid('one.two.', $options));

$this->assertFalse($prefixMatcher->uriIsValid('..', $options));
$this->assertFalse($prefixMatcher->uriIsValid('one..', $options));
$this->assertFalse($prefixMatcher->uriIsValid('!', $options));
$this->assertFalse($prefixMatcher->uriIsValid('one..two', $options));
$this->assertFalse($prefixMatcher->uriIsValid('one..two.', $options));

$this->assertTrue($prefixMatcher->matches('a', '.', $options));
$this->assertTrue($prefixMatcher->matches('a', '', $options));
$this->assertTrue($prefixMatcher->matches('a.b', '.', $options));
$this->assertTrue($prefixMatcher->matches('a.b', '', $options));
$this->assertTrue($prefixMatcher->matches('a', 'a', $options));
$this->assertTrue($prefixMatcher->matches('ab', 'a', $options));
$this->assertTrue($prefixMatcher->matches('a.b', 'a', $options));
$this->assertFalse($prefixMatcher->matches('a', 'a.', $options));
$this->assertTrue($prefixMatcher->matches('a.b', 'a.', $options));
$this->assertTrue($prefixMatcher->matches('a.b.c', 'a.', $options));
}

public function testPrefixMatcher()
{
$transport = $this->getMockBuilder('\Thruway\Transport\TransportInterface')
Expand Down

0 comments on commit 10d9949

Please sign in to comment.