Skip to content
This repository

update request param predicate to take a tuple #705

Merged
merged 4 commits into from over 1 year ago

2 participants

Michael Merickel Chris McDonough
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
47  pyramid/config/predicates.py
@@ -64,43 +64,48 @@ def __call__(self, context, request):
64 64
     
65 65
 class RequestParamPredicate(object):
66 66
     def __init__(self, val, config):
67  
-        name = val
68  
-        v = None
69  
-        if '=' in name:
70  
-            name, v = name.split('=', 1)
71  
-            name, v = name.strip(), v.strip()
72  
-        if v is None:
73  
-            self._text = 'request_param %s' % (name,)
74  
-        else:
75  
-            self._text = 'request_param %s = %s' % (name, v)
76  
-        self.name = name
77  
-        self.val = v
  67
+        val = as_sorted_tuple(val)
  68
+        reqs = []
  69
+        for p in val:
  70
+            k = p
  71
+            v = None
  72
+            if '=' in p:
  73
+                k, v = p.split('=', 1)
  74
+                k, v = k.strip(), v.strip()
  75
+            reqs.append((k, v))
  76
+        self.val = val
  77
+        self.reqs = reqs
78 78
 
79 79
     def text(self):
80  
-        return self._text
  80
+        return 'request_param %s' % ','.join(
  81
+            ['%s=%s' % (x,y) if y else x for x, y in self.reqs]
  82
+        )
81 83
 
82 84
     phash = text
83 85
 
84 86
     def __call__(self, context, request):
85  
-        if self.val is None:
86  
-            return self.name in request.params
87  
-        return request.params.get(self.name) == self.val
88  
-    
  87
+        for k, v in self.reqs:
  88
+            actual = request.params.get(k)
  89
+            if actual is None:
  90
+                return False
  91
+            if v is not None and actual != v:
  92
+                return False
  93
+        return True
89 94
 
90 95
 class HeaderPredicate(object):
91 96
     def __init__(self, val, config):
92 97
         name = val
93 98
         v = None
94 99
         if ':' in name:
95  
-            name, v = name.split(':', 1)
  100
+            name, val_str = name.split(':', 1)
96 101
             try:
97  
-                v = re.compile(v)
  102
+                v = re.compile(val_str)
98 103
             except re.error as why:
99 104
                 raise ConfigurationError(why.args[0])
100 105
         if v is None:
101 106
             self._text = 'header %s' % (name,)
102 107
         else:
103  
-            self._text = 'header %s = %s' % (name, v)
  108
+            self._text = 'header %s=%s' % (name, val_str)
104 109
         self.name = name
105 110
         self.val = v
106 111
 
@@ -156,9 +161,7 @@ def __call__(self, context, request):
156 161
     
157 162
 class MatchParamPredicate(object):
158 163
     def __init__(self, val, config):
159  
-        if not is_nonstr_iter(val):
160  
-            val = (val,)
161  
-        val = sorted(val)
  164
+        val = as_sorted_tuple(val)
162 165
         self.val = val
163 166
         reqs = [ p.split('=', 1) for p in val ]
164 167
         self.reqs = [ (x.strip(), y.strip()) for x, y in reqs ]
86  pyramid/tests/test_config/test_predicates.py
@@ -117,6 +117,20 @@ def test___call___true_withval(self):
117 117
         result = inst(None, request)
118 118
         self.assertTrue(result)
119 119
 
  120
+    def test___call___true_multi(self):
  121
+        inst = self._makeOne(('abc', 'def =2 '))
  122
+        request = Dummy()
  123
+        request.params = {'abc':'1', 'def': '2'}
  124
+        result = inst(None, request)
  125
+        self.assertTrue(result)
  126
+
  127
+    def test___call___false_multi(self):
  128
+        inst = self._makeOne(('abc=3', 'def =2 '))
  129
+        request = Dummy()
  130
+        request.params = {'abc':'3', 'def': '1'}
  131
+        result = inst(None, request)
  132
+        self.assertFalse(result)
  133
+
120 134
     def test___call___false(self):
121 135
         inst = self._makeOne('abc')
122 136
         request = Dummy()
@@ -130,7 +144,11 @@ def test_text_exists(self):
130 144
 
131 145
     def test_text_withval(self):
132 146
         inst = self._makeOne('abc=  1')
133  
-        self.assertEqual(inst.text(), 'request_param abc = 1')
  147
+        self.assertEqual(inst.text(), 'request_param abc=1')
  148
+
  149
+    def test_text_multi(self):
  150
+        inst = self._makeOne(('abc=  1', 'def'))
  151
+        self.assertEqual(inst.text(), 'request_param abc=1,def')
134 152
 
135 153
     def test_phash_exists(self):
136 154
         inst = self._makeOne('abc')
@@ -138,7 +156,7 @@ def test_phash_exists(self):
138 156
 
139 157
     def test_phash_withval(self):
140 158
         inst = self._makeOne('abc=   1')
141  
-        self.assertEqual(inst.phash(), "request_param abc = 1")
  159
+        self.assertEqual(inst.phash(), "request_param abc=1")
142 160
 
143 161
 class TestMatchParamPredicate(unittest.TestCase):
144 162
     def _makeOne(self, val):
@@ -299,6 +317,70 @@ def test_it_call_val_False(self):
299 317
         result = inst(None, request)
300 318
         self.assertEqual(result, True)
301 319
 
  320
+class TestHeaderPredicate(unittest.TestCase):
  321
+    def _makeOne(self, val):
  322
+        from pyramid.config.predicates import HeaderPredicate
  323
+        return HeaderPredicate(val, None)
  324
+
  325
+    def test___call___true_exists(self):
  326
+        inst = self._makeOne('abc')
  327
+        request = Dummy()
  328
+        request.headers = {'abc':1}
  329
+        result = inst(None, request)
  330
+        self.assertTrue(result)
  331
+
  332
+    def test___call___true_withval(self):
  333
+        inst = self._makeOne('abc:1')
  334
+        request = Dummy()
  335
+        request.headers = {'abc':'1'}
  336
+        result = inst(None, request)
  337
+        self.assertTrue(result)
  338
+
  339
+    def test___call___true_withregex(self):
  340
+        inst = self._makeOne(r'abc:\d+')
  341
+        request = Dummy()
  342
+        request.headers = {'abc':'1'}
  343
+        result = inst(None, request)
  344
+        self.assertTrue(result)
  345
+
  346
+    def test___call___false_withregex(self):
  347
+        inst = self._makeOne(r'abc:\d+')
  348
+        request = Dummy()
  349
+        request.headers = {'abc':'a'}
  350
+        result = inst(None, request)
  351
+        self.assertFalse(result)
  352
+
  353
+    def test___call___false(self):
  354
+        inst = self._makeOne('abc')
  355
+        request = Dummy()
  356
+        request.headers = {}
  357
+        result = inst(None, request)
  358
+        self.assertFalse(result)
  359
+
  360
+    def test_text_exists(self):
  361
+        inst = self._makeOne('abc')
  362
+        self.assertEqual(inst.text(), 'header abc')
  363
+
  364
+    def test_text_withval(self):
  365
+        inst = self._makeOne('abc:1')
  366
+        self.assertEqual(inst.text(), 'header abc=1')
  367
+
  368
+    def test_text_withregex(self):
  369
+        inst = self._makeOne(r'abc:\d+')
  370
+        self.assertEqual(inst.text(), r'header abc=\d+')
  371
+
  372
+    def test_phash_exists(self):
  373
+        inst = self._makeOne('abc')
  374
+        self.assertEqual(inst.phash(), 'header abc')
  375
+
  376
+    def test_phash_withval(self):
  377
+        inst = self._makeOne('abc:1')
  378
+        self.assertEqual(inst.phash(), "header abc=1")
  379
+
  380
+    def test_phash_withregex(self):
  381
+        inst = self._makeOne(r'abc:\d+')
  382
+        self.assertEqual(inst.phash(), r'header abc=\d+')
  383
+
302 384
 class predicate(object):
303 385
     def __repr__(self):
304 386
         return 'predicate'
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.