Best Python code snippet using tempest_python
test_http.py
Source:test_http.py
...20PROXY_URL = 'http://myproxy:3128'21REQUEST_URL = 'http://10.0.0.107:5000/v2.0'22REQUEST_METHOD = 'GET'23class TestClosingHttp(base.TestCase):24 def closing_http(self, **kwargs):25 return http.ClosingHttp(**kwargs)26 def test_closing_http(self):27 connection = self.closing_http()28 self.assertNotIn('cert_reqs', connection.connection_pool_kw)29 self.assertNotIn('ca_certs', connection.connection_pool_kw)30 self.assertNotIn('timeout', connection.connection_pool_kw)31 def test_closing_http_with_ca_certs(self):32 connection = self.closing_http(ca_certs=CERT_LOCATION)33 self.assertEqual(CERT_REQUIRED,34 connection.connection_pool_kw['cert_reqs'])35 self.assertEqual(CERT_LOCATION,36 connection.connection_pool_kw['ca_certs'])37 def test_closing_http_with_dscv(self):38 connection = self.closing_http(39 disable_ssl_certificate_validation=True)40 self.assertEqual(CERT_NONE,41 connection.connection_pool_kw['cert_reqs'])42 self.assertNotIn('ca_certs',43 connection.connection_pool_kw)44 def test_closing_http_with_ca_certs_and_dscv(self):45 connection = self.closing_http(46 disable_ssl_certificate_validation=True,47 ca_certs=CERT_LOCATION)48 self.assertEqual(CERT_NONE,49 connection.connection_pool_kw['cert_reqs'])50 self.assertNotIn('ca_certs',51 connection.connection_pool_kw)52 def test_closing_http_with_timeout(self):53 timeout = 3054 connection = self.closing_http(timeout=timeout)55 self.assertEqual(timeout,56 connection.connection_pool_kw['timeout'])57 def test_request(self):58 # Given59 connection = self.closing_http()60 http_response = urllib3.HTTPResponse()61 request = self.patch('urllib3.PoolManager.request',62 return_value=http_response)63 retry = self.patch('urllib3.util.Retry')64 # When65 response, data = connection.request(66 method=REQUEST_METHOD,67 url=REQUEST_URL)68 # Then69 request.assert_called_once_with(70 REQUEST_METHOD,71 REQUEST_URL,72 headers={'connection': 'close'},73 retries=retry(raise_on_redirect=False, redirect=5))74 self.assertEqual(75 {'content-location': REQUEST_URL,76 'status': str(http_response.status)},77 response)78 self.assertEqual(http_response.status, response.status)79 self.assertEqual(http_response.reason, response.reason)80 self.assertEqual(http_response.version, response.version)81 self.assertEqual(http_response.data, data)82 def test_request_with_fields(self):83 # Given84 connection = self.closing_http()85 http_response = urllib3.HTTPResponse()86 request = self.patch('urllib3.PoolManager.request',87 return_value=http_response)88 retry = self.patch('urllib3.util.Retry')89 fields = object()90 # When91 connection.request(92 method=REQUEST_METHOD,93 url=REQUEST_URL,94 fields=fields)95 # Then96 request.assert_called_once_with(97 REQUEST_METHOD,98 REQUEST_URL,99 fields=fields,100 headers=dict(connection='close'),101 retries=retry(raise_on_redirect=False, redirect=5))102 def test_request_with_headers(self):103 # Given104 connection = self.closing_http()105 headers = {'Xtra Key': 'Xtra Value'}106 http_response = urllib3.HTTPResponse(headers=headers)107 request = self.patch('urllib3.PoolManager.request',108 return_value=http_response)109 retry = self.patch('urllib3.util.Retry')110 # When111 response, _ = connection.request(112 method=REQUEST_METHOD,113 url=REQUEST_URL,114 headers=headers)115 # Then116 request.assert_called_once_with(117 REQUEST_METHOD,118 REQUEST_URL,119 headers=dict(headers, connection='close'),120 retries=retry(raise_on_redirect=False, redirect=5))121 self.assertEqual(122 {'content-location': REQUEST_URL,123 'status': str(http_response.status),124 'xtra key': 'Xtra Value'},125 response)126class TestClosingProxyHttp(TestClosingHttp):127 def closing_http(self, proxy_url=PROXY_URL, **kwargs):128 connection = http.ClosingProxyHttp(proxy_url=proxy_url, **kwargs)129 self.assertHasProxy(connection, proxy_url)130 return connection131 def test_class_without_proxy_url(self):132 self.assertRaises(ValueError, http.ClosingProxyHttp, None)133 def assertHasProxy(self, connection, proxy_url):134 self.assertIsInstance(connection, http.ClosingProxyHttp)135 proxy = connection.proxy136 self.assertEqual(proxy_url,137 '%s://%s:%i' % (proxy.scheme,138 proxy.host,139 proxy.port))140class TestClosingHttpRedirects(base.TestCase):141 def test_redirect_default(self):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!