def test_base(self): client = HTTPClient() self.assertTrue(client) self.assertEqual(client.agent, "python-httpclient/{v}".format(v=__version__)) client = HTTPClient(agent='foo') self.assertEqual(client.agent, 'foo')
def getpage(url, params="", unicode_mode=False, headers=False): # Importación dentro de función, para no causar referencia circular. from httpclient import HTTPClient # Encodeo de parametro 'search'. url += urlencode(params) # Agregación de http:// en caso que sea necesario if not urlparse.urlparse(url).scheme: url = "http:" + url client = HTTPClient() # Creación de request. http = client.open(url) # Si se requiere en formato unicode if unicode_mode: # Si se requieren encabezados if headers: return (unicode(http.read(), "UTF-8"), http.headers) else: return unicode(http.read(), "UTF-8") else: if headers: return (http.read(), http.headers) else: return http.read()
def test_GET(self): client = HTTPClient(url="http://httpbin.org/get", requests=webtest.TestApp(Application())) response = client.GET() assert '"Host": "httpbin.org"' in response assert '"args": {}' in response
def test_decorator_exception(self): client = HTTPClient(with_exceptions=True) client.add_handler('request_send', _exception_cb) with self.assertRaises(Exception) as cm: client.get('http://lumberjaph.net') exception = cm.exception self.assertTrue(exception) self.assertTrue(exception.is_client_error) self.assertEqual(str(exception), "Not Found") # without `with_exception`, no exception should be raised client = HTTPClient() client.add_handler('request_send', _exception_cb) res = client.get('http://lumberjaph.net') self.assertEqual(res.status, 404)
def test_follow(self): client = HTTPClient(url="http://httpbin.org/anything") assert client._url == "http://httpbin.org/anything" client2 = client.follow("me") assert client2._url == "http://httpbin.org/anything/me"
def test_DELETE(self): client = HTTPClient(url="http://httpbin.org/anything/27", requests=webtest.TestApp(Application())) response = client.DELETE() assert '"method": "DELETE"' in response assert '"url": "http://httpbin.org/anything/27"' in response
def test_GET(self): client = HTTPClient(url="http://httpbin.org/get") with requests_mock.Mocker() as m: m.get(client._url, text='{"Host": "httpbin.org", "args": {}}') response = client.GET() assert '"Host": "httpbin.org"' in response assert '"args": {}' in response
def test_GET_params(self): client = HTTPClient(url="http://httpbin.org/get?alpha=1", requests=webtest.TestApp(Application())) response = client.GET() response = json.loads(response) assert response["headers"]["Host"] == "httpbin.org" assert response["args"] == {"alpha": "1"}
def test_POST(self): client = HTTPClient(url="http://httpbin.org/post?alpha=1", requests=webtest.TestApp(Application())) response = client.POST(beta=2) response = json.loads(response) assert response["headers"]["Host"] == "httpbin.org" assert response["args"] == {"alpha": "1"} assert response["form"] == {"beta": "2"}
def test_GET_params(self): client = HTTPClient(url="http://httpbin.org/get?alpha=1") with requests_mock.Mocker() as m: m.get(client._url, text='''{"headers": {"Host": "httpbin.org"}, "args": {"alpha": "1"}}''') response = client.GET() response = json.loads(response) assert response["headers"]["Host"] == "httpbin.org" assert response["args"] == {"alpha": "1"}
def test_DELETE(self): client = HTTPClient(url="http://httpbin.org/anything/27") with requests_mock.Mocker() as m: m.delete(client._url, json={ "method": "DELETE", "url": "http://httpbin.org/anything/27" }) response = client.DELETE() assert '"method": "DELETE"' in response assert '"url": "http://httpbin.org/anything/27"' in response
def test_POST(self): client = HTTPClient(url="http://httpbin.org/post?alpha=1") with requests_mock.Mocker() as m: m.post(client._url, json={"headers": {"Host": "httpbin.org"}, "args": {"alpha": "1"}, "form": {"beta": "2"}}) response = client.POST(beta=2) response = json.loads(response) assert response["headers"]["Host"] == "httpbin.org" assert response["args"] == {"alpha": "1"} assert response["form"] == {"beta": "2"}
def _test_request(self, method): client = HTTPClient() client.add_handler('request_send', _test_cb) request = Request(method, tests[method]['url'], tests[method]['headers']) if 'content' in tests[method]: request.content = tests[method]['content'] resp = client.request(request) self.assertTrue(resp) self.assertEqual(resp.status, 204)
def test_prepare(self): request = Request('FOO', 'http') client = HTTPClient() client.add_handler('request_prepare', _cb_request_prepare) resp = client.request(request) self.assertEqual(resp.status, 400) handlers = Handlers() handlers.add_handler('request_prepare', _cb_request_prepare_change_request) req = handlers.dispatch('request_prepare', request) self.assertEqual(req.method, 'PUT') handlers = Handlers() handlers.add_handler('request_prepare', _cb_request_prepare_do_nothing) req = handlers.dispatch('request_prepare', request) self.assertEqual(req.method, 'FOO') self.assertEqual(req.url, 'http')
def __init__(self, esmcfg, api_version='2'): """Initialize the ESM class. Args: esmcfg: Instance of ESMConfig object OR Dict with these keys: {'esmhost': '<ESM-IP>', 'esmuser': <ESM-USERNAME>, 'esmpass': <ESM-PASSWORD>} api_version (str): '1' or '2'. Defaults to '2'. Returns: Instance of ESM Raises: ValueError: esmcfg required. ValueError: esmhost, esmuser, esmpass required in esmcfg. ValueError: api_version must be '1' or '2'. """ if api_version is not '2': # or not '2': raise ValueError('api_version is "1" or "2".') try: esmuser = esmcfg['esmuser'] except KeyError: raise ValueError('Invalid esmcreds: Missing esmuser.') try: esmpass = esmcfg['esmpass'] except KeyError: raise ValueError('Invalid esmcfg: Missing esmpass.') try: esmhost = esmcfg['esmhost'] except KeyError: raise ValueError('Invalid esmcfg: Missing esmhost.') self.session = HTTPClient() self.base_url = self._set_base_url(esmhost, api_version) self.int_url = 'https://{}/ess'.format(esmhost) self.login_url = 'https://{}/rs/esm/login'.format(esmhost) self._setup_auth(esmuser, esmpass) self.logged_in = False
def setUp(self): self.url = "http://lumberjaph.net" custom_client = HTTPClient() custom_client.add_handler("request_send", _cb) client(custom_client)
def test_response(self): request = Request('GET', 'http') client = HTTPClient() client.add_handler('request_send', _cb_request_send) #client.add_handler('response_done', _cb_response_done) resp = client.request(request)
def test_headers(self): client = HTTPClient() self.assertTrue(client.default_headers.get('Connection')) self.assertEqual(client.default_headers.get('User-Agent'), 'python-httpclient/{v}'.format(v=__version__))
def __init__(self, user_agent=None, base_url='https://api.github.com'): self.base_url = base_url if user_agent is None: self._user_agent = HTTPClient() else: self._user_agent = user_agent
def testName(self): ua = HTTPClient() ua.add_handler('request_send', _cb) client = GitHubClient(user_agent=ua) res = client.get_user('franckcuny') self.assertEqual(res['name'], 'batman')
from http import Request from httpclient import HTTPClient _client = HTTPClient() def client(custom_client=None): global _client if custom_client is None: return _client _client = custom_client return client def get(url): result = client().get(url) return result def head(url): req = Request('HEAD', url) result = client().request(req) if result.is_success: return result return None def mirror(url, filename): client().mirror(url, filename)
def _get_client(self): client = HTTPClient() return client