blob: c7eead5c14d14630c73ac6ab1c3f049cf39401c4 [file] [log] [blame]
Sergey Shepelev0112eff2017-05-05 06:46:43 +03001import 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(
Alex Yuaa1b95b2018-07-26 23:23:35 -04009 headers={"content-encoding": "gzip", "content-length": 42}
Sergey Shepelev0112eff2017-05-05 06:46:43 +030010 )
11 with tests.server_const_bytes(response) as uri:
Alex Yuaa1b95b2018-07-26 23:23:35 -040012 response, content = http.request(uri, "HEAD")
Sergey Shepelev0112eff2017-05-05 06:46:43 +030013 assert response.status == 200
Alex Yuaa1b95b2018-07-26 23:23:35 -040014 assert int(response["content-length"]) != 0
15 assert content == b""
Sergey Shepelev0112eff2017-05-05 06:46:43 +030016
17
18def test_gzip_get():
19 # Test that we support gzip compression
20 http = httplib2.Http()
21 response = tests.http_response_bytes(
Alex Yuaa1b95b2018-07-26 23:23:35 -040022 headers={"content-encoding": "gzip"},
23 body=tests.gzip_compress(b"properly compressed"),
Sergey Shepelev0112eff2017-05-05 06:46:43 +030024 )
25 with tests.server_const_bytes(response) as uri:
Alex Yuaa1b95b2018-07-26 23:23:35 -040026 response, content = http.request(uri, "GET")
Sergey Shepelev0112eff2017-05-05 06:46:43 +030027 assert response.status == 200
Alex Yuaa1b95b2018-07-26 23:23:35 -040028 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"
Sergey Shepelev0112eff2017-05-05 06:46:43 +030032
33
34def test_gzip_post_response():
35 http = httplib2.Http()
36 response = tests.http_response_bytes(
Alex Yuaa1b95b2018-07-26 23:23:35 -040037 headers={"content-encoding": "gzip"},
38 body=tests.gzip_compress(b"properly compressed"),
Sergey Shepelev0112eff2017-05-05 06:46:43 +030039 )
40 with tests.server_const_bytes(response) as uri:
Alex Yuaa1b95b2018-07-26 23:23:35 -040041 response, content = http.request(uri, "POST", body=b"")
Sergey Shepelev0112eff2017-05-05 06:46:43 +030042 assert response.status == 200
Alex Yuaa1b95b2018-07-26 23:23:35 -040043 assert "content-encoding" not in response
44 assert "-content-encoding" in response
Sergey Shepelev0112eff2017-05-05 06:46:43 +030045
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(
Alex Yuaa1b95b2018-07-26 23:23:35 -040052 headers={"content-encoding": "gzip"}, body=b"obviously not compressed"
Sergey Shepelev0112eff2017-05-05 06:46:43 +030053 )
54 with tests.server_const_bytes(response, request_count=2) as uri:
55 with tests.assert_raises(httplib2.FailedToDecompressContent):
Alex Yuaa1b95b2018-07-26 23:23:35 -040056 http.request(uri, "GET")
Sergey Shepelev0112eff2017-05-05 06:46:43 +030057
58 # Re-run the test with out the exceptions
59 http.force_exception_to_status_code = True
60
Alex Yuaa1b95b2018-07-26 23:23:35 -040061 response, content = http.request(uri, "GET")
Sergey Shepelev0112eff2017-05-05 06:46:43 +030062 assert response.status == 500
Alex Yuaa1b95b2018-07-26 23:23:35 -040063 assert response.reason.startswith("Content purported")
Sergey Shepelev0112eff2017-05-05 06:46:43 +030064
65
66def test_deflate_get():
67 # Test that we support deflate compression
68 http = httplib2.Http()
69 response = tests.http_response_bytes(
Alex Yuaa1b95b2018-07-26 23:23:35 -040070 headers={"content-encoding": "deflate"},
71 body=tests.deflate_compress(b"properly compressed"),
Sergey Shepelev0112eff2017-05-05 06:46:43 +030072 )
73 with tests.server_const_bytes(response) as uri:
Alex Yuaa1b95b2018-07-26 23:23:35 -040074 response, content = http.request(uri, "GET")
Sergey Shepelev0112eff2017-05-05 06:46:43 +030075 assert response.status == 200
Alex Yuaa1b95b2018-07-26 23:23:35 -040076 assert "content-encoding" not in response
77 assert int(response["content-length"]) == len(b"properly compressed")
78 assert content == b"properly compressed"
Sergey Shepelev0112eff2017-05-05 06:46:43 +030079
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(
Alex Yuaa1b95b2018-07-26 23:23:35 -040086 headers={"content-encoding": "deflate"}, body=b"obviously not compressed"
Sergey Shepelev0112eff2017-05-05 06:46:43 +030087 )
88 with tests.server_const_bytes(response, request_count=2) as uri:
89 with tests.assert_raises(httplib2.FailedToDecompressContent):
Alex Yuaa1b95b2018-07-26 23:23:35 -040090 http.request(uri, "GET")
Sergey Shepelev0112eff2017-05-05 06:46:43 +030091
92 # Re-run the test with out the exceptions
93 http.force_exception_to_status_code = True
94
Alex Yuaa1b95b2018-07-26 23:23:35 -040095 response, content = http.request(uri, "GET")
Sergey Shepelev0112eff2017-05-05 06:46:43 +030096 assert response.status == 500
Alex Yuaa1b95b2018-07-26 23:23:35 -040097 assert response.reason.startswith("Content purported")