Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error self.assertsRedirects #154

Open
carlosmcastro opened this issue Apr 6, 2022 · 0 comments
Open

Error self.assertsRedirects #154

carlosmcastro opened this issue Apr 6, 2022 · 0 comments

Comments

@carlosmcastro
Copy link

carlosmcastro commented Apr 6, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant