Replace urllib2 with requests in urlfetch module

simple replacement of urllib2.urlopen with requests.get

Fixes LP# 1201534

Change-Id: Ia0a2ed3b092e22246339a42f432086dd05f788fa
This commit is contained in:
Davanum Srinivas 2013-07-23 22:08:20 -04:00
parent 07a2840585
commit 50e177e3c9
2 changed files with 17 additions and 10 deletions

View File

@ -17,7 +17,7 @@
Utility for fetching a resource (e.g. a template) from a URL.
'''
import urllib2
import requests
import urlparse
from heat.openstack.common import log as logging
@ -38,7 +38,6 @@ def get(url):
components = urlparse.urlparse(url)
if components.scheme not in ('http', 'https'):
raise urllib2.URLError('Invalid URL scheme %s' % components.scheme)
raise IOError('Invalid URL scheme %s' % components.scheme)
response = urllib2.urlopen(url)
return response.read()
return requests.get(url).text

View File

@ -13,17 +13,25 @@
# License for the specific language governing permissions and limitations
# under the License.
import StringIO
import urllib2
import requests
from heat.common import urlfetch
from heat.tests.common import HeatTestCase
class Response:
def __init__(self, buf=''):
self._text = buf
@property
def text(self):
return self._text
class UrlFetchTest(HeatTestCase):
def setUp(self):
super(UrlFetchTest, self).setUp()
self.m.StubOutWithMock(urllib2, 'urlopen')
self.m.StubOutWithMock(requests, 'get')
def test_file_scheme(self):
self.m.ReplayAll()
@ -34,7 +42,7 @@ class UrlFetchTest(HeatTestCase):
url = 'http://example.com/template'
data = '{ "foo": "bar" }'
urllib2.urlopen(url).AndReturn(StringIO.StringIO(data))
requests.get(url).AndReturn(Response(data))
self.m.ReplayAll()
self.assertEqual(urlfetch.get(url), data)
@ -44,7 +52,7 @@ class UrlFetchTest(HeatTestCase):
url = 'https://example.com/template'
data = '{ "foo": "bar" }'
urllib2.urlopen(url).AndReturn(StringIO.StringIO(data))
requests.get(url).AndReturn(Response(data))
self.m.ReplayAll()
self.assertEqual(urlfetch.get(url), data)
@ -53,7 +61,7 @@ class UrlFetchTest(HeatTestCase):
def test_http_error(self):
url = 'http://example.com/template'
urllib2.urlopen(url).AndRaise(urllib2.URLError('fubar'))
requests.get(url).AndRaise(IOError('fubar'))
self.m.ReplayAll()
self.assertRaises(IOError, urlfetch.get, url)