__init__.py 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import urllib.parse as urlparse
  2. from unittest import TestCase
  3. # URL comparison where query param order is insignificant
  4. def url_equals(self, a, b, parse_fragment=False):
  5. parsed_a = urlparse.urlparse(a, allow_fragments=parse_fragment)
  6. parsed_b = urlparse.urlparse(b, allow_fragments=parse_fragment)
  7. query_a = urlparse.parse_qsl(parsed_a.query)
  8. query_b = urlparse.parse_qsl(parsed_b.query)
  9. if parse_fragment:
  10. fragment_a = urlparse.parse_qsl(parsed_a.fragment)
  11. fragment_b = urlparse.parse_qsl(parsed_b.fragment)
  12. self.assertCountEqual(fragment_a, fragment_b)
  13. else:
  14. self.assertEqual(parsed_a.fragment, parsed_b.fragment)
  15. self.assertEqual(parsed_a.scheme, parsed_b.scheme)
  16. self.assertEqual(parsed_a.netloc, parsed_b.netloc)
  17. self.assertEqual(parsed_a.path, parsed_b.path)
  18. self.assertEqual(parsed_a.params, parsed_b.params)
  19. self.assertEqual(parsed_a.username, parsed_b.username)
  20. self.assertEqual(parsed_a.password, parsed_b.password)
  21. self.assertEqual(parsed_a.hostname, parsed_b.hostname)
  22. self.assertEqual(parsed_a.port, parsed_b.port)
  23. self.assertCountEqual(query_a, query_b)
  24. TestCase.assertURLEqual = url_equals
  25. # Form body comparison where order is insignificant
  26. TestCase.assertFormBodyEqual = lambda self, a, b: self.assertCountEqual(
  27. urlparse.parse_qsl(a), urlparse.parse_qsl(b))