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
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
# 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()
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)
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)
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)