Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 1 | import httplib2 |
| 2 | import tests |
| 3 | |
| 4 | |
| 5 | def 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 Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 9 | headers={"content-encoding": "gzip", "content-length": 42} |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 10 | ) |
| 11 | with tests.server_const_bytes(response) as uri: |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 12 | response, content = http.request(uri, "HEAD") |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 13 | assert response.status == 200 |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 14 | assert int(response["content-length"]) != 0 |
| 15 | assert content == b"" |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 16 | |
| 17 | |
| 18 | def test_gzip_get(): |
| 19 | # Test that we support gzip compression |
| 20 | http = httplib2.Http() |
| 21 | response = tests.http_response_bytes( |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 22 | headers={"content-encoding": "gzip"}, |
| 23 | body=tests.gzip_compress(b"properly compressed"), |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 24 | ) |
| 25 | with tests.server_const_bytes(response) as uri: |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 26 | response, content = http.request(uri, "GET") |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 27 | assert response.status == 200 |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 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" |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def test_gzip_post_response(): |
| 35 | http = httplib2.Http() |
| 36 | response = tests.http_response_bytes( |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 37 | headers={"content-encoding": "gzip"}, |
| 38 | body=tests.gzip_compress(b"properly compressed"), |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 39 | ) |
| 40 | with tests.server_const_bytes(response) as uri: |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 41 | response, content = http.request(uri, "POST", body=b"") |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 42 | assert response.status == 200 |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 43 | assert "content-encoding" not in response |
| 44 | assert "-content-encoding" in response |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def 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 Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 52 | headers={"content-encoding": "gzip"}, body=b"obviously not compressed" |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 53 | ) |
| 54 | with tests.server_const_bytes(response, request_count=2) as uri: |
| 55 | with tests.assert_raises(httplib2.FailedToDecompressContent): |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 56 | http.request(uri, "GET") |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 57 | |
| 58 | # Re-run the test with out the exceptions |
| 59 | http.force_exception_to_status_code = True |
| 60 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 61 | response, content = http.request(uri, "GET") |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 62 | assert response.status == 500 |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 63 | assert response.reason.startswith("Content purported") |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 64 | |
| 65 | |
| 66 | def test_deflate_get(): |
| 67 | # Test that we support deflate compression |
| 68 | http = httplib2.Http() |
| 69 | response = tests.http_response_bytes( |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 70 | headers={"content-encoding": "deflate"}, |
| 71 | body=tests.deflate_compress(b"properly compressed"), |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 72 | ) |
| 73 | with tests.server_const_bytes(response) as uri: |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 74 | response, content = http.request(uri, "GET") |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 75 | assert response.status == 200 |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 76 | assert "content-encoding" not in response |
| 77 | assert int(response["content-length"]) == len(b"properly compressed") |
| 78 | assert content == b"properly compressed" |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 79 | |
| 80 | |
| 81 | def 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 Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 86 | headers={"content-encoding": "deflate"}, body=b"obviously not compressed" |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 87 | ) |
| 88 | with tests.server_const_bytes(response, request_count=2) as uri: |
| 89 | with tests.assert_raises(httplib2.FailedToDecompressContent): |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 90 | http.request(uri, "GET") |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 91 | |
| 92 | # Re-run the test with out the exceptions |
| 93 | http.force_exception_to_status_code = True |
| 94 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 95 | response, content = http.request(uri, "GET") |
Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame] | 96 | assert response.status == 500 |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 97 | assert response.reason.startswith("Content purported") |