From 50e177e3c9c207ff832aac6adcafcf81a181a145 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Tue, 23 Jul 2013 22:08:20 -0400 Subject: [PATCH] Replace urllib2 with requests in urlfetch module simple replacement of urllib2.urlopen with requests.get Fixes LP# 1201534 Change-Id: Ia0a2ed3b092e22246339a42f432086dd05f788fa --- heat/common/urlfetch.py | 7 +++---- heat/tests/test_urlfetch.py | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/heat/common/urlfetch.py b/heat/common/urlfetch.py index bde5f7568..05ef8bb2c 100644 --- a/heat/common/urlfetch.py +++ b/heat/common/urlfetch.py @@ -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 diff --git a/heat/tests/test_urlfetch.py b/heat/tests/test_urlfetch.py index 7d794ef8f..7cf29533b 100644 --- a/heat/tests/test_urlfetch.py +++ b/heat/tests/test_urlfetch.py @@ -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)