def test_auth_str_bytes(): # https://github.com/httplib2/httplib2/pull/115 # Proxy-Authorization b64encode() TypeError: a bytes-like object is required, not 'str' with tests.server_const_http(request_count=2) as uri: uri_parsed = urllib.parse.urlparse(uri) http = httplib2.Http( proxy_info=httplib2.ProxyInfo( httplib2.socks.PROXY_TYPE_HTTP, proxy_host=uri_parsed.hostname, proxy_port=uri_parsed.port, proxy_rdns=True, proxy_user=u"user_str", proxy_pass=u"pass_str", ) ) response, _ = http.request(uri, "GET") assert response.status == 200 with tests.server_const_http(request_count=2) as uri: uri_parsed = urllib.parse.urlparse(uri) http = httplib2.Http( proxy_info=httplib2.ProxyInfo( httplib2.socks.PROXY_TYPE_HTTP, proxy_host=uri_parsed.hostname, proxy_port=uri_parsed.port, proxy_rdns=True, proxy_user=b"user_bytes", proxy_pass=b"pass_bytes", ) ) response, _ = http.request(uri, "GET") assert response.status == 200
def test_auth_str_bytes(): # https://github.com/httplib2/httplib2/pull/115 # Proxy-Authorization b64encode() TypeError: a bytes-like object is required, not 'str' with tests.server_const_http(request_count=2) as uri: uri_parsed = urllib.parse.urlparse(uri) http = httplib2.Http( proxy_info=httplib2.ProxyInfo( httplib2.socks.PROXY_TYPE_HTTP, proxy_host=uri_parsed.hostname, proxy_port=uri_parsed.port, proxy_rdns=True, proxy_user=u"user_str", proxy_pass=u"pass_str", ) ) response, _ = http.request(uri, "GET") assert response.status == 200 with tests.server_const_http(request_count=2) as uri: uri_parsed = urllib.parse.urlparse(uri) http = httplib2.Http( proxy_info=httplib2.ProxyInfo( httplib2.socks.PROXY_TYPE_HTTP, proxy_host=uri_parsed.hostname, proxy_port=uri_parsed.port, proxy_rdns=True, proxy_user=b"user_bytes", proxy_pass=b"pass_bytes", ) ) response, _ = http.request(uri, "GET") assert response.status == 200
def test_get_cache_control_no_cache(): # Test Cache-Control: no-cache on requests http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http(add_date=True, add_etag=True, headers={'cache-control': 'max-age=300'}, request_count=2) as uri: response, _ = http.request(uri, 'GET', headers={'accept-encoding': 'identity'}) assert response.status == 200 assert response['etag'] != '' assert not response.fromcache response, _ = http.request(uri, 'GET', headers={'accept-encoding': 'identity'}) assert response.status == 200 assert response.fromcache response, _ = http.request(uri, 'GET', headers={ 'accept-encoding': 'identity', 'Cache-Control': 'no-cache' }) assert response.status == 200 assert not response.fromcache
def test_get_cache_control_pragma_no_cache(): # Test Pragma: no-cache on requests http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http( add_date=True, add_etag=True, headers={"cache-control": "max-age=300"}, request_count=2, ) as uri: response, _ = http.request(uri, "GET", headers={"accept-encoding": "identity"}) assert response["etag"] != "" response, _ = http.request(uri, "GET", headers={"accept-encoding": "identity"}) assert response.status == 200 assert response.fromcache response, _ = http.request(uri, "GET", headers={ "accept-encoding": "identity", "Pragma": "no-cache" }) assert response.status == 200 assert not response.fromcache
def test_custom_redirect_codes(): http = httplib2.Http() http.redirect_codes = set([300]) with tests.server_const_http(status=301, request_count=1) as uri: response, content = http.request(uri, "GET") assert response.status == 301 assert response.previous is None
def test_get_no_cache(): # Test that can do a GET w/o the cache turned on. http = httplib2.Http() with tests.server_const_http() as uri: response, content = http.request(uri, "GET") assert response.status == 200 assert response.previous is None
def test_get_no_cache(): # Test that can do a GET w/o the cache turned on. http = httplib2.Http() with tests.server_const_http() as uri: response, content = http.request(uri, 'GET') assert response.status == 200 assert response.previous is None
def test_get_only_if_cached_cache_miss(): # Test that can do a GET with no cache with 'only-if-cached' http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http(request_count=0) as uri: response, content = http.request(uri, 'GET', headers={'cache-control': 'only-if-cached'}) assert not response.fromcache assert response.status == 504
def test_get_only_if_cached_cache_miss(): # Test that can do a GET with no cache with 'only-if-cached' http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http(request_count=0) as uri: response, content = http.request( uri, "GET", headers={"cache-control": "only-if-cached"}) assert not response.fromcache assert response.status == 504
def test_get_only_if_cached_cache_hit(): # Test that can do a GET with cache and 'only-if-cached' http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http(add_etag=True) as uri: http.request(uri, 'GET') response, content = http.request(uri, 'GET', headers={'cache-control': 'only-if-cached'}) assert response.fromcache assert response.status == 200
def test_close(): http = httplib2.Http() assert len(http.connections) == 0 with tests.server_const_http() as uri: http.request(uri) assert len(http.connections) == 1 http.close() assert len(http.connections) == 0
def test_get_only_if_cached_cache_hit(): # Test that can do a GET with cache and 'only-if-cached' http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http(add_etag=True) as uri: http.request(uri, "GET") response, content = http.request( uri, "GET", headers={"cache-control": "only-if-cached"}) assert response.fromcache assert response.status == 200
def test_get_300_without_location(): # Not giving a Location: header in a 300 response is acceptable # In which case we just return the 300 response http = httplib2.Http() with tests.server_const_http(status='300 Multiple Choices', body=b'redirect body') as uri: response, content = http.request(uri, 'GET') assert response.status == 300 assert response.previous is None assert content == b'redirect body'
def test_get_300_without_location(): # Not giving a Location: header in a 300 response is acceptable # In which case we just return the 300 response http = httplib2.Http() with tests.server_const_http(status="300 Multiple Choices", body=b"redirect body") as uri: response, content = http.request(uri, "GET") assert response.status == 300 assert response.previous is None assert content == b"redirect body"
def test_get_only_if_cached_no_cache_at_all(): # Test that can do a GET with no cache with 'only-if-cached' # Of course, there might be an intermediary beyond us # that responds to the 'only-if-cached', so this # test can't really be guaranteed to pass. http = httplib2.Http() with tests.server_const_http(request_count=0) as uri: response, content = http.request(uri, 'GET', headers={'cache-control': 'only-if-cached'}) assert not response.fromcache assert response.status == 504
def test_get_only_if_cached_no_cache_at_all(): # Test that can do a GET with no cache with 'only-if-cached' # Of course, there might be an intermediary beyond us # that responds to the 'only-if-cached', so this # test can't really be guaranteed to pass. http = httplib2.Http() with tests.server_const_http(request_count=0) as uri: response, content = http.request( uri, "GET", headers={"cache-control": "only-if-cached"}) assert not response.fromcache assert response.status == 504
def test_get_cache_control_no_store_response(): # A no-store response means that the response should not be stored. http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http( add_date=True, add_etag=True, headers={'cache-control': 'max-age=300, no-store'}, request_count=2) as uri: response, _ = http.request(uri, 'GET') assert response.status == 200 assert not response.fromcache response, _ = http.request(uri, 'GET') assert response.status == 200 assert not response.fromcache
def test_not_trusted_ca(): # Test that we get a SSLHandshakeError if we try to access # server using a CA cert file that doesn't contain server's CA. http = httplib2.Http(ca_certs=tests.CA_UNUSED_CERTS) with tests.server_const_http(tls=True) as uri: try: http.request(uri, "GET") assert False, "expected CERTIFICATE_VERIFY_FAILED" except ssl.SSLError as e: assert e.reason == "CERTIFICATE_VERIFY_FAILED" except httplib2.SSLHandshakeError: # Python2 pass
def test_sni_set_servername_callback(): sni_log = [] def setup_tls(context, server, skip_errors): context.set_servername_callback(lambda _sock, hostname, _context: sni_log.append(hostname)) return context.wrap_socket(server, server_side=True) http = httplib2.Http(ca_certs=tests.CA_CERTS) with tests.server_const_http(tls=setup_tls) as uri: uri_parsed = urllib.parse.urlparse(uri) http.request(uri) assert sni_log == [uri_parsed.hostname]
def test_get_cache_control_pragma_no_cache(): # Test Pragma: no-cache on requests http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http( add_date=True, add_etag=True, headers={'cache-control': 'max-age=300'}, request_count=2) as uri: response, _ = http.request(uri, 'GET', headers={'accept-encoding': 'identity'}) assert response['etag'] != '' response, _ = http.request(uri, 'GET', headers={'accept-encoding': 'identity'}) assert response.status == 200 assert response.fromcache response, _ = http.request(uri, 'GET', headers={'accept-encoding': 'identity', 'Pragma': 'no-cache'}) assert response.status == 200 assert not response.fromcache
def test_min_tls_version(): def setup_tls(context, server, skip_errors): skip_errors.append("WRONG_VERSION_NUMBER") context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_1) context.load_cert_chain(tests.SERVER_CHAIN) return context.wrap_socket(server, server_side=True) http = httplib2.Http(ca_certs=tests.CA_CERTS, tls_minimum_version="TLSv1_2") with tests.server_const_http(tls=setup_tls) as uri: try: http.request(uri) assert False, "expected SSLError" except ssl.SSLError as e: assert e.reason in ("UNSUPPORTED_PROTOCOL", "VERSION_TOO_LOW")
def test_get_cache_control_no_store_response(): # A no-store response means that the response should not be stored. http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http( add_date=True, add_etag=True, headers={'cache-control': 'max-age=300, no-store'}, request_count=2) as uri: response, _ = http.request(uri, 'GET') assert response.status == 200 assert not response.fromcache response, _ = http.request(uri, 'GET') assert response.status == 200 assert not response.fromcache
def test_get_cache_control_no_cache_no_store_request(): # Test that a no-store, no-cache clears the entry from the cache # even if it was cached previously. http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http( add_date=True, add_etag=True, headers={'cache-control': 'max-age=300'}, request_count=3) as uri: response, _ = http.request(uri, 'GET') response, _ = http.request(uri, 'GET') assert response.fromcache response, _ = http.request(uri, 'GET', headers={'Cache-Control': 'no-store, no-cache'}) assert response.status == 200 assert not response.fromcache response, _ = http.request(uri, 'GET', headers={'Cache-Control': 'no-store, no-cache'}) assert response.status == 200 assert not response.fromcache
def test_get_cache_control_no_store_request(): # A no-store request means that the response should not be stored. http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http( add_date=True, add_etag=True, headers={"cache-control": "max-age=300"}, request_count=2, ) as uri: response, _ = http.request(uri, "GET", headers={"Cache-Control": "no-store"}) assert response.status == 200 assert not response.fromcache response, _ = http.request(uri, "GET", headers={"Cache-Control": "no-store"}) assert response.status == 200 assert not response.fromcache
def test_get_cache_control_no_cache_no_store_request(): # Test that a no-store, no-cache clears the entry from the cache # even if it was cached previously. http = httplib2.Http(cache=tests.get_cache_path()) with tests.server_const_http(add_date=True, add_etag=True, headers={'cache-control': 'max-age=300'}, request_count=3) as uri: response, _ = http.request(uri, 'GET') response, _ = http.request(uri, 'GET') assert response.fromcache response, _ = http.request( uri, 'GET', headers={'Cache-Control': 'no-store, no-cache'}) assert response.status == 200 assert not response.fromcache response, _ = http.request( uri, 'GET', headers={'Cache-Control': 'no-store, no-cache'}) assert response.status == 200 assert not response.fromcache
def test_get_302_no_location(): # Test that we throw an exception when we get # a 302 with no Location: header. http = httplib2.Http() http.force_exception_to_status_code = False with tests.server_const_http(status='302 Found', request_count=2) as uri: try: http.request(uri, 'GET') assert False, 'Should never reach here' except httplib2.RedirectMissingLocation: pass except Exception: assert False, 'Threw wrong kind of exception ' # Re-run the test with out the exceptions http.force_exception_to_status_code = True response, content = http.request(uri, 'GET') assert response.status == 500 assert response.reason.startswith('Redirected but') assert '302' == response['status'] assert content == b''
def test_get_302_no_location(): # Test that we throw an exception when we get # a 302 with no Location: header. http = httplib2.Http() http.force_exception_to_status_code = False with tests.server_const_http(status='302 Found', request_count=2) as uri: try: http.request(uri, 'GET') assert False, 'Should never reach here' except httplib2.RedirectMissingLocation: pass except Exception: assert False, 'Threw wrong kind of exception ' # Re-run the test with out the exceptions http.force_exception_to_status_code = True response, content = http.request(uri, 'GET') assert response.status == 500 assert response.reason.startswith('Redirected but') assert '302' == response['status'] assert content == b''
def test_get_302_no_location(): # Test that we throw an exception when we get # a 302 with no Location: header. http = httplib2.Http() http.force_exception_to_status_code = False with tests.server_const_http(status="302 Found", request_count=2) as uri: try: http.request(uri, "GET") assert False, "Should never reach here" except httplib2.RedirectMissingLocation: pass except Exception: assert False, "Threw wrong kind of exception " # Re-run the test with out the exceptions http.force_exception_to_status_code = True response, content = http.request(uri, "GET") assert response.status == 500 assert response.reason.startswith("Redirected but") assert "302" == response["status"] assert content == b""
def test_get_302_no_location(): # Test that we throw an exception when we get # a 302 with no Location: header. http = httplib2.Http() http.force_exception_to_status_code = False with tests.server_const_http(status="302 Found", request_count=2) as uri: try: http.request(uri, "GET") assert False, "Should never reach here" except httplib2.RedirectMissingLocation: pass except Exception: assert False, "Threw wrong kind of exception " # Re-run the test with out the exceptions http.force_exception_to_status_code = True response, content = http.request(uri, "GET") assert response.status == 500 assert response.reason.startswith("Redirected but") assert "302" == response["status"] assert content == b""
def test_get_410(): # Test that we pass 410's through http = httplib2.Http() with tests.server_const_http(status=410) as uri: response, content = http.request(uri, "GET") assert response.status == 410
def test_get_410(): # Test that we pass 410's through http = httplib2.Http() with tests.server_const_http(status=410) as uri: response, content = http.request(uri, 'GET') assert response.status == 410
def test_max_tls_version(): http = httplib2.Http(ca_certs=tests.CA_CERTS, tls_maximum_version="TLSv1") with tests.server_const_http(tls=True) as uri: http.request(uri) _, tls_ver, _ = http.connections.popitem()[1].sock.cipher() assert tls_ver == "TLSv1.0"
def test_invalid_ca_certs_path(): http = httplib2.Http(ca_certs="/nosuchfile") with tests.server_const_http(request_count=0, tls=True) as uri: with tests.assert_raises(IOError): http.request(uri, "GET")
def test_get_via_https(): # Test that we can handle HTTPS http = httplib2.Http(ca_certs=tests.CA_CERTS) with tests.server_const_http(tls=True) as uri: response, _ = http.request(uri, "GET") assert response.status == 200