Commits

Alexander Shorin  committed e8048ac

Rules are stricly matches pattern by default.

Motivation:

For rule `ping <host>` it's intuitive to expect matching of `ping example.com`,
but not `ping example.com foo bar baz`.

On other hand, sometimes you'd like to match messages by pattern no matter where
it occurs.

  • Participants
  • Parent commits 1ff4061

Comments (0)

Files changed (2)

File xmppflask/routing.py

     """Rule to handle single message"""
 
     def __init__(self, string=None, defaults=None, endpoint=None,
-                 event_type=None, from_jid=None, type=None, redirect_to=None):
+                 event_type=None, from_jid=None, type=None, redirect_to=None,
+                 strict=True):
         self.rule = string or ''
         self.map = None
         self.defaults = defaults
         self.type = type
         self.greediness = 0
         self.redirect_to = redirect_to
+        self.strict = strict
 
         self._trace = None
         self._weights = None
                     self.greediness += 1
 
         regex = u''.join(regex_parts)
+        if self.strict:
+            regex = '^' + regex + '$'
         self._regex = re.compile(regex, re.UNICODE)
 
     def match(self, message, event_type, from_jid, type):

File xmppflask/tests/test_routing.py

 
         adapter = rmap.bind()
         self.assertEqual(adapter.match('ping foo'), ('ping', {'host': 'foo'}))
-        self.assertEqual(adapter.match('ping foo bar'),
-                         ('ping', {'host': 'foo'}))
+        self.assertRaises(NotFound, adapter.match, 'ping foo bar')
 
     def test_string_converter(self):
         rmap = Map()
         self.assertEqual(adapter.match('ping foo'), ('ping', {'host': 'foo'}))
         self.assertEqual(adapter.match('ping foo bar'),
                          ('ping', {'host': 'foo bar'}))
+
+    def test_diff_of_strict_and_non_strict_rules(self):
+        rmap = Map()
+        rmap.add(Rule('ping <host>', endpoint='ping'))
+        rmap.add(Rule('pong <host>', endpoint='pong', strict=False))
+
+        adapter = rmap.bind()
+        self.assertEqual(adapter.match('ping example.com.'),
+                         ('ping', {'host': 'example.com.'}))
+        self.assertRaises(NotFound, adapter.match, 'ping foo bar')
+
+        self.assertEqual(adapter.match('pong example.com.'),
+                         ('pong', {'host': 'example.com.'}))
+        self.assertEqual(adapter.match('pong foo tail'),
+                         ('pong', {'host': 'foo'}))