Advertisement
Guest User

Correction self.assertRedirects

a guest
Apr 6th, 2022
174
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None
  1.  
  2.     def assertRedirects(self, response, location, message=None):
  3.         """
  4.        Checks if response is an HTTP redirect to the
  5.        given location.
  6.  
  7.        :param response: Flask response
  8.        :param location: relative URL path to SERVER_NAME or an absolute URL
  9.        """
  10.         parts_location = urlparse(location)
  11.  
  12.         valid_status_codes = (301, 302, 303, 305, 307)
  13.         valid_status_code_str = ', '.join(str(code) for code in valid_status_codes)
  14.         not_redirect = "HTTP Status %s expected but got %d" % (valid_status_code_str, response.status_code)
  15.         self.assertTrue(response.status_code in valid_status_codes, message or not_redirect)
  16.  
  17.         if parts_location.netloc:
  18.             expected_location = location
  19.         else:
  20.             server_name = self.app.config.get('SERVER_NAME') or 'localhost'
  21.             expected_location = urljoin("http://%s" % server_name, location)
  22.             #expected_location = location
  23.  
  24.         parts_response = urlparse(response.location)
  25.  
  26.         if parts_response.netloc:
  27.             self.assertEqual(response.location, expected_location, message)
  28.         else:
  29.             server_name = self.app.config.get('SERVER_NAME') or 'localhost'
  30.             response_url = urljoin("http://%s" % server_name, location)
  31.             self.assertEqual(response_url, expected_location, message)
Advertisement
RAW Paste Data Copied
Advertisement