1import httplib2 2import tests 3 4 5def test_gzip_head(): 6 # Test that we don't try to decompress a HEAD response 7 http = httplib2.Http() 8 response = tests.http_response_bytes( 9 headers={"content-encoding": "gzip", "content-length": 42} 10 ) 11 with tests.server_const_bytes(response) as uri: 12 response, content = http.request(uri, "HEAD") 13 assert response.status == 200 14 assert int(response["content-length"]) != 0 15 assert content == b"" 16 17 18def test_gzip_get(): 19 # Test that we support gzip compression 20 http = httplib2.Http() 21 response = tests.http_response_bytes( 22 headers={"content-encoding": "gzip"}, 23 body=tests.gzip_compress(b"properly compressed"), 24 ) 25 with tests.server_const_bytes(response) as uri: 26 response, content = http.request(uri, "GET") 27 assert response.status == 200 28 assert "content-encoding" not in response 29 assert "-content-encoding" in response 30 assert int(response["content-length"]) == len(b"properly compressed") 31 assert content == b"properly compressed" 32 33 34def test_gzip_post_response(): 35 http = httplib2.Http() 36 response = tests.http_response_bytes( 37 headers={"content-encoding": "gzip"}, 38 body=tests.gzip_compress(b"properly compressed"), 39 ) 40 with tests.server_const_bytes(response) as uri: 41 response, content = http.request(uri, "POST", body=b"") 42 assert response.status == 200 43 assert "content-encoding" not in response 44 assert "-content-encoding" in response 45 46 47def test_gzip_malformed_response(): 48 http = httplib2.Http() 49 # Test that we raise a good exception when the gzip fails 50 http.force_exception_to_status_code = False 51 response = tests.http_response_bytes( 52 headers={"content-encoding": "gzip"}, body=b"obviously not compressed" 53 ) 54 with tests.server_const_bytes(response, request_count=2) as uri: 55 with tests.assert_raises(httplib2.FailedToDecompressContent): 56 http.request(uri, "GET") 57 58 # Re-run the test with out the exceptions 59 http.force_exception_to_status_code = True 60 61 response, content = http.request(uri, "GET") 62 assert response.status == 500 63 assert response.reason.startswith("Content purported") 64 65 66def test_deflate_get(): 67 # Test that we support deflate compression 68 http = httplib2.Http() 69 response = tests.http_response_bytes( 70 headers={"content-encoding": "deflate"}, 71 body=tests.deflate_compress(b"properly compressed"), 72 ) 73 with tests.server_const_bytes(response) as uri: 74 response, content = http.request(uri, "GET") 75 assert response.status == 200 76 assert "content-encoding" not in response 77 assert int(response["content-length"]) == len(b"properly compressed") 78 assert content == b"properly compressed" 79 80 81def test_deflate_malformed_response(): 82 # Test that we raise a good exception when the deflate fails 83 http = httplib2.Http() 84 http.force_exception_to_status_code = False 85 response = tests.http_response_bytes( 86 headers={"content-encoding": "deflate"}, body=b"obviously not compressed" 87 ) 88 with tests.server_const_bytes(response, request_count=2) as uri: 89 with tests.assert_raises(httplib2.FailedToDecompressContent): 90 http.request(uri, "GET") 91 92 # Re-run the test with out the exceptions 93 http.force_exception_to_status_code = True 94 95 response, content = http.request(uri, "GET") 96 assert response.status == 500 97 assert response.reason.startswith("Content purported") 98