def test_response_code(scheme, tmpdir): """Ensure we can read a response code from a fetch""" url = scheme + "://httpbin.org/" with vcr.use_cassette(str(tmpdir.join("atts.yaml"))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) assert urllib2.urlopen(url).getcode() == urllib2.urlopen(url).getcode() # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
def test_random_body(scheme, tmpdir): """Ensure we can read the content, and that it's served from cache""" url = scheme + "://httpbin.org/bytes/1024" with vcr.use_cassette(str(tmpdir.join("body.yaml"))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) assert urllib2.urlopen(url).read() == urllib2.urlopen(url).read() # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
def test_body(tmpdir, scheme): '''Ensure the responses are all identical enough''' url = scheme + '://httpbin.org/bytes/1024' with vcr.use_cassette(str(tmpdir.join('body.yaml'))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) assert requests.get(url).content == requests.get(url).content # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
def test_random_body(scheme, tmpdir): '''Ensure we can read the content, and that it's served from cache''' url = scheme + '://httpbin.org/bytes/1024' with vcr.use_cassette(str(tmpdir.join('body.yaml'))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) assert urllib2.urlopen(url).read() == urllib2.urlopen(url).read() # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
def test_headers(scheme, tmpdir): '''Ensure that we can read the headers back''' url = scheme + '://httpbin.org/' with vcr.use_cassette(str(tmpdir.join('headers.yaml'))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) assert requests.get(url).headers == requests.get(url).headers # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
def test_response_code(scheme, tmpdir): '''Ensure we can read a response code from a fetch''' url = scheme + '://httpbin.org/' with vcr.use_cassette(str(tmpdir.join('atts.yaml'))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) assert urllib2.urlopen(url).getcode() == urllib2.urlopen(url).getcode() # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
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_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_post_unicode_data(scheme, tmpdir): '''Ensure that it works when posting unicode data''' data = urlencode({'snowman': u'☃'.encode('utf-8')}) url = scheme + '://httpbin.org/post' with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))) as cass: res1 = urllib2.urlopen(url, data).read() with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))) as cass: res2 = urllib2.urlopen(url, data).read() assert res1 == res2 assert_cassette_has_one_response(cass)
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_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_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_response_headers(scheme, tmpdir): '''Ensure we can get information from the response''' url = scheme + '://httpbin.org/' with vcr.use_cassette(str(tmpdir.join('headers.yaml'))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) open1 = urllib2.urlopen(url).info().items() open2 = urllib2.urlopen(url).info().items() assert open1 == open2 # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
def test_response_headers(scheme, tmpdir): """Ensure we can get information from the response""" url = scheme + "://httpbin.org/" with vcr.use_cassette(str(tmpdir.join("headers.yaml"))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) open1 = urllib2.urlopen(url).info().items() open2 = urllib2.urlopen(url).info().items() assert open1 == open2 # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
def test_post_data(tmpdir, httpbin_both): '''Ensure that it works when posting data''' data = urlencode({'some': 1, 'data': 'here'}) url = httpbin_both.url + '/post' with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))): _, res1 = http().request(url, "POST", data) with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))) as cass: _, res2 = http().request(url, "POST", data) assert res1 == res2 assert_cassette_has_one_response(cass)
def test_post_data(scheme, tmpdir): '''Ensure that it works when posting data''' data = urlencode({'some': 1, 'data': 'here'}).encode('utf-8') url = scheme + '://httpbin.org/post' with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))) as cass: res1 = urlopen(url, data).read() with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))) as cass: res2 = urlopen(url, data).read() assert res1 == res2 assert_cassette_has_one_response(cass)
def test_post(tmpdir, scheme): '''Ensure that we can post and cache the results''' data = {'key1': 'value1', 'key2': 'value2'} url = scheme + '://httpbin.org/post' with vcr.use_cassette(str(tmpdir.join('requests.yaml'))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) req1 = requests.post(url, data).content req2 = requests.post(url, data).content assert req1 == req2 # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
def test_post_data(scheme, tmpdir): '''Ensure that it works when posting data''' data = urlencode({'some': 1, 'data': 'here'}) url = scheme + '://httpbin.org/post' with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) res1 = urllib2.urlopen(url, data).read() res2 = urllib2.urlopen(url, data).read() assert res1 == res2 # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
def test_post_unicode_data(tmpdir, httpbin_both): '''Ensure that it works when posting unicode data''' data = urlencode({'snowman': u'☃'.encode('utf-8')}) url = httpbin_both.url + '/post' with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))): _, res1 = http().request(url, "POST", data) with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))) as cass: _, res2 = http().request(url, "POST", data) assert res1 == res2 assert_cassette_has_one_response(cass)
def test_post_data(scheme, tmpdir): '''Ensure that it works when posting data''' data = urlencode({'some': 1, 'data': 'here'}) url = scheme + '://httpbin.org/post' with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))) as cass: _, res1 = httplib2.Http().request(url, "POST", data) with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))) as cass: _, res2 = httplib2.Http().request(url, "POST", data) assert res1 == res2 assert_cassette_has_one_response(cass)
def test_post_data(scheme, tmpdir): """Ensure that it works when posting data""" data = urlencode({"some": 1, "data": "here"}) url = scheme + "://httpbin.org/post" with vcr.use_cassette(str(tmpdir.join("post_data.yaml"))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) res1 = urllib2.urlopen(url, data).read() res2 = urllib2.urlopen(url, data).read() assert res1 == res2 # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
def test_post_data(tmpdir, httpbin_both): """Ensure that it works when posting data""" data = urlencode({"some": 1, "data": "here"}) url = httpbin_both.url + "/post" with vcr.use_cassette(str(tmpdir.join("post_data.yaml"))): _, res1 = http().request(url, "POST", data) with vcr.use_cassette(str(tmpdir.join("post_data.yaml"))) as cass: _, res2 = http().request(url, "POST", data) assert res1 == res2 assert_cassette_has_one_response(cass)
def test_post_unicode_data(tmpdir, httpbin_both): """Ensure that it works when posting unicode data""" data = urlencode({"snowman": u"☃".encode("utf-8")}) url = httpbin_both.url + "/post" with vcr.use_cassette(str(tmpdir.join("post_data.yaml"))): _, res1 = http().request(url, "POST", data) with vcr.use_cassette(str(tmpdir.join("post_data.yaml"))) as cass: _, res2 = http().request(url, "POST", data) assert res1 == res2 assert_cassette_has_one_response(cass)
def test_auth_failed(tmpdir, scheme): '''Ensure that we can save failed auth statuses''' auth = ('user', 'wrongwrongwrong') url = scheme + '://httpbin.org/basic-auth/user/passwd' with vcr.use_cassette(str(tmpdir.join('auth-failed.yaml'))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) one = requests.get(url, auth=auth) two = requests.get(url, auth=auth) assert one.content == two.content assert one.status_code == two.status_code == 401 # Ensure that we've now cached a single response assert_cassette_has_one_response(cass)
def test_post_data(httpbin_both, tmpdir): """Ensure that it works when posting data""" data = urlencode({"some": 1, "data": "here"}).encode("utf-8") url = httpbin_both.url + "/post" with vcr.use_cassette(str(tmpdir.join("post_data.yaml"))): res1 = urlopen_with_cafile(url, data).read() with vcr.use_cassette(str(tmpdir.join("post_data.yaml"))) as cass: res2 = urlopen_with_cafile(url, data).read() assert len(cass) == 1 assert res1 == res2 assert_cassette_has_one_response(cass)
def test_post_data(httpbin_both, tmpdir): '''Ensure that it works when posting data''' data = urlencode({'some': 1, 'data': 'here'}).encode('utf-8') url = httpbin_both.url + '/post' with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))): res1 = urlopen_with_cafile(url, data).read() with vcr.use_cassette(str(tmpdir.join('post_data.yaml'))) as cass: res2 = urlopen_with_cafile(url, data).read() assert len(cass) == 1 assert res1 == res2 assert_cassette_has_one_response(cass)