def test_303(): # Do a follow-up GET on a Location: header # returned from a POST that gave a 303. http = httplib2.Http() routes = { '/final': tests.make_http_reflect(), '': tests.make_http_reflect(status='303 See Other', headers={'location': '/final'}), } with tests.server_route(routes, request_count=2) as uri: response, content = http.request(uri, 'POST', " ") assert response.status == 200 reflected = tests.HttpRequest.from_bytes(content) assert reflected.uri == '/final' assert response.previous.status == 303 # Skip follow-up GET http = httplib2.Http() http.follow_redirects = False with tests.server_route(routes, request_count=1) as uri: response, content = http.request(uri, 'POST', " ") assert response.status == 303 # All methods can be used http = httplib2.Http() cases = 'DELETE GET HEAD POST PUT EVEN_NEW_ONES'.split(' ') with tests.server_route(routes, request_count=len(cases) * 2) as uri: for method in cases: response, content = http.request(uri, method, body=b'q q') assert response.status == 200 reflected = tests.HttpRequest.from_bytes(content) assert reflected.method == 'GET'
def test_303(): # Do a follow-up GET on a Location: header # returned from a POST that gave a 303. http = httplib2.Http() routes = { "/final": tests.make_http_reflect(), "": tests.make_http_reflect( status="303 See Other", headers={"location": "/final"} ), } with tests.server_route(routes, request_count=2) as uri: response, content = http.request(uri, "POST", " ") assert response.status == 200 reflected = tests.HttpRequest.from_bytes(content) assert reflected.uri == "/final" assert response.previous.status == 303 # Skip follow-up GET http = httplib2.Http() http.follow_redirects = False with tests.server_route(routes, request_count=1) as uri: response, content = http.request(uri, "POST", " ") assert response.status == 303 # All methods can be used http = httplib2.Http() cases = "DELETE GET HEAD POST PUT EVEN_NEW_ONES".split(" ") with tests.server_route(routes, request_count=len(cases) * 2) as uri: for method in cases: response, content = http.request(uri, method, body=b"q q") assert response.status == 200 reflected = tests.HttpRequest.from_bytes(content) assert reflected.method == "GET"
def test_get_300_with_location(): # Test the we automatically follow 300 redirects if a Location: header is provided http = httplib2.Http() final_content = b"This is the final destination.\n" routes = { "/final": tests.http_response_bytes(body=final_content), "": tests.http_response_bytes(status="300 Multiple Choices", headers={"location": "/final"}), } with tests.server_route(routes, request_count=2) as uri: response, content = http.request(uri, "GET") assert response.status == 200 assert content == final_content assert response.previous.status == 300 assert not response.previous.fromcache # Confirm that the intermediate 300 is not cached with tests.server_route(routes, request_count=2) as uri: response, content = http.request(uri, "GET") assert response.status == 200 assert content == final_content assert response.previous.status == 300 assert not response.previous.fromcache
def test_303(): # Do a follow-up GET on a Location: header # returned from a POST that gave a 303. http = httplib2.Http() routes = { "/final": tests.make_http_reflect(), "": tests.make_http_reflect(status="303 See Other", headers={"location": "/final"}), } with tests.server_route(routes, request_count=2) as uri: response, content = http.request(uri, "POST", " ") assert response.status == 200 reflected = tests.HttpRequest.from_bytes(content) assert reflected.uri == "/final" assert response.previous.status == 303 # Skip follow-up GET http = httplib2.Http() http.follow_redirects = False with tests.server_route(routes, request_count=1) as uri: response, content = http.request(uri, "POST", " ") assert response.status == 303 # All methods can be used http = httplib2.Http() cases = "DELETE GET HEAD POST PUT EVEN_NEW_ONES".split(" ") with tests.server_route(routes, request_count=len(cases) * 2) as uri: for method in cases: response, content = http.request(uri, method, body=b"q q") assert response.status == 200 reflected = tests.HttpRequest.from_bytes(content) assert reflected.method == "GET"
def test_get_301(): # Test that we automatically follow 301 redirects # and that we cache the 301 response http = httplib2.Http(cache=tests.get_cache_path()) destination = '' routes = { '/final': tests.http_response_bytes(body=b'This is the final destination.\n'), '': tests.http_response_bytes( status='301 Now where did I leave that URL', headers={'location': '/final'}, body=b'redirect body'), } with tests.server_route(routes, request_count=3) as uri: destination = urllib.parse.urljoin(uri, '/final') response1, content1 = http.request(uri, 'GET') response2, content2 = http.request(uri, 'GET') assert response1.status == 200 assert 'content-location' in response2 assert response1['content-location'] == destination assert content1 == b'This is the final destination.\n' assert response1.previous.status == 301 assert not response1.previous.fromcache assert response2.status == 200 assert response2['content-location'] == destination assert content2 == b'This is the final destination.\n' assert response2.previous.status == 301 assert response2.previous.fromcache
def test_get_302_redirection_limit(): # Test that we can set a lower redirection limit # and that we raise an exception when we exceed # that limit. http = httplib2.Http() http.force_exception_to_status_code = False routes = { "/second": tests.http_response_bytes( status="302 Found", headers={"location": "/final"}, body=b"second redirect" ), "": tests.http_response_bytes( status="302 Found", headers={"location": "/second"}, body=b"redirect body" ), } with tests.server_route(routes, request_count=4) as uri: try: http.request(uri, "GET", redirections=1) assert False, "This should not happen" except httplib2.RedirectLimit: 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", redirections=1) assert response.status == 500 assert response.reason.startswith("Redirected more") assert response["status"] == "302" assert content == b"second redirect" assert response.previous is not None
def test_get_301(): # Test that we automatically follow 301 redirects # and that we cache the 301 response http = httplib2.Http(cache=tests.get_cache_path()) destination = '' routes = { '/final': tests.http_response_bytes(body=b'This is the final destination.\n'), '': tests.http_response_bytes(status='301 Now where did I leave that URL', headers={'location': '/final'}, body=b'redirect body'), } with tests.server_route(routes, request_count=3) as uri: destination = urllib.parse.urljoin(uri, '/final') response1, content1 = http.request(uri, 'GET') response2, content2 = http.request(uri, 'GET') assert response1.status == 200 assert 'content-location' in response2 assert response1['content-location'] == destination assert content1 == b'This is the final destination.\n' assert response1.previous.status == 301 assert not response1.previous.fromcache assert response2.status == 200 assert response2['content-location'] == destination assert content2 == b'This is the final destination.\n' assert response2.previous.status == 301 assert response2.previous.fromcache
def test_get_302_redirection_limit(): # Test that we can set a lower redirection limit # and that we raise an exception when we exceed # that limit. http = httplib2.Http() http.force_exception_to_status_code = False routes = { "/second": tests.http_response_bytes(status="302 Found", headers={"location": "/final"}, body=b"second redirect"), "": tests.http_response_bytes(status="302 Found", headers={"location": "/second"}, body=b"redirect body"), } with tests.server_route(routes, request_count=4) as uri: try: http.request(uri, "GET", redirections=1) assert False, "This should not happen" except httplib2.RedirectLimit: 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", redirections=1) assert response.status == 500 assert response.reason.startswith("Redirected more") assert response["status"] == "302" assert content == b"second redirect" assert response.previous is not None
def test_get_301(): # Test that we automatically follow 301 redirects # and that we cache the 301 response http = httplib2.Http(cache=tests.get_cache_path()) destination = "" routes = { "/final": tests.http_response_bytes(body=b"This is the final destination.\n"), "": tests.http_response_bytes( status="301 Now where did I leave that URL", headers={"location": "/final"}, body=b"redirect body", ), } with tests.server_route(routes, request_count=3) as uri: destination = urllib.parse.urljoin(uri, "/final") response1, content1 = http.request(uri, "GET") response2, content2 = http.request(uri, "GET") assert response1.status == 200 assert "content-location" in response2 assert response1["content-location"] == destination assert content1 == b"This is the final destination.\n" assert response1.previous.status == 301 assert not response1.previous.fromcache assert response2.status == 200 assert response2["content-location"] == destination assert content2 == b"This is the final destination.\n" assert response2.previous.status == 301 assert response2.previous.fromcache
def test_change_308(): # 308: follow with same method, cache redirect http = httplib2.Http(cache=tests.get_cache_path(), timeout=1) routes = { "/final": tests.make_http_reflect(), "": tests.http_response_bytes( status="308 Permanent Redirect", add_date=True, headers={ "cache-control": "max-age=300", "location": "/final" }, ), } with tests.server_route(routes, request_count=3) as uri: response, content = http.request(uri, "CHANGE", body=b"hello308") assert response.previous.status == 308 assert not response.previous.fromcache assert response.status == 200 assert not response.fromcache assert content.startswith(b"CHANGE /final HTTP") response, content = http.request(uri, "CHANGE") assert response.previous.status == 308 assert response.previous.fromcache assert response.status == 200 assert not response.fromcache assert content.startswith(b"CHANGE /final HTTP")
def test_get_302_redirection_limit(): # Test that we can set a lower redirection limit # and that we raise an exception when we exceed # that limit. http = httplib2.Http() http.force_exception_to_status_code = False routes = { '/second': tests.http_response_bytes( status='302 Found', headers={'location': '/final'}, body=b'second redirect'), '': tests.http_response_bytes( status='302 Found', headers={'location': '/second'}, body=b'redirect body'), } with tests.server_route(routes, request_count=4) as uri: try: http.request(uri, 'GET', redirections=1) assert False, 'This should not happen' except httplib2.RedirectLimit: 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', redirections=1) assert response.status == 500 assert response.reason.startswith('Redirected more') assert response['status'] == '302' assert content == b'second redirect' assert response.previous is not None
def test_get_302_redirection_limit(): # Test that we can set a lower redirection limit # and that we raise an exception when we exceed # that limit. http = httplib2.Http() http.force_exception_to_status_code = False routes = { '/second': tests.http_response_bytes(status='302 Found', headers={'location': '/final'}, body=b'second redirect'), '': tests.http_response_bytes(status='302 Found', headers={'location': '/second'}, body=b'redirect body'), } with tests.server_route(routes, request_count=4) as uri: try: http.request(uri, 'GET', redirections=1) assert False, 'This should not happen' except httplib2.RedirectLimit: 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', redirections=1) assert response.status == 500 assert response.reason.startswith('Redirected more') assert response['status'] == '302' assert content == b'second redirect' assert response.previous is not None
def test_get_300_with_location(): # Test the we automatically follow 300 redirects if a Location: header is provided http = httplib2.Http() final_content = b'This is the final destination.\n' routes = { '/final': tests.http_response_bytes(body=final_content), '': tests.http_response_bytes(status='300 Multiple Choices', headers={'location': '/final'}), } with tests.server_route(routes, request_count=2) as uri: response, content = http.request(uri, 'GET') assert response.status == 200 assert content == final_content assert response.previous.status == 300 assert not response.previous.fromcache # Confirm that the intermediate 300 is not cached with tests.server_route(routes, request_count=2) as uri: response, content = http.request(uri, 'GET') assert response.status == 200 assert content == final_content assert response.previous.status == 300 assert not response.previous.fromcache
def test_head_301(): # Test that we automatically follow 301 redirects http = httplib2.Http() destination = '' routes = { '/final': tests.http_response_bytes(body=b'This is the final destination.\n'), '': tests.http_response_bytes( status='301 Now where did I leave that URL', headers={'location': '/final'}, body=b'redirect body'), } with tests.server_route(routes, request_count=2) as uri: destination = urllib.parse.urljoin(uri, '/final') response, content = http.request(uri, 'HEAD') assert response.status == 200 assert response['content-location'] == destination assert response.previous.status == 301 assert not response.previous.fromcache
def test_get_302(): # Test that we automatically follow 302 redirects # and that we DO NOT cache the 302 response http = httplib2.Http(cache=tests.get_cache_path()) second_url, final_url = "", "" routes = { "/final": tests.http_response_bytes(body=b"This is the final destination.\n"), "/second": tests.http_response_bytes(status="302 Found", headers={"location": "/final"}, body=b"second redirect"), "": tests.http_response_bytes(status="302 Found", headers={"location": "/second"}, body=b"redirect body"), } with tests.server_route(routes, request_count=7) as uri: second_url = urllib.parse.urljoin(uri, "/second") final_url = urllib.parse.urljoin(uri, "/final") response1, content1 = http.request(second_url, "GET") response2, content2 = http.request(second_url, "GET") response3, content3 = http.request(uri, "GET") assert response1.status == 200 assert response1["content-location"] == final_url assert content1 == b"This is the final destination.\n" assert response1.previous.status == 302 assert not response1.previous.fromcache assert response2.status == 200 # FIXME: # assert response2.fromcache assert response2["content-location"] == final_url assert content2 == b"This is the final destination.\n" assert response2.previous.status == 302 assert not response2.previous.fromcache assert response2.previous["content-location"] == second_url assert response3.status == 200 # FIXME: # assert response3.fromcache assert content3 == b"This is the final destination.\n" assert response3.previous.status == 302 assert not response3.previous.fromcache
def test_get_302(): # Test that we automatically follow 302 redirects # and that we DO NOT cache the 302 response http = httplib2.Http(cache=tests.get_cache_path()) second_url, final_url = '', '' routes = { '/final': tests.http_response_bytes(body=b'This is the final destination.\n'), '/second': tests.http_response_bytes(status='302 Found', headers={'location': '/final'}, body=b'second redirect'), '': tests.http_response_bytes(status='302 Found', headers={'location': '/second'}, body=b'redirect body'), } with tests.server_route(routes, request_count=7) as uri: second_url = urllib.parse.urljoin(uri, '/second') final_url = urllib.parse.urljoin(uri, '/final') response1, content1 = http.request(second_url, 'GET') response2, content2 = http.request(second_url, 'GET') response3, content3 = http.request(uri, 'GET') assert response1.status == 200 assert response1['content-location'] == final_url assert content1 == b'This is the final destination.\n' assert response1.previous.status == 302 assert not response1.previous.fromcache assert response2.status == 200 # FIXME: # assert response2.fromcache assert response2['content-location'] == final_url assert content2 == b'This is the final destination.\n' assert response2.previous.status == 302 assert not response2.previous.fromcache assert response2.previous['content-location'] == second_url assert response3.status == 200 # FIXME: # assert response3.fromcache assert content3 == b'This is the final destination.\n' assert response3.previous.status == 302 assert not response3.previous.fromcache
def test_head_301(): # Test that we automatically follow 301 redirects http = httplib2.Http() destination = "" routes = { "/final": tests.http_response_bytes(body=b"This is the final destination.\n"), "": tests.http_response_bytes( status="301 Now where did I leave that URL", headers={"location": "/final"}, body=b"redirect body", ), } with tests.server_route(routes, request_count=2) as uri: destination = urllib.parse.urljoin(uri, "/final") response, content = http.request(uri, "HEAD") assert response.status == 200 assert response["content-location"] == destination assert response.previous.status == 301 assert not response.previous.fromcache
def test_head_301(): # Test that we automatically follow 301 redirects http = httplib2.Http() destination = '' routes = { '/final': tests.http_response_bytes(body=b'This is the final destination.\n'), '': tests.http_response_bytes(status='301 Now where did I leave that URL', headers={'location': '/final'}, body=b'redirect body'), } with tests.server_route(routes, request_count=2) as uri: destination = urllib.parse.urljoin(uri, '/final') response, content = http.request(uri, 'HEAD') assert response.status == 200 assert response['content-location'] == destination assert response.previous.status == 301 assert not response.previous.fromcache
def test_get_302(): # Test that we automatically follow 302 redirects # and that we DO NOT cache the 302 response http = httplib2.Http(cache=tests.get_cache_path()) second_url, final_url = "", "" routes = { "/final": tests.http_response_bytes(body=b"This is the final destination.\n"), "/second": tests.http_response_bytes( status="302 Found", headers={"location": "/final"}, body=b"second redirect" ), "": tests.http_response_bytes( status="302 Found", headers={"location": "/second"}, body=b"redirect body" ), } with tests.server_route(routes, request_count=7) as uri: second_url = urllib.parse.urljoin(uri, "/second") final_url = urllib.parse.urljoin(uri, "/final") response1, content1 = http.request(second_url, "GET") response2, content2 = http.request(second_url, "GET") response3, content3 = http.request(uri, "GET") assert response1.status == 200 assert response1["content-location"] == final_url assert content1 == b"This is the final destination.\n" assert response1.previous.status == 302 assert not response1.previous.fromcache assert response2.status == 200 # FIXME: # assert response2.fromcache assert response2["content-location"] == final_url assert content2 == b"This is the final destination.\n" assert response2.previous.status == 302 assert not response2.previous.fromcache assert response2.previous["content-location"] == second_url assert response3.status == 200 # FIXME: # assert response3.fromcache assert content3 == b"This is the final destination.\n" assert response3.previous.status == 302 assert not response3.previous.fromcache
def test_get_302(): # Test that we automatically follow 302 redirects # and that we DO NOT cache the 302 response http = httplib2.Http(cache=tests.get_cache_path()) second_url, final_url = '', '' routes = { '/final': tests.http_response_bytes(body=b'This is the final destination.\n'), '/second': tests.http_response_bytes( status='302 Found', headers={'location': '/final'}, body=b'second redirect'), '': tests.http_response_bytes( status='302 Found', headers={'location': '/second'}, body=b'redirect body'), } with tests.server_route(routes, request_count=7) as uri: second_url = urllib.parse.urljoin(uri, '/second') final_url = urllib.parse.urljoin(uri, '/final') response1, content1 = http.request(second_url, 'GET') response2, content2 = http.request(second_url, 'GET') response3, content3 = http.request(uri, 'GET') assert response1.status == 200 assert response1['content-location'] == final_url assert content1 == b'This is the final destination.\n' assert response1.previous.status == 302 assert not response1.previous.fromcache assert response2.status == 200 # FIXME: # assert response2.fromcache assert response2['content-location'] == final_url assert content2 == b'This is the final destination.\n' assert response2.previous.status == 302 assert not response2.previous.fromcache assert response2.previous['content-location'] == second_url assert response3.status == 200 # FIXME: # assert response3.fromcache assert content3 == b'This is the final destination.\n' assert response3.previous.status == 302 assert not response3.previous.fromcache