blob: df991a1f8ea75e4aab4821d9b4f3586d94225270 [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(
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'},
53 body=b'obviously not compressed',
54 )
55 with tests.server_const_bytes(response, request_count=2) as uri:
56 with tests.assert_raises(httplib2.FailedToDecompressContent):
57 http.request(uri, 'GET')
58
59 # Re-run the test with out the exceptions
60 http.force_exception_to_status_code = True
61
62 response, content = http.request(uri, 'GET')
63 assert response.status == 500
64 assert response.reason.startswith('Content purported')
65
66
67def test_deflate_get():
68 # Test that we support deflate compression
69 http = httplib2.Http()
70 response = tests.http_response_bytes(
71 headers={'content-encoding': 'deflate'},
72 body=tests.deflate_compress(b'properly compressed'),
73 )
74 with tests.server_const_bytes(response) as uri:
75 response, content = http.request(uri, 'GET')
76 assert response.status == 200
77 assert 'content-encoding' not in response
78 assert int(response['content-length']) == len(b'properly compressed')
79 assert content == b'properly compressed'
80
81
82def test_deflate_malformed_response():
83 # Test that we raise a good exception when the deflate fails
84 http = httplib2.Http()
85 http.force_exception_to_status_code = False
86 response = tests.http_response_bytes(
87 headers={'content-encoding': 'deflate'},
88 body=b'obviously not compressed',
89 )
90 with tests.server_const_bytes(response, request_count=2) as uri:
91 with tests.assert_raises(httplib2.FailedToDecompressContent):
92 http.request(uri, 'GET')
93
94 # Re-run the test with out the exceptions
95 http.force_exception_to_status_code = True
96
97 response, content = http.request(uri, 'GET')
98 assert response.status == 500
99 assert response.reason.startswith('Content purported')