def setUp(self): super(TestOpencontrailDriver, self).setUp() self.nc_ports = mock.patch( 'ceilometer.neutron_client' '.Client.port_get_all', return_value=self.fake_ports()) self.nc_ports.start() self.nc_networks = mock.patch( 'ceilometer.neutron_client' '.Client.network_get_all', return_value=self.fake_networks()) self.nc_networks.start() self.driver = driver.OpencontrailDriver() self.parse_url = url_parse.ParseResult('opencontrail', '127.0.0.1:8143', '/', None, None, None) self.params = { 'password': ['admin'], 'scheme': ['http'], 'username': ['admin'], 'verify_ssl': ['false'] }
def before_record_request(self, request): # vcrpy isn't doing a very good job of giving us a copy here... # lets try harder... request_class = type(request) assert hasattr(request_class, '_from_dict') request = request_class._from_dict(copy.deepcopy(request._to_dict())) # chef api calls tend to have the username in the URI request.uri = request.uri.replace(self.okchef_client.user_id, TEST_USERNAME) # the following properties are derived from self.uri # port, host, scheme, path, query, url, protocol # i.e. self.uri can be modified, the others cant parsed_uri = parse.urlparse(request.uri) request.uri = parse.ParseResult( scheme=parsed_uri.scheme, netloc=TEST_NETLOC, # path=sha512_hex(parsed_uri.path), path=parsed_uri.path, params=sha512_hex(parsed_uri.params), query=sha512_hex(parsed_uri.query), fragment=sha512_hex(parsed_uri.fragment)).geturl() for hdr, val in request.headers.items(): if 'x-ops-userid' in hdr.lower(): request.headers[hdr] = TEST_USERNAME elif 'x-ops-authorization' in hdr.lower(): request.headers[hdr] = sha512_hex(val) elif 'x-ops-content-hash' in hdr.lower(): request.headers[hdr] = sha512_hex(val) assert request.host == 'test-chef-server.net' return request
def get_request_uri(request): host = get_host(request) scheme = request.scheme or "" return parse.urlunparse( parse.ParseResult(scheme=scheme, netloc=host, path=request.path, params="", query="", fragment=""))
def get_path_url(request): """Return correct link if X-Forwarded-Proto exists in headers.""" protocol = request.headers.get('X-Forwarded-Proto') parsed = parse.urlparse(request.path_url) if protocol and parsed.scheme != protocol: new_parsed = parse.ParseResult(protocol, parsed.netloc, parsed.path, parsed.params, parsed.query, parsed.fragment) return parse.urlunparse(new_parsed) else: return request.path_url
def _construct_url(self, action, params=None): """ Create a URL object we can use to pass to _do_request(). """ path = '/'.join([self.doc_root or '', action.lstrip('/')]) scheme = "https" if self.use_ssl else "http" netloc = "%s:%d" % (self.host, self.port) if isinstance(params, dict): for (key, value) in params.items(): if value is None: del params[key] query = urllib.urlencode(params) else: query = None return urlparse.ParseResult(scheme, netloc, path, '', query, '')
def setUp(self): super(TestOpencontrailDriver, self).setUp() self.nc_ports = mock.patch('ceilometer.neutron_client' '.Client.port_get_all', return_value=self.fake_ports()) self.nc_ports.start() self.CONF = self.useFixture(fixture_config.Config()).conf self.driver = driver.OpencontrailDriver(self.CONF) self.parse_url = urlparse.ParseResult('opencontrail', '127.0.0.1:8143', '/', None, None, None) self.params = {'password': ['admin'], 'scheme': ['http'], 'username': ['admin'], 'verify_ssl': ['false'], 'resource': ['if_stats_list']}
def change_query_params(self, **kwargs): # Seeks out the query params in a URL and changes/appends to them # from the kwargs given. So change_query_params(foo='bar') # would remove from the URL any old instance of foo=something and # then add &foo=bar to the URL. parsed_url = urlparse.urlparse(self.url) # Build a dictionary out of the query parameters in the URL query_params = dict(urlparse.parse_qsl(parsed_url.query)) # Use kwargs to change or update any values in the query dict. query_params.update(kwargs) # Build a new query based on the updated query dict. new_query_params = urllib.urlencode(query_params) return self.__class__( # Force HTTPS. urlparse.ParseResult('https', parsed_url.netloc, parsed_url.path, parsed_url.params, new_query_params, parsed_url.fragment).geturl())