def test_getconnection(self): """Test HTTPClient._getconnection.""" # http http = HTTPClient("127.0.0.1", 80) con = http._getconnection() assert isinstance(con, httplib.HTTPConnection) # https http = HTTPClient("127.0.0.1", 80, protocol="https") con = http._getconnection() assert isinstance(con, httplib.HTTPSConnection) http = HTTPClient("127.0.0.1", timeout=300, source_address="here.loc") # Python2.5 mockhttplib = Mock.Omnivore(HTTPConnection=[None]) context = dict( PYTHON2_6=False, PYTHON2_7=False, httplib=mockhttplib, ) with injected(http._getconnection, **context): http._getconnection() call_log = mockhttplib.called["HTTPConnection"][0][1] assert not call_log.get("strict") assert call_log.get("timeout") == None assert call_log.get("source_address") == None # Python2.6 mockhttplib = Mock.Omnivore(HTTPConnection=[None]) context = dict( PYTHON2_6=True, PYTHON2_7=False, httplib=mockhttplib, ) with injected(http._getconnection, **context): http._getconnection() call_log = mockhttplib.called["HTTPConnection"][0][1] assert not call_log.get("strict") assert call_log["timeout"] == 300 assert call_log.get("source_address") == None # Python2.7 mockhttplib = Mock.Omnivore(HTTPConnection=[None]) context = dict( PYTHON2_6=True, PYTHON2_7=True, httplib=mockhttplib, ) with injected(http._getconnection, **context): http._getconnection() call_log = mockhttplib.called["HTTPConnection"][0][1] assert not call_log.get("strict") assert call_log["timeout"] == 300 assert call_log.get("source_address") == "here.loc"
def test_prepare(self): """Test HTTPClient._prepare.""" headers = {"X-Test": "Hello"} query = {"foo": "bär"} http = HTTPClient("127.0.0.1", 80) http.setbasicauth(b"me", b"secret") (uri, headers) = http._prepare("/foo%20bar/baz", headers, query) assert uri == "/foo%20bar/baz?foo=b%C3%A4r" expect = { 'Authorization': 'Basic bWU6c2VjcmV0', 'X-Test': 'Hello', } assert headers == expect
def test_fake_http_request(self): """Test util.FakeHTTPReqest.""" client = HTTPClient("localhost") headers = dict(a="1", b="2") fake = util.FakeHTTPRequest(client, "/foo/bar", headers) assert fake.get_full_url() == "http://localhost:80/foo/bar" assert fake.get_host() == "localhost" assert not fake.is_unverifiable() assert fake.get_origin_req_host() == "localhost" assert fake.get_type() == "http" assert fake.has_header("a") assert not fake.has_header("foobar") fake.add_unredirected_header("foobar", "baz") assert fake.has_header("foobar")
def upload_webdav(filepath): client = HTTPClient(host=config['webdav']['host'], port=config.getint('webdav', 'port')) image_uri, thumb_uri = random_name(), random_name() image, thumb = minify(filepath) def save(img, img_uri): with BytesIO() as byte_file: img.save(byte_file, format=config['default']['format']) byte_file.seek(0) client.put(img_uri, byte_file.read()) save(image, image_uri) save(thumb, thumb_uri) return image_uri, thumb_uri
def setUp(self): """Setup the client.""" self.http = HTTPClient("127.0.0.1", 80) self.con = Mock.HTTPConnection() self.http._getconnection = lambda: self.con