def test_get_host(self): env = { 'HTTP_X_FORWARDED_HOST': 'example.org', 'SERVER_NAME': 'bullshit', 'HOST_NAME': 'ignore me dammit', } self.assertEqual(wsgi.get_host(env), 'example.org') self.assertEqual( wsgi.get_host(create_environ('/', 'http://example.org')), 'example.org')
def test_get_host(self): env = { 'HTTP_X_FORWARDED_HOST': 'example.org', 'SERVER_NAME': 'bullshit', 'HOST_NAME': 'ignore me dammit', } self.assertEqual(wsgi.get_host(env), 'example.org') self.assertEqual( wsgi.get_host(create_environ('/', 'http://example.org')), 'example.org' )
def test_get_host_fallback(self): self.assertEqual( wsgi.get_host({ 'SERVER_NAME': 'foobar.example.com', 'wsgi.url_scheme': 'http', 'SERVER_PORT': '80' }), 'foobar.example.com') self.assertEqual( wsgi.get_host({ 'SERVER_NAME': 'foobar.example.com', 'wsgi.url_scheme': 'http', 'SERVER_PORT': '81' }), 'foobar.example.com:81')
def test_get_host_fallback(self): self.assertEqual( wsgi.get_host({ 'SERVER_NAME': 'foobar.example.com', 'wsgi.url_scheme': 'http', 'SERVER_PORT': '80' }), 'foobar.example.com' ) self.assertEqual( wsgi.get_host({ 'SERVER_NAME': 'foobar.example.com', 'wsgi.url_scheme': 'http', 'SERVER_PORT': '81' }), 'foobar.example.com:81' )
def test_get_host_validation(self): env = { 'HTTP_X_FORWARDED_HOST': 'example.org', 'SERVER_NAME': 'bullshit', 'HOST_NAME': 'ignore me dammit', } self.assertEqual( wsgi.get_host(env, trusted_hosts=['.example.org']), 'example.org' ) self.assertRaises( BadRequest, wsgi.get_host, env, trusted_hosts=['example.com'] )
def test_get_host_validation(self): env = { 'HTTP_X_FORWARDED_HOST': 'example.org', 'SERVER_NAME': 'bullshit', 'HOST_NAME': 'ignore me dammit', } self.assertEqual( wsgi.get_host(env, trusted_hosts=['.example.org']), 'example.org' ) self.assertRaises( BadRequest, wsgi.get_host, env, trusted_hosts=['example.com'] )
def resolve_redirect(self, response, new_location, environ, buffered=False): """Resolves a single redirect and triggers the request again directly on this redirect client. """ scheme, netloc, script_root, qs, anchor = url_parse(new_location) base_url = url_unparse((scheme, netloc, '', '', '')).rstrip('/') + '/' cur_server_name = netloc.split(':', 1)[0].split('.') real_server_name = get_host(environ).rsplit(':', 1)[0].split('.') if self.allow_subdomain_redirects: allowed = cur_server_name.endswith(real_server_name) else: allowed = cur_server_name == real_server_name if not allowed: raise RuntimeError('%r does not support redirect to ' 'external targets' % self.__class__) status_code = int(response[1].split(None, 1)[0]) if status_code == 307: method = environ['REQUEST_METHOD'] else: method = 'GET' # For redirect handling we temporarily disable the response # wrapper. This is not threadsafe but not a real concern # since the test client must not be shared anyways. old_response_wrapper = self.response_wrapper self.response_wrapper = None try: return self.open(path=script_root, base_url=base_url, query_string=qs, as_tuple=True, buffered=buffered, method=method) finally: self.response_wrapper = old_response_wrapper
def resolve_redirect( self, response, new_location, environ, buffered=False ): """Resolves a single redirect and triggers the request again directly on this redirect client. """ scheme, netloc, script_root, qs, anchor = urlsplit(new_location) base_url = urlunsplit((scheme, netloc, '', '', '')).rstrip('/') + '/' cur_server_name = netloc.split(':', 1)[0].split('.') real_server_name = get_host(environ).rsplit(':', 1)[0].split('.') if self.allow_subdomain_redirects: allowed = cur_server_name.endswith(real_server_name) else: allowed = cur_server_name == real_server_name if not allowed: raise RuntimeError(( '{cls!r} does not support redirect to external targets' ).format(cls=self.__class__)) status_code = int(response[1].split(None, 1)[0]) if status_code == 307: method = environ['REQUEST_METHOD'] else: method = 'GET' # For redirect handling we temporarily disable the response # wrapper. This is not threadsafe but not a real concern # since the test client must not be shared anyways. old_response_wrapper = self.response_wrapper self.response_wrapper = None try: return self.open( path=script_root, base_url=base_url, query_string=qs, as_tuple=True, buffered=buffered, method=method, ) finally: self.response_wrapper = old_response_wrapper
def host(self): """Just the host including the port if available. See also: :attr:`trusted_hosts`. """ return get_host(self.environ, trusted_hosts=self.trusted_hosts)