Mercurial > p > roundup > code
comparison test/test_liveserver.py @ 7916:4cca0ae9f901
issue2551334 - get test suite running under windows
https://stackoverflow.com/questions/59506097/python-requests-library-is-very-slow-on-windows/75425238#75425238
reports that the requests libary uses urllib3. On windows this
tries (and retries) an IPv6 address if localhost is used in the
url. This takes 2s per request to test IPv6, give up and use
IPv4. At that rate, the rate limit is never reached and the
rest_login_RateLimit test fails.
This patch rewrites the base url to use 127.0.0.1 replacing
localhost. It forced urllib3 to open only an IPv4 address and the
speedup allows the test to pass.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 28 Apr 2024 19:24:19 -0400 |
| parents | 80cf6098ea65 |
| children | 6a13cf7bdca5 |
comparison
equal
deleted
inserted
replaced
| 7915:82093eb944d6 | 7916:4cca0ae9f901 |
|---|---|
| 1399 def test_rest_login_RateLimit(self): | 1399 def test_rest_login_RateLimit(self): |
| 1400 """login rate limit applies to api endpoints. Only failure | 1400 """login rate limit applies to api endpoints. Only failure |
| 1401 logins count though. So log in 10 times in a row | 1401 logins count though. So log in 10 times in a row |
| 1402 to verify that valid username/passwords aren't limited. | 1402 to verify that valid username/passwords aren't limited. |
| 1403 """ | 1403 """ |
| 1404 # On windows, using localhost in the URL with requests | |
| 1405 # tries an IPv6 address first. This causes a request to | |
| 1406 # take 2 seconds which is too slow to ever trip the rate | |
| 1407 # limit. So replace localhost with 127.0.0.1 that does an | |
| 1408 # IPv4 request only. | |
| 1409 url_base_numeric = self.url_base() | |
| 1410 url_base_numeric = url_base_numeric.replace('localhost','127.0.0.1') | |
| 1404 | 1411 |
| 1405 # verify that valid logins are not counted against the limit. | 1412 # verify that valid logins are not counted against the limit. |
| 1406 for i in range(10): | 1413 for i in range(10): |
| 1407 # use basic auth for rest endpoint | 1414 # use basic auth for rest endpoint |
| 1408 | 1415 |
| 1409 request_headers = {'content-type': "", | 1416 request_headers = {'content-type': "", |
| 1410 'Origin': "http://localhost:9001",} | 1417 'Origin': "http://localhost:9001",} |
| 1411 f = requests.options(self.url_base() + '/rest/data', | 1418 f = requests.options(url_base_numeric + '/rest/data', |
| 1412 auth=('admin', 'sekrit'), | 1419 auth=('admin', 'sekrit'), |
| 1413 headers=request_headers | 1420 headers=request_headers |
| 1414 ) | 1421 ) |
| 1415 #print(f.status_code) | 1422 #print(f.status_code) |
| 1416 #print(f.headers) | 1423 #print(f.headers) |
| 1439 } | 1446 } |
| 1440 | 1447 |
| 1441 for i in range(10): | 1448 for i in range(10): |
| 1442 # use basic auth for rest endpoint | 1449 # use basic auth for rest endpoint |
| 1443 | 1450 |
| 1444 f = requests.options(self.url_base() + '/rest/data', | 1451 f = requests.options(url_base_numeric + '/rest/data', |
| 1445 auth=('admin', 'ekrit'), | 1452 auth=('admin', 'ekrit'), |
| 1446 headers = {'content-type': "", | 1453 headers = {'content-type': "", |
| 1447 'Origin': "http://localhost:9001",} | 1454 'Origin': "http://localhost:9001",} |
| 1448 ) | 1455 ) |
| 1449 | 1456 |
| 1476 10.0, delta=3, | 1483 10.0, delta=3, |
| 1477 msg="limit reset not within 3 seconds of 10") | 1484 msg="limit reset not within 3 seconds of 10") |
| 1478 | 1485 |
| 1479 # test lockout this is a valid login but should be rejected | 1486 # test lockout this is a valid login but should be rejected |
| 1480 # with 429. | 1487 # with 429. |
| 1481 f = requests.options(self.url_base() + '/rest/data', | 1488 f = requests.options(url_base_numeric + '/rest/data', |
| 1482 auth=('admin', 'sekrit'), | 1489 auth=('admin', 'sekrit'), |
| 1483 headers = {'content-type': "", | 1490 headers = {'content-type': "", |
| 1484 'Origin': "http://localhost:9001",} | 1491 'Origin': "http://localhost:9001",} |
| 1485 ) | 1492 ) |
| 1486 self.assertEqual(f.status_code, 429) | 1493 self.assertEqual(f.status_code, 429) |
| 1491 | 1498 |
| 1492 | 1499 |
| 1493 sleep(4) | 1500 sleep(4) |
| 1494 # slept long enough to get a login slot. Should work with | 1501 # slept long enough to get a login slot. Should work with |
| 1495 # 200 return code. | 1502 # 200 return code. |
| 1496 f = requests.get(self.url_base() + '/rest/data', | 1503 f = requests.get(url_base_numeric + '/rest/data', |
| 1497 auth=('admin', 'sekrit'), | 1504 auth=('admin', 'sekrit'), |
| 1498 headers = {'content-type': "", | 1505 headers = {'content-type': "", |
| 1499 'Origin': "http://localhost:9001",} | 1506 'Origin': "http://localhost:9001",} |
| 1500 ) | 1507 ) |
| 1501 self.assertEqual(f.status_code, 200) | 1508 self.assertEqual(f.status_code, 200) |
