jarus / flask-testing

Unittest extensions for Flask
http://pythonhosted.org/Flask-Testing/
Other
502 stars 111 forks source link

Error self.assertsRedirects #154

Open carlosmcastro opened 2 years ago

carlosmcastro commented 2 years ago

Testing locally. Transform location paths, although the response may be just the path. Giving this error. '/' != 'http://localhost/' even when response.location is equal to '/' and location is equal to '/'.

Here is the corrected code for my particular case. Try not to break anything, I hope they implement it, if at all.

def assertRedirects(self, response, location, message=None):
          """
          Checks if response is an HTTP redirect to the
          given location.

          :param response: Flask response
          :param location: relative URL path to SERVER_NAME or an absolute URL
          """
          parts_location = urlparse(location)

          valid_status_codes = (301, 302, 303, 305, 307)
          valid_status_code_str = ', '.join(str(code) for code in valid_status_codes)
          not_redirect = "HTTP Status %s expected but got %d" % (valid_status_code_str, response.status_code)
          self.assertTrue(response.status_code in valid_status_codes, message or not_redirect)

          if parts_location.netloc:
              expected_location = location
          else:
              server_name = self.app.config.get('SERVER_NAME') or 'localhost'
              expected_location = urljoin("http://%s" % server_name, location)
              #expected_location = location

          parts_response = urlparse(response.location)

          if parts_response.netloc:
              self.assertEqual(response.location, expected_location, message)
          else:
              server_name = self.app.config.get('SERVER_NAME') or 'localhost'
              response_url = urljoin("http://%s" % server_name, location)
              self.assertEqual(response_url, expected_location, message)