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_prepare(self): """Test HTTPClient._prepare.""" headers = {"X-Test": "Hello"} query = {"foo": "bär"} http = HTTPClient("127.0.0.1", 80) http.setbasicauth("me", "secret") (uri, headers) = http._prepare("/foo bar/baz", headers, query) self.assertEqual(uri, "/foo%20bar/baz?foo=b%C3%A4r") expect = { 'Authorization': 'Basic bWU6c2VjcmV0', 'X-Test': '=?utf-8?q?Hello?=', } self.assertEqual(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 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 setUp(self): """Setup the client.""" self.http = HTTPClient("127.0.0.1", 80) self.con = Mock.HTTPConnection() self.http._getconnection = lambda: self.con
class HTTPClientTestCase(unittest.TestCase): """Test the HTTPClient class.""" def setUp(self): """Setup the client.""" self.http = HTTPClient("127.0.0.1", 80) self.con = Mock.HTTPConnection() self.http._getconnection = lambda: self.con def test_init(self): """Test initializing the HTTPClient.""" self.assertEqual(self.http.host, "127.0.0.1") self.assertEqual(self.http.port, 80) 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_request(self): """Test HTTPClient._request.""" headers = {"X-Test": "Hello"} resp = self.http._request("POST", "/foo", "my content", headers) assert resp == 200 # relative path to absolute path resp = self.http._request("POST", "foo", "my content", headers) assert self.con.path.startswith("/") assert resp == 200 # cookies self.http.cookie = Mock.Omnivore() resp = self.http._request("POST", "/foo", "my content", headers) assert "add_cookie_header" in self.http.cookie.called # errors self.con.response.status = 400 with pytest.raises(HTTPUserError): self.http._request("POST", "/foo") self.con.response.status = 500 with pytest.raises(HTTPServerError): self.http._request("POST", "/foo") def test_setcookie(self): """Test HTTPClient.setcookie.""" self.http.setcookie(CookieJar()) assert isinstance(self.http.cookie, CookieJar) def test_setssl(self): """Test HTTPClient.setssl.""" # set nothing self.http.setssl(None, None) assert self.http.protocol == "http" assert self.http.key_file == None assert self.http.cert_file == None # set key file only self.http.setssl("Foo", None) assert self.http.protocol == "https" assert self.http.key_file == "Foo" assert self.http.cert_file == None self.http.protocol = "http" self.http.key_file = None # set cert file only self.http.setssl(None, "Foo") assert self.http.protocol == "https" assert self.http.key_file == None assert self.http.cert_file == "Foo" self.http.protocol = "http" self.http.key_file = None # set key file and cert file self.http.setssl("Foo", "Bar") assert self.http.protocol == "https" assert self.http.key_file == "Foo" assert self.http.cert_file == "Bar" 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_get(self): """Test HTTPClient.get.""" # prepare mock connection self.con.response.status = 200 query = {"path": "/foo/bar"} assert self.http.get("/index", None, query=query) == 200 assert self.con.method == "GET" assert self.con.path == "/index?path=%2Ffoo%2Fbar" assert self.con.closed def test_post(self): """Test HTTPClient.post.""" data = StringIO("Test data") # prepare mock connection self.con.response.status = 200 query = {"path": "/foo/bar"} assert self.http.post("/index", data, query=query) == 200 assert self.con.method == "POST" assert self.con.path == "/index?path=%2Ffoo%2Fbar" assert self.con.closed def test_post_py25(self): """Test HTTPClient.post with Python 2.5.""" data = StringIO("Test data") # prepare mock connection self.con.response.status = 200 with injected(self.http.post, PYTHON2_6=False): assert self.http.post("/index", data) == 200 assert self.con.method == "POST" assert self.con.path == "/index" assert self.con.closed def test_post_content_none(self): """Test HTTPClient.post with None as content.""" # prepare mock connection self.con.response.status = 200 query = {"path": "/foo/bar"} assert self.http.post("/index", None, query=query) == 200 assert self.con.method == "POST" assert self.con.path == "/index?path=%2Ffoo%2Fbar" assert self.con.closed def test_post_no_query(self): """Test HTTPClient.post without query string.""" data = StringIO("Test data") # prepare mock connection self.con.response.status = 200 assert self.http.post("/index", data) == 200 assert self.con.method == "POST" assert self.con.path == "/index" assert self.con.closed def test_post_form_data(self): """Test HTTPClient.post form-data.""" data = dict(a="foo", b="bar") def urlencode(data): urlencode.count += 1 return urllib.urlencode(data) urlencode.count = 0 # prepare mock connection mockurllib = Mock.Omnivore() mockurllib.quote = urllib.quote mockurllib.urlencode = urlencode context = dict( urllib_quote=mockurllib.quote, urllib_urlencode=mockurllib.urlencode, ) with injected(self.http.post, **context): resp = self.http.post("/index", data) assert urlencode.count == 1 assert resp == 200 def test_post_multipart(self): """Test HTTPClient.post multipart/form-data.""" data = dict(a="foo", b="bar") resp = self.http.post("/index", data, as_multipart=True) assert resp == 200 assert self.con.method == "POST" assert self.con.path == "/index" assert self.con.closed def test_options(self): """Test HTTPClient.options.""" self.con.response.status = 200 assert self.http.options("/index") == 200 assert self.con.method == "OPTIONS" assert self.con.path == "/index" assert self.con.closed def test_head(self): """Test HTTPClient.head.""" self.con.response.status = 200 assert self.http.head("/index") == 200 assert self.con.method == "HEAD" assert self.con.path == "/index" assert self.con.closed def test_delete(self): """Test HTTPClient.delete.""" self.con.response.status = 200 assert self.http.delete("/index") == 200 assert self.con.method == "DELETE" assert self.con.path == "/index" assert self.con.closed def test_trace(self): """Test HTTPClient.trace.""" self.con.response.status = 200 assert self.http.trace("/index") == 200 assert self.con.method == "TRACE" assert self.con.path == "/index" assert self.con.closed def test_trace_maxforwards_via(self): """Test HTTPClient.trace with given maxforwards and via.""" self.con.response.status = 200 assert self.http.trace("/index", 5, ["a", "b"]) == 200 assert self.con.method == "TRACE" assert self.con.path == "/index" assert self.con.headers.get("Max-Forwards") == "5" assert self.con.headers.get("Via") == "a, b" assert self.con.closed def test_connect(self): """Test HTTPClient.connect.""" self.con.response.status = 200 assert self.http.connect("/") == 200 assert self.con.method == "CONNECT" assert self.con.path == "/" assert self.con.closed
def test_getconnection(self): """Test HTTPClient._getconnection.""" # http http = HTTPClient("127.0.0.1", 80) con = http._getconnection() self.assertTrue(isinstance(con, httplib.HTTPConnection)) # https http = HTTPClient("127.0.0.1", 80, protocol="https") con = http._getconnection() self.assertTrue(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] self.assertFalse(call_log["strict"]) self.assertEqual(call_log.get("timeout"), None) self.assertEqual(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] self.assertFalse(call_log["strict"]) self.assertEqual(call_log["timeout"], 300) self.assertEqual(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] self.assertFalse(call_log["strict"]) self.assertEqual(call_log["timeout"], 300) self.assertEqual(call_log.get("source_address"), "here.loc")
class HTTPClientTestCase(unittest.TestCase): """Test the HTTPClient class.""" def setUp(self): """Setup the client.""" self.http = HTTPClient("127.0.0.1", 80) self.con = Mock.HTTPConnection() self.http._getconnection = lambda: self.con def test_init(self): """Test initializing the HTTPClient.""" self.assertEqual(self.http.host, "127.0.0.1") self.assertEqual(self.http.port, 80) def test_getconnection(self): """Test HTTPClient._getconnection.""" # http http = HTTPClient("127.0.0.1", 80) con = http._getconnection() self.assertTrue(isinstance(con, httplib.HTTPConnection)) # https http = HTTPClient("127.0.0.1", 80, protocol="https") con = http._getconnection() self.assertTrue(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] self.assertFalse(call_log["strict"]) self.assertEqual(call_log.get("timeout"), None) self.assertEqual(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] self.assertFalse(call_log["strict"]) self.assertEqual(call_log["timeout"], 300) self.assertEqual(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] self.assertFalse(call_log["strict"]) self.assertEqual(call_log["timeout"], 300) self.assertEqual(call_log.get("source_address"), "here.loc") def test_request(self): """Test HTTPClient._request.""" headers = {"X-Test": "Hello"} resp = self.http._request("POST", "/foo", "my content", headers) self.assertEqual(resp, 200) # relative path to absolute path resp = self.http._request("POST", "foo", "my content", headers) self.assertTrue(self.con.path.startswith("/")) self.assertEqual(resp, 200) # cookies self.http.cookie = Mock.Omnivore() resp = self.http._request("POST", "/foo", "my content", headers) self.assertTrue("add_cookie_header" in self.http.cookie.called) # errors self.con.response.status = 400 self.assertRaises(HTTPUserError, self.http._request, "POST", "/foo") self.con.response.status = 500 self.assertRaises(HTTPServerError, self.http._request, "POST", "/foo") def test_setcookie(self): """Test HTTPClient.setcookie.""" self.http.setcookie(CookieJar()) self.assertTrue(isinstance(self.http.cookie, CookieJar)) def test_setssl(self): """Test HTTPClient.setssl.""" # set nothing self.http.setssl(None, None) self.assertEqual(self.http.protocol, "http") self.assertEqual(self.http.key_file, None) self.assertEqual(self.http.cert_file, None) # set key file only self.http.setssl("Foo", None) self.assertEqual(self.http.protocol, "https") self.assertEqual(self.http.key_file, "Foo") self.assertEqual(self.http.cert_file, None) self.http.protocol = "http" self.http.key_file = None # set cert file only self.http.setssl(None, "Foo") self.assertEqual(self.http.protocol, "https") self.assertEqual(self.http.key_file, None) self.assertEqual(self.http.cert_file, "Foo") self.http.protocol = "http" self.http.key_file = None # set key file and cert file self.http.setssl("Foo", "Bar") self.assertEqual(self.http.protocol, "https") self.assertEqual(self.http.key_file, "Foo") self.assertEqual(self.http.cert_file, "Bar") def test_prepare(self): """Test HTTPClient._prepare.""" headers = {"X-Test": "Hello"} query = {"foo": "bär"} http = HTTPClient("127.0.0.1", 80) http.setbasicauth("me", "secret") (uri, headers) = http._prepare("/foo bar/baz", headers, query) self.assertEqual(uri, "/foo%20bar/baz?foo=b%C3%A4r") expect = { 'Authorization': 'Basic bWU6c2VjcmV0', 'X-Test': '=?utf-8?q?Hello?=', } self.assertEqual(headers, expect) def test_get(self): """Test HTTPClient.get.""" # prepare mock connection self.con.response.status = 200 query = {"path": "/foo/bar"} self.assertEqual(self.http.get("/index", None, query=query), 200) self.assertEqual(self.con.method, "GET") self.assertEqual(self.con.path, "/index?path=%2Ffoo%2Fbar") self.assertTrue(self.con.closed) def test_post(self): """Test HTTPClient.post.""" data = StringIO("Test data") # prepare mock connection self.con.response.status = 200 query = {"path": "/foo/bar"} self.assertEqual(self.http.post("/index", None, query=query), 200) self.assertEqual(self.con.method, "POST") self.assertEqual(self.con.path, "/index?path=%2Ffoo%2Fbar") self.assertTrue(self.con.closed) def test_post_py25(self): """Test HTTPClient.post with Python 2.5.""" data = StringIO("Test data") # prepare mock connection self.con.response.status = 200 query = {"path": "/foo/bar"} with injected(self.http.post, PYTHON2_6=False): self.assertEqual(self.http.post("/index", data), 200) self.assertEqual(self.con.method, "POST") self.assertEqual(self.con.path, "/index") self.assertTrue(self.con.closed) def test_post_content_none(self): """Test HTTPClient.post with None as content.""" # prepare mock connection self.con.response.status = 200 query = {"path": "/foo/bar"} self.assertEqual(self.http.post("/index", None, query=query), 200) self.assertEqual(self.con.method, "POST") self.assertEqual(self.con.path, "/index?path=%2Ffoo%2Fbar") self.assertTrue(self.con.closed) def test_post_no_query(self): """Test HTTPClient.post without query string.""" data = StringIO("Test data") # prepare mock connection self.con.response.status = 200 self.assertEqual(self.http.post("/index", data), 200) self.assertEqual(self.con.method, "POST") self.assertEqual(self.con.path, "/index") self.assertTrue(self.con.closed) def test_post_form_data(self): """Test HTTPClient.post form-data.""" data = dict(a="foo", b="bar") def urlencode(data): urlencode.count += 1 return urllib.urlencode(data) urlencode.count = 0 # prepare mock connection mockurllib = Mock.Omnivore() mockurllib.quote = urllib.quote mockurllib.urlencode = urlencode context = dict( urllib_quote=mockurllib.quote, urllib_urlencode=mockurllib.urlencode, ) with injected(self.http.post, **context): resp = self.http.post("/index", data) self.assertEqual(urlencode.count, 1) self.assertEqual(resp, 200) def test_post_multipart(self): """Test HTTPClient.post multipart/form-data.""" data = dict(a="foo", b="bar") resp = self.http.post("/index", data, as_multipart=True) self.assertEqual(resp, 200) self.assertEqual(self.con.method, "POST") self.assertEqual(self.con.path, "/index") self.assertTrue(self.con.closed) def test_options(self): """Test HTTPClient.options.""" self.con.response.status = 200 self.assertEqual(self.http.options("/index"), 200) self.assertEqual(self.con.method, "OPTIONS") self.assertEqual(self.con.path, "/index") self.assertTrue(self.con.closed) def test_head(self): """Test HTTPClient.head.""" self.con.response.status = 200 self.assertEqual(self.http.head("/index"), 200) self.assertEqual(self.con.method, "HEAD") self.assertEqual(self.con.path, "/index") self.assertTrue(self.con.closed) def test_delete(self): """Test HTTPClient.delete.""" self.con.response.status = 200 self.assertEqual(self.http.delete("/index"), 200) self.assertEqual(self.con.method, "DELETE") self.assertEqual(self.con.path, "/index") self.assertTrue(self.con.closed) def test_trace(self): """Test HTTPClient.trace.""" self.con.response.status = 200 self.assertEqual(self.http.trace("/index"), 200) self.assertEqual(self.con.method, "TRACE") self.assertEqual(self.con.path, "/index") self.assertTrue(self.con.closed) def test_trace_maxforwards_via(self): """Test HTTPClient.trace with given maxforwards and via.""" self.con.response.status = 200 self.assertEqual(self.http.trace("/index", 5, ["a", "b"]), 200) self.assertEqual(self.con.method, "TRACE") self.assertEqual(self.con.path, "/index") self.assertEqual(self.con.headers.get("Max-Forwards"), "5") self.assertEqual(self.con.headers.get("Via"), "a, b") self.assertTrue(self.con.closed) def test_connect(self): """Test HTTPClient.connect.""" self.con.response.status = 200 self.assertEqual(self.http.connect("/"), 200) self.assertEqual(self.con.method, "CONNECT") self.assertEqual(self.con.path, "/") self.assertTrue(self.con.closed)