def test_original_decoded_response_is_not_modified(tmpdir, httpbin): testfile = str(tmpdir.join('decoded_response.yml')) host, port = httpbin.host, httpbin.port conn = httplib.HTTPConnection(host, port) conn.request('GET', '/gzip') outside = conn.getresponse() with vcr.use_cassette(testfile, decode_compressed_response=True): conn = httplib.HTTPConnection(host, port) conn.request('GET', '/gzip') inside = conn.getresponse() # Assert that we do not modify the original response while appending # to the casssette. assert 'gzip' == inside.headers['content-encoding'] # They should effectively be the same response. inside_headers = (h for h in inside.headers.items() if h[0] != 'Date') outside_headers = (h for h in outside.getheaders() if h[0] != 'Date') assert set(inside_headers) == set(outside_headers) assert inside.read() == outside.read() # Even though the above are raw bytes, the JSON data should have been # decoded and saved to the cassette. with vcr.use_cassette(testfile): conn = httplib.HTTPConnection(host, port) conn.request('GET', '/gzip') inside = conn.getresponse() assert 'content-encoding' not in inside.headers assert_is_json(inside.read())
def test_decompress_regular(tmpdir, httpbin): """Test that it doesn't try to decompress content that isn't compressed""" url = httpbin.url + '/get' cass_file = str(tmpdir.join('noncompressed_response.yaml')) with vcr.use_cassette(cass_file, decode_compressed_response=True): urlopen(url) with vcr.use_cassette(cass_file) as cass: resp = urlopen(url).read() assert_cassette_has_one_response(cass) assert_is_json(resp)
def test_decompress_gzip(tmpdir): url = "http://httpbin.org/gzip" request = Request(url, headers={"Accept-Encoding": ["gzip, deflate"]}) cass_file = str(tmpdir.join("gzip_response.yaml")) with vcr.use_cassette(cass_file, decode_compressed_response=True): urlopen(request) with vcr.use_cassette(cass_file) as cass: decoded_response = urlopen(url).read() assert_cassette_has_one_response(cass) assert_is_json(decoded_response)
def test_decompress_deflate(tmpdir): url = 'http://httpbin.org/deflate' request = Request(url, headers={'Accept-Encoding': ['gzip, deflate']}) cass_file = str(tmpdir.join('deflate_response.yaml')) with vcr.use_cassette(cass_file, decode_compressed_response=True): urlopen(request) with vcr.use_cassette(cass_file) as cass: decoded_response = urlopen(url).read() assert_cassette_has_one_response(cass) assert_is_json(decoded_response)
def test_decompress_gzip(tmpdir, httpbin): url = httpbin.url + "/gzip" request = Request(url, headers={"Accept-Encoding": ["gzip, deflate"]}) cass_file = str(tmpdir.join("gzip_response.yaml")) with vcr.use_cassette(cass_file, decode_compressed_response=True): urlopen(request) with vcr.use_cassette(cass_file) as cass: decoded_response = urlopen(url).read() assert_cassette_has_one_response(cass) assert_is_json(decoded_response)
def test_decompress_gzip(tmpdir, httpbin): url = httpbin.url + '/gzip' request = Request(url, headers={'Accept-Encoding': ['gzip, deflate']}) cass_file = str(tmpdir.join('gzip_response.yaml')) with vcr.use_cassette(cass_file, decode_compressed_response=True): urlopen(request) with vcr.use_cassette(cass_file) as cass: decoded_response = urlopen(url).read() assert_cassette_has_one_response(cass) assert_is_json(decoded_response)
def test_decompress_regular(tmpdir): """Test that it doesn't try to decompress content that isn't compressed""" url = 'http://httpbin.org/get' cass_file = str(tmpdir.join('noncompressed_response.yaml')) with vcr.use_cassette(cass_file, decode_compressed_response=True): urlopen(url) with vcr.use_cassette(cass_file) as cass: resp = urlopen(url).read() assert_cassette_has_one_response(cass) assert_is_json(resp)
def test_gzip(tmpdir, httpbin_both): """ Ensure that requests (actually urllib3) is able to automatically decompress the response body """ url = httpbin_both + "/gzip" response = requests.get(url) with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))): response = requests.get(url) assert_is_json(response.content) with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))): assert_is_json(response.content)
def test_gzip(tmpdir, httpbin_both, verify_pool_mgr): ''' Ensure that requests (actually urllib3) is able to automatically decompress the response body ''' url = httpbin_both.url + '/gzip' response = verify_pool_mgr.request('GET', url) with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): response = verify_pool_mgr.request('GET', url) assert_is_json(response.data) with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): assert_is_json(response.data)
def test_gzip(tmpdir, httpbin_both, verify_pool_mgr): """ Ensure that requests (actually urllib3) is able to automatically decompress the response body """ url = httpbin_both.url + "/gzip" response = verify_pool_mgr.request("GET", url) with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))): response = verify_pool_mgr.request("GET", url) assert_is_json(response.data) with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))): assert_is_json(response.data)
def test_gzip(tmpdir, scheme, verify_pool_mgr): ''' Ensure that requests (actually urllib3) is able to automatically decompress the response body ''' url = scheme + '://httpbin.org/gzip' response = verify_pool_mgr.request('GET', url) with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): response = verify_pool_mgr.request('GET', url) assert_is_json(response.data) with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): assert_is_json(response.data)
def test_gzip(tmpdir, httpbin_both): ''' Ensure that requests (actually urllib3) is able to automatically decompress the response body ''' url = httpbin_both + '/gzip' response = requests.get(url) with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): response = requests.get(url) assert_is_json(response.content) with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): assert_is_json(response.content)
def test_gzip(tmpdir, scheme): ''' Ensure that requests (actually urllib3) is able to automatically decompress the response body ''' url = scheme + '://httpbin.org/gzip' response = requests.get(url) with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): response = requests.get(url) assert_is_json(response.content) with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): assert_is_json(response.content)
def test_gzip(get_client, tmpdir, scheme): ''' Ensure that httpclient is able to automatically decompress the response body ''' url = scheme + '://httpbin.org/gzip' with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): response = yield get(get_client(), url, decompress_response=True) assert_is_json(response.body) with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))) as cass: response = yield get(get_client(), url, decompress_response=True) assert_is_json(response.body) assert 1 == cass.play_count
def test_gzip(get_client, tmpdir, scheme): """ Ensure that httpclient is able to automatically decompress the response body """ url = scheme + "://httpbin.org/gzip" # use_gzip was renamed to decompress_response in 4.0 kwargs = {} if tornado.version_info < (4, ): kwargs["use_gzip"] = True else: kwargs["decompress_response"] = True with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))): response = yield get(get_client(), url, **kwargs) assert_is_json(response.body) with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))) as cass: response = yield get(get_client(), url, **kwargs) assert_is_json(response.body) assert 1 == cass.play_count
def test_gzip(get_client, tmpdir, scheme): ''' Ensure that httpclient is able to automatically decompress the response body ''' url = scheme + '://httpbin.org/gzip' # use_gzip was renamed to decompress_response in 4.0 kwargs = {} if tornado.version_info < (4,): kwargs['use_gzip'] = True else: kwargs['decompress_response'] = True with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): response = yield get(get_client(), url, **kwargs) assert_is_json(response.body) with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))) as cass: response = yield get(get_client(), url, **kwargs) assert_is_json(response.body) assert 1 == cass.play_count
def test_original_decoded_response_is_not_modified(tmpdir, httpbin): testfile = str(tmpdir.join("decoded_response.yml")) host, port = httpbin.host, httpbin.port conn = httplib.HTTPConnection(host, port) conn.request("GET", "/gzip") outside = conn.getresponse() with vcr.use_cassette(testfile, decode_compressed_response=True): conn = httplib.HTTPConnection(host, port) conn.request("GET", "/gzip") inside = conn.getresponse() # Assert that we do not modify the original response while appending # to the casssette. assert "gzip" == inside.headers["content-encoding"] # They should effectively be the same response. inside_headers = (h for h in inside.headers.items() if h[0].lower() != "date") outside_headers = (h for h in outside.getheaders() if h[0].lower() != "date") assert set(inside_headers) == set(outside_headers) inside = zlib.decompress(inside.read(), 16 + zlib.MAX_WBITS) outside = zlib.decompress(outside.read(), 16 + zlib.MAX_WBITS) assert inside == outside # Even though the above are raw bytes, the JSON data should have been # decoded and saved to the cassette. with vcr.use_cassette(testfile): conn = httplib.HTTPConnection(host, port) conn.request("GET", "/gzip") inside = conn.getresponse() assert "content-encoding" not in inside.headers assert_is_json(inside.read())