python3: use six.moves for httplib imports

This adds six to the requirements.txt file in order to make
some HTTP-related imports work across Python 2's httplib and Python 3's
http.client modules. Tests were updated, including one change to the
location of HTTPConnection - moving it from being accessed where it was
imported rather than its canonical location inside of
six.moves.http_client.

Change-Id: Ibc4932b37dfdf195cd5091066914513af1876955
Signed-off-by: Chuck Short <chuck.short@canonical.com>
This commit is contained in:
Chuck Short 2013-10-15 14:47:30 -04:00
parent cd11833cff
commit 518cb2508d
2 changed files with 22 additions and 21 deletions

View File

@ -16,7 +16,6 @@
import copy
import errno
import hashlib
import httplib
import logging
import posixpath
import socket
@ -24,6 +23,8 @@ import StringIO
import struct
import urlparse
from six.moves import http_client
try:
import json
except ImportError:
@ -52,7 +53,7 @@ try:
else:
raise ImportError
except ImportError:
from httplib import HTTPSConnection
HTTPSConnection = http_client.HTTPSConnection
from OpenSSL.SSL import Connection as Connection
@ -91,7 +92,7 @@ class HTTPClient(object):
if scheme == 'https':
return VerifiedHTTPSConnection
else:
return httplib.HTTPConnection
return http_client.HTTPConnection
@staticmethod
def get_connection_kwargs(scheme, **kwargs):
@ -111,7 +112,7 @@ class HTTPClient(object):
try:
return _class(self.endpoint_hostname, self.endpoint_port,
**self.connection_kwargs)
except httplib.InvalidURL:
except http_client.InvalidURL:
raise exc.InvalidEndpoint()
def log_curl_request(self, method, url, kwargs):

View File

@ -14,7 +14,6 @@
# under the License.
import errno
import httplib
import socket
import StringIO
import urlparse
@ -25,6 +24,7 @@ import testtools
from glanceclient import exc
import glanceclient
from glanceclient.common import http
from six.moves import http_client
from tests import utils
@ -33,8 +33,8 @@ class TestClient(testtools.TestCase):
def setUp(self):
super(TestClient, self).setUp()
self.mock = mox.Mox()
self.mock.StubOutWithMock(httplib.HTTPConnection, 'request')
self.mock.StubOutWithMock(httplib.HTTPConnection, 'getresponse')
self.mock.StubOutWithMock(http_client.HTTPConnection, 'request')
self.mock.StubOutWithMock(http_client.HTTPConnection, 'getresponse')
self.endpoint = 'http://example.com:9292'
self.client = http.HTTPClient(self.endpoint, token=u'abc123')
@ -82,7 +82,7 @@ class TestClient(testtools.TestCase):
And the error should list the host and port that refused the
connection
"""
httplib.HTTPConnection.request(
http_client.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
headers=mox.IgnoreArg(),
@ -103,29 +103,29 @@ class TestClient(testtools.TestCase):
def test_request_redirected(self):
resp = utils.FakeResponse({'location': 'http://www.example.com'},
status=302, body=StringIO.StringIO())
httplib.HTTPConnection.request(
http_client.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
headers=mox.IgnoreArg(),
)
httplib.HTTPConnection.getresponse().AndReturn(resp)
http_client.HTTPConnection.getresponse().AndReturn(resp)
# The second request should be to the redirected location
expected_response = 'Ok'
resp2 = utils.FakeResponse({}, StringIO.StringIO(expected_response))
httplib.HTTPConnection.request(
http_client.HTTPConnection.request(
'GET',
'http://www.example.com',
headers=mox.IgnoreArg(),
)
httplib.HTTPConnection.getresponse().AndReturn(resp2)
http_client.HTTPConnection.getresponse().AndReturn(resp2)
self.mock.ReplayAll()
self.client.json_request('GET', '/v1/images/detail')
def test_http_encoding(self):
httplib.HTTPConnection.request(
http_client.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
headers=mox.IgnoreArg())
@ -134,7 +134,7 @@ class TestClient(testtools.TestCase):
# returned by httplib
expected_response = 'Ok'
fake = utils.FakeResponse({}, StringIO.StringIO(expected_response))
httplib.HTTPConnection.getresponse().AndReturn(fake)
http_client.HTTPConnection.getresponse().AndReturn(fake)
self.mock.ReplayAll()
headers = {"test": u'ni\xf1o'}
@ -155,14 +155,14 @@ class TestClient(testtools.TestCase):
# NOTE(kmcdonald): See bug #1179984 for more details.
self.assertEqual(path, '/v1/images/detail')
httplib.HTTPConnection.request(
http_client.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
headers=mox.IgnoreArg()).WithSideEffects(check_request)
# fake the response returned by httplib
fake = utils.FakeResponse({}, StringIO.StringIO('Ok'))
httplib.HTTPConnection.getresponse().AndReturn(fake)
http_client.HTTPConnection.getresponse().AndReturn(fake)
self.mock.ReplayAll()
resp, body = self.client.raw_request('GET', '/v1/images/detail')
@ -183,14 +183,14 @@ class TestClient(testtools.TestCase):
client = http.HTTPClient(endpoint, token=u'abc123')
self.assertEqual(client.endpoint_path, '/customized-path')
httplib.HTTPConnection.request(
http_client.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
headers=mox.IgnoreArg()).WithSideEffects(check_request)
# fake the response returned by httplib
fake = utils.FakeResponse({}, StringIO.StringIO('Ok'))
httplib.HTTPConnection.getresponse().AndReturn(fake)
http_client.HTTPConnection.getresponse().AndReturn(fake)
self.mock.ReplayAll()
resp, body = client.raw_request('GET', '/v1/images/detail')
@ -204,9 +204,9 @@ class TestClient(testtools.TestCase):
"""
endpoint = 'http://example.com:9292'
client = http.HTTPClient(endpoint, token=u'abc123')
httplib.HTTPConnection.request(mox.IgnoreArg(), mox.IgnoreArg(),
headers=mox.IgnoreArg()
).AndRaise(socket.error())
http_client.HTTPConnection.request(mox.IgnoreArg(), mox.IgnoreArg(),
headers=mox.IgnoreArg()
).AndRaise(socket.error())
self.mock.ReplayAll()
try:
client.raw_request('GET', '/v1/images/detail?limit=20')