1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import re
from requests_mock import adapter
from . import base
from requests_mock.response import _MatcherResponse
ANY = adapter.ANY
class TestMatcher(base.TestCase):
def match(self,
target,
url,
matcher_method='GET',
request_method='GET',
complete_qs=False,
headers=None,
request_data=None,
request_headers={},
additional_matcher=None,
real_http=False,
case_sensitive=False):
matcher = adapter._Matcher(matcher_method,
target,
[],
complete_qs=complete_qs,
additional_matcher=additional_matcher,
request_headers=request_headers,
real_http=real_http,
case_sensitive=case_sensitive)
request = adapter._RequestObjectProxy._create(request_method,
url,
headers,
data=request_data)
return matcher._match(request)
def assertMatch(self,
target=ANY,
url='http://example.com/requests-mock',
matcher_method='GET',
request_method='GET',
**kwargs):
self.assertEqual(True,
self.match(target,
url,
matcher_method=matcher_method,
request_method=request_method,
**kwargs),
'Matcher %s %s failed to match %s %s' %
(matcher_method, target, request_method, url))
def assertMatchBoth(self,
target=ANY,
url='http://example.com/requests-mock',
matcher_method='GET',
request_method='GET',
**kwargs):
self.assertMatch(target,
url,
matcher_method=matcher_method,
request_method=request_method,
**kwargs)
self.assertMatch(url,
target,
matcher_method=request_method,
request_method=matcher_method,
**kwargs)
def assertNoMatch(self,
target=ANY,
url='http://example.com/requests-mock',
matcher_method='GET',
request_method='GET',
**kwargs):
self.assertEqual(False,
self.match(target,
url,
matcher_method=matcher_method,
request_method=request_method,
**kwargs),
'Matcher %s %s unexpectedly matched %s %s' %
(matcher_method, target, request_method, url))
def assertNoMatchBoth(self,
target=ANY,
url='http://example.com/requests-mock',
matcher_method='GET',
request_method='GET',
**kwargs):
self.assertNoMatch(target,
url,
matcher_method=matcher_method,
request_method=request_method,
**kwargs)
self.assertNoMatch(url,
target,
matcher_method=request_method,
request_method=matcher_method,
**kwargs)
def assertMatchMethodBoth(self, matcher_method, request_method, **kwargs):
url = 'http://www.test.com'
self.assertMatchBoth(url,
url,
request_method=request_method,
matcher_method=matcher_method,
**kwargs)
def assertNoMatchMethodBoth(self,
matcher_method,
request_method,
**kwargs):
url = 'http://www.test.com'
self.assertNoMatchBoth(url,
url,
request_method=request_method,
matcher_method=matcher_method,
**kwargs)
def test_url_matching(self):
self.assertMatchBoth('http://www.test.com',
'http://www.test.com')
self.assertMatchBoth('http://www.test.com',
'http://www.test.com/')
self.assertMatchBoth('http://www.test.com/abc',
'http://www.test.com/abc')
self.assertMatchBoth('http://www.test.com:5000/abc',
'http://www.test.com:5000/abc')
self.assertNoMatchBoth('https://www.test.com',
'http://www.test.com')
self.assertNoMatchBoth('http://www.test.com/abc',
'http://www.test.com')
self.assertNoMatchBoth('http://test.com',
'http://www.test.com')
self.assertNoMatchBoth('http://test.com',
'http://www.test.com')
self.assertNoMatchBoth('http://test.com/abc',
'http://www.test.com/abc/')
self.assertNoMatchBoth('http://test.com/abc/',
'http://www.test.com/abc')
self.assertNoMatchBoth('http://test.com:5000/abc/',
'http://www.test.com/abc')
self.assertNoMatchBoth('http://test.com/abc/',
'http://www.test.com:5000/abc')
def test_quotation(self):
self.assertMatchBoth('http://www.test.com/a string%url',
'http://www.test.com/a string%url')
self.assertMatchBoth('http://www.test.com/ABC 123',
'http://www.test.com/ABC%20123')
self.assertMatchBoth('http://www.test.com/user@example.com',
'http://www.test.com/user@example.com')
def test_subset_match(self):
self.assertMatch('/path', 'http://www.test.com/path')
self.assertMatch('/path', 'http://www.test.com/path')
self.assertMatch('//www.test.com/path', 'http://www.test.com/path')
self.assertMatch('//www.test.com/path', 'https://www.test.com/path')
def test_query_string(self):
self.assertMatch('/path?a=1&b=2',
'http://www.test.com/path?a=1&b=2')
self.assertMatch('/path?a=1',
'http://www.test.com/path?a=1&b=2',
complete_qs=False)
self.assertNoMatch('/path?a=1',
'http://www.test.com/path?a=1&b=2',
complete_qs=True)
self.assertNoMatch('/path?a=1&b=2',
'http://www.test.com/path?a=1')
def test_query_empty_string(self):
self.assertMatch('/path?a',
'http://www.test.com/path?a')
self.assertMatch('/path?bob&paul',
'http://www.test.com/path?paul&bob')
self.assertNoMatch('/path?bob',
'http://www.test.com/path?paul')
self.assertNoMatch('/path?pual&bob',
'http://www.test.com/path?bob')
def test_method_match(self):
self.assertNoMatchMethodBoth('GET', 'POST')
self.assertMatchMethodBoth('GET', 'get')
self.assertMatchMethodBoth('GeT', 'geT')
def test_match_ANY_url(self):
self.assertMatch(ANY, 'http://anything')
self.assertMatch(ANY, 'http://somethingelse')
self.assertNoMatch(ANY, 'http://somethingelse', request_method='POST')
def test_match_ANY_method(self):
for m in ('GET', 'POST', 'HEAD', 'OPTION'):
self.assertMatch('http://www.test.com',
'http://www.test.com',
matcher_method=ANY,
request_method=m)
self.assertNoMatch('http://www.test.com',
'http://another',
matcher_method=ANY)
def test_match_with_regex(self):
r1 = re.compile('test.com/a')
r2 = re.compile('/b/c')
self.assertMatch(r1, 'http://mock.test.com/a/b')
self.assertMatch(r1, 'http://test.com/a/')
self.assertMatch(r1, 'mock://test.com/a/b')
self.assertNoMatch(r1, 'mock://test.com/')
self.assertMatch(r2, 'http://anything/a/b/c/d')
self.assertMatch(r2, 'mock://anything/a/b/c/d')
def test_match_with_headers(self):
self.assertMatch('/path',
'http://www.test.com/path',
headers={'A': 'abc', 'b': 'def'},
request_headers={'a': 'abc'})
self.assertMatch('/path',
'http://www.test.com/path',
headers={'A': 'abc', 'b': 'def'})
self.assertNoMatch('/path',
'http://www.test.com/path',
headers={'A': 'abc', 'b': 'def'},
request_headers={'b': 'abc'})
self.assertNoMatch('/path',
'http://www.test.com/path',
headers={'A': 'abc', 'b': 'def'},
request_headers={'c': 'ghi'})
# headers should be key insensitive and value sensitive, we have no
# choice here because they go into an insensitive dict.
self.assertMatch('/path',
'http://www.test.com/path',
headers={'aBc': 'abc', 'DEF': 'def'},
request_headers={'abC': 'abc'})
self.assertNoMatch('/path',
'http://www.test.com/path',
headers={'abc': 'aBC', 'DEF': 'def'},
request_headers={'abc': 'Abc'})
def test_case_sensitive_ignored_for_netloc_and_protocol(self):
for case_sensitive in (True, False):
self.assertMatch('http://AbC.CoM',
'http://aBc.CoM',
case_sensitive=case_sensitive)
self.assertMatch('htTP://abc.com',
'hTTp://abc.com',
case_sensitive=case_sensitive)
self.assertMatch('htTP://aBC.cOm',
'hTTp://AbC.Com',
case_sensitive=case_sensitive)
def assertSensitiveMatch(self, target, url, **kwargs):
self.assertMatch(target, url, case_sensitive=False, **kwargs)
self.assertNoMatch(target, url, case_sensitive=True, **kwargs)
def test_case_sensitive_paths(self):
self.assertSensitiveMatch('http://abc.com/pAtH', 'http://abc.com/path')
self.assertSensitiveMatch('/pAtH', 'http://abc.com/path')
def test_case_sensitive_query(self):
self.assertSensitiveMatch('http://abc.com/path?abCD=efGH',
'http://abc.com/path?abCd=eFGH')
self.assertSensitiveMatch('http://abc.com/path?abcd=efGH',
'http://abc.com/path?abcd=eFGH')
def test_additional_matcher(self):
def test_match_body(request):
return 'hello' in request.text
self.assertMatch(request_method='POST',
matcher_method='POST',
request_data='hello world',
additional_matcher=test_match_body)
self.assertNoMatch(request_method='POST',
matcher_method='POST',
request_data='goodbye world',
additional_matcher=test_match_body)
def test_reset_reverts_count(self):
url = 'mock://test/site/'
matcher = adapter._Matcher('GET',
url,
[_MatcherResponse()],
complete_qs=False,
additional_matcher=None,
request_headers={},
real_http=False,
case_sensitive=False)
request = adapter._RequestObjectProxy._create('GET', url)
call_count = 3
for _ in range(call_count):
matcher(request)
self.assertEqual(matcher.call_count, call_count)
matcher.reset()
self.assertEqual(matcher.call_count, 0)
|