'''Integration tests with urllib2''' # coding=utf-8 # External imports import os import urllib2 from urllib import urlencode import pytest # Internal imports import vcr from assertions import assert_cassette_empty, assert_cassette_has_one_response @pytest.fixture(params=["https","http"]) def scheme(request): """ Fixture that returns both http and https """ return request.param 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_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) assert urllib2.urlopen(url).info().items() == urllib2.urlopen(url).info().items() # Ensure that we've now cached a single response assert_cassette_has_one_response(cass) def test_multiple_requests(scheme, tmpdir): '''Ensure that we can cache multiple requests''' urls = [ scheme + '://httpbin.org/', scheme + '://httpbin.org/get', scheme + '://httpbin.org/bytes/1024' ] with vcr.use_cassette(str(tmpdir.join('multiple.yaml'))) as cass: for index in range(len(urls)): url = urls[index] assert len(cass) == index assert cass.play_count == index assert urllib2.urlopen(url).read() == urllib2.urlopen(url).read() assert len(cass) == index + 1 assert cass.play_count == index + 1 def test_get_data(scheme, tmpdir): '''Ensure that it works with query data''' data = urlencode({'some': 1, 'data': 'here'}) url = scheme + '://httpbin.org/get?' + data with vcr.use_cassette(str(tmpdir.join('get_data.yaml'))) as cass: # Ensure that this is empty to begin with assert_cassette_empty(cass) res1 = urllib2.urlopen(url).read() res2 = urllib2.urlopen(url).read() assert res1 == res2 # Ensure that we've now cached a single response assert len(cass) == 1 assert cass.play_count == 1 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(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: # 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_cross_scheme(tmpdir): '''Ensure that requests between schemes are treated separately''' # First fetch a url under https, and then again under https and then # ensure that we haven't served anything out of cache, and we have two # requests / response pairs in the cassette with vcr.use_cassette(str(tmpdir.join('cross_scheme.yaml'))) as cass: urllib2.urlopen('https://httpbin.org/') urllib2.urlopen('http://httpbin.org/') assert len(cass) == 2 assert cass.play_count == 0