def test_get_connection_url_not_fount(self): self.useFixture( mockpatch.PatchObject(self.client, 'connection_class', side_effect=httplib.InvalidURL())) self.assertRaises(exceptions.EndpointNotFound, self.client.get_connection)
def _get_hostport(self, host, port): # Python 2.7.7rc1 (hg r90728:568041fd8090), 3.4.1 and 3.5 rename # _set_hostport to _get_hostport and changes it's functionality. The # Python 2.7.7rc1 version of this method is included here for # compatibility with earlier versions of Python. Without this, HTTPS over # HTTP CONNECT proxies cannot be used. # This method may be removed if compatibility with Python <2.7.7rc1 is not # required. # Python bug: http://bugs.python.org/issue7776 if port is None: i = host.rfind(":") j = host.rfind("]") # ipv6 addresses have [...] if i > j: try: port = int(host[i+1:]) except ValueError: if host[i+1:] == "": # http://foo.com:/ == http://foo.com/ port = self.default_port else: raise httplib.InvalidURL("nonnumeric port: '%s'" % host[i+1:]) host = host[:i] else: port = self.default_port if host and host[0] == "[" and host[-1] == "]": host = host[1:-1] return (host, port)
def test_url_bad_url(self): self.expect_urlopen('http://astralandopal.com\\').AndRaise( httplib.InvalidURL('')) self.mox.ReplayAll() resp = app.application.get_response( '/url?url=http://astralandopal.com\\&input=html') self.assert_equals(400, resp.status_int)
def open_with_auth(url): """Open a urllib2 request, handling HTTP authentication""" scheme, netloc, path, params, query, frag = urlparse.urlparse(url) # Double scheme does not raise on Mac OS X as revealed by a # failing test. We would expect "nonnumeric port". Refs #20. if sys.platform == 'darwin': if netloc.endswith(':'): raise httplib.InvalidURL("nonnumeric port: ''") if scheme in ('http', 'https'): auth, host = urllib2.splituser(netloc) else: auth = None if auth: auth = "Basic " + _encode_auth(auth) new_url = urlparse.urlunparse((scheme,host,path,params,query,frag)) request = urllib2.Request(new_url) request.add_header("Authorization", auth) else: request = urllib2.Request(url) request.add_header('User-Agent', user_agent) fp = urllib2.urlopen(request) if auth: # Put authentication info back into request URL if same host, # so that links found on the page will work s2, h2, path2, param2, query2, frag2 = urlparse.urlparse(fp.url) if s2==scheme and h2==host: fp.url = urlparse.urlunparse((s2,netloc,path2,param2,query2,frag2)) return fp
def json_request(method, url, payload=None, basic_auth=None): """ Make an HTTP request under the presumption of JSON payload and response @param method HTTP method (e.g., GET, PUT, etc.) @param url URL to request @param payload Request payload @param basic_auth Basic authentication tuple (username, password) @return Response status code, header, body JSON tuple """ url_parts = urlparse(url) if url_parts.scheme == "http": connection = httplib.HTTPConnection(url_parts.netloc) elif url_parts.scheme == "https": connection = httplib.HTTPSConnection(url_parts.netloc) else: raise httplib.InvalidURL("I don't understand \"%s\"" % url) # We expect JSON back headers = {"Accept": "application/json"} # Serialise JSON payload, if there is one if payload and method in ["PUT", "POST"]: json_payload = json.dumps(payload) headers["Content-Type"] = "application/json" else: json_payload = None # Set authorisation header, if necessary if basic_auth: basic_hash = b64encode("%s:%s" % basic_auth) headers["Authorization"] = "Basic %s" % basic_hash connection.request(method, url_parts.path, json_payload, headers) res = connection.getresponse() # Get response headers res_headers = {} res_is_json = False for h, v in res.getheaders(): res_headers[h] = v if h.lower() == "content-type" and v == "application/json": res_is_json = True # Deserialise response body, if possible res_body = json.loads(res.read()) if res_is_json else res.read() return res.status, res_headers, res_body