From 10d99496727da8694cd458241238dc10b8904b84 Mon Sep 17 00:00:00 2001 From: matt Date: Sat, 7 Feb 2015 17:42:53 -0500 Subject: [PATCH] PrefixMatcher URI validation fix and tests. Fixes #72 --- src/Thruway/Subscription/PrefixMatcher.php | 17 ++++++++++++ tests/Unit/Role/BrokerTest.php | 30 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/Thruway/Subscription/PrefixMatcher.php b/src/Thruway/Subscription/PrefixMatcher.php index 752a7109..d60a208a 100644 --- a/src/Thruway/Subscription/PrefixMatcher.php +++ b/src/Thruway/Subscription/PrefixMatcher.php @@ -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; @@ -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 diff --git a/tests/Unit/Role/BrokerTest.php b/tests/Unit/Role/BrokerTest.php index 94d33499..20f91c46 100644 --- a/tests/Unit/Role/BrokerTest.php +++ b/tests/Unit/Role/BrokerTest.php @@ -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')