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:
@@ -16,7 +16,6 @@
|
|||||||
import copy
|
import copy
|
||||||
import errno
|
import errno
|
||||||
import hashlib
|
import hashlib
|
||||||
import httplib
|
|
||||||
import logging
|
import logging
|
||||||
import posixpath
|
import posixpath
|
||||||
import socket
|
import socket
|
||||||
@@ -24,6 +23,8 @@ import StringIO
|
|||||||
import struct
|
import struct
|
||||||
import urlparse
|
import urlparse
|
||||||
|
|
||||||
|
from six.moves import http_client
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -52,7 +53,7 @@ try:
|
|||||||
else:
|
else:
|
||||||
raise ImportError
|
raise ImportError
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from httplib import HTTPSConnection
|
HTTPSConnection = http_client.HTTPSConnection
|
||||||
from OpenSSL.SSL import Connection as Connection
|
from OpenSSL.SSL import Connection as Connection
|
||||||
|
|
||||||
|
|
||||||
@@ -91,7 +92,7 @@ class HTTPClient(object):
|
|||||||
if scheme == 'https':
|
if scheme == 'https':
|
||||||
return VerifiedHTTPSConnection
|
return VerifiedHTTPSConnection
|
||||||
else:
|
else:
|
||||||
return httplib.HTTPConnection
|
return http_client.HTTPConnection
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_connection_kwargs(scheme, **kwargs):
|
def get_connection_kwargs(scheme, **kwargs):
|
||||||
@@ -111,7 +112,7 @@ class HTTPClient(object):
|
|||||||
try:
|
try:
|
||||||
return _class(self.endpoint_hostname, self.endpoint_port,
|
return _class(self.endpoint_hostname, self.endpoint_port,
|
||||||
**self.connection_kwargs)
|
**self.connection_kwargs)
|
||||||
except httplib.InvalidURL:
|
except http_client.InvalidURL:
|
||||||
raise exc.InvalidEndpoint()
|
raise exc.InvalidEndpoint()
|
||||||
|
|
||||||
def log_curl_request(self, method, url, kwargs):
|
def log_curl_request(self, method, url, kwargs):
|
||||||
|
@@ -14,7 +14,6 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import errno
|
import errno
|
||||||
import httplib
|
|
||||||
import socket
|
import socket
|
||||||
import StringIO
|
import StringIO
|
||||||
import urlparse
|
import urlparse
|
||||||
@@ -25,6 +24,7 @@ import testtools
|
|||||||
from glanceclient import exc
|
from glanceclient import exc
|
||||||
import glanceclient
|
import glanceclient
|
||||||
from glanceclient.common import http
|
from glanceclient.common import http
|
||||||
|
from six.moves import http_client
|
||||||
from tests import utils
|
from tests import utils
|
||||||
|
|
||||||
|
|
||||||
@@ -33,8 +33,8 @@ class TestClient(testtools.TestCase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestClient, self).setUp()
|
super(TestClient, self).setUp()
|
||||||
self.mock = mox.Mox()
|
self.mock = mox.Mox()
|
||||||
self.mock.StubOutWithMock(httplib.HTTPConnection, 'request')
|
self.mock.StubOutWithMock(http_client.HTTPConnection, 'request')
|
||||||
self.mock.StubOutWithMock(httplib.HTTPConnection, 'getresponse')
|
self.mock.StubOutWithMock(http_client.HTTPConnection, 'getresponse')
|
||||||
|
|
||||||
self.endpoint = 'http://example.com:9292'
|
self.endpoint = 'http://example.com:9292'
|
||||||
self.client = http.HTTPClient(self.endpoint, token=u'abc123')
|
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
|
And the error should list the host and port that refused the
|
||||||
connection
|
connection
|
||||||
"""
|
"""
|
||||||
httplib.HTTPConnection.request(
|
http_client.HTTPConnection.request(
|
||||||
mox.IgnoreArg(),
|
mox.IgnoreArg(),
|
||||||
mox.IgnoreArg(),
|
mox.IgnoreArg(),
|
||||||
headers=mox.IgnoreArg(),
|
headers=mox.IgnoreArg(),
|
||||||
@@ -103,29 +103,29 @@ class TestClient(testtools.TestCase):
|
|||||||
def test_request_redirected(self):
|
def test_request_redirected(self):
|
||||||
resp = utils.FakeResponse({'location': 'http://www.example.com'},
|
resp = utils.FakeResponse({'location': 'http://www.example.com'},
|
||||||
status=302, body=StringIO.StringIO())
|
status=302, body=StringIO.StringIO())
|
||||||
httplib.HTTPConnection.request(
|
http_client.HTTPConnection.request(
|
||||||
mox.IgnoreArg(),
|
mox.IgnoreArg(),
|
||||||
mox.IgnoreArg(),
|
mox.IgnoreArg(),
|
||||||
headers=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
|
# The second request should be to the redirected location
|
||||||
expected_response = 'Ok'
|
expected_response = 'Ok'
|
||||||
resp2 = utils.FakeResponse({}, StringIO.StringIO(expected_response))
|
resp2 = utils.FakeResponse({}, StringIO.StringIO(expected_response))
|
||||||
httplib.HTTPConnection.request(
|
http_client.HTTPConnection.request(
|
||||||
'GET',
|
'GET',
|
||||||
'http://www.example.com',
|
'http://www.example.com',
|
||||||
headers=mox.IgnoreArg(),
|
headers=mox.IgnoreArg(),
|
||||||
)
|
)
|
||||||
httplib.HTTPConnection.getresponse().AndReturn(resp2)
|
http_client.HTTPConnection.getresponse().AndReturn(resp2)
|
||||||
|
|
||||||
self.mock.ReplayAll()
|
self.mock.ReplayAll()
|
||||||
|
|
||||||
self.client.json_request('GET', '/v1/images/detail')
|
self.client.json_request('GET', '/v1/images/detail')
|
||||||
|
|
||||||
def test_http_encoding(self):
|
def test_http_encoding(self):
|
||||||
httplib.HTTPConnection.request(
|
http_client.HTTPConnection.request(
|
||||||
mox.IgnoreArg(),
|
mox.IgnoreArg(),
|
||||||
mox.IgnoreArg(),
|
mox.IgnoreArg(),
|
||||||
headers=mox.IgnoreArg())
|
headers=mox.IgnoreArg())
|
||||||
@@ -134,7 +134,7 @@ class TestClient(testtools.TestCase):
|
|||||||
# returned by httplib
|
# returned by httplib
|
||||||
expected_response = 'Ok'
|
expected_response = 'Ok'
|
||||||
fake = utils.FakeResponse({}, StringIO.StringIO(expected_response))
|
fake = utils.FakeResponse({}, StringIO.StringIO(expected_response))
|
||||||
httplib.HTTPConnection.getresponse().AndReturn(fake)
|
http_client.HTTPConnection.getresponse().AndReturn(fake)
|
||||||
self.mock.ReplayAll()
|
self.mock.ReplayAll()
|
||||||
|
|
||||||
headers = {"test": u'ni\xf1o'}
|
headers = {"test": u'ni\xf1o'}
|
||||||
@@ -155,14 +155,14 @@ class TestClient(testtools.TestCase):
|
|||||||
# NOTE(kmcdonald): See bug #1179984 for more details.
|
# NOTE(kmcdonald): See bug #1179984 for more details.
|
||||||
self.assertEqual(path, '/v1/images/detail')
|
self.assertEqual(path, '/v1/images/detail')
|
||||||
|
|
||||||
httplib.HTTPConnection.request(
|
http_client.HTTPConnection.request(
|
||||||
mox.IgnoreArg(),
|
mox.IgnoreArg(),
|
||||||
mox.IgnoreArg(),
|
mox.IgnoreArg(),
|
||||||
headers=mox.IgnoreArg()).WithSideEffects(check_request)
|
headers=mox.IgnoreArg()).WithSideEffects(check_request)
|
||||||
|
|
||||||
# fake the response returned by httplib
|
# fake the response returned by httplib
|
||||||
fake = utils.FakeResponse({}, StringIO.StringIO('Ok'))
|
fake = utils.FakeResponse({}, StringIO.StringIO('Ok'))
|
||||||
httplib.HTTPConnection.getresponse().AndReturn(fake)
|
http_client.HTTPConnection.getresponse().AndReturn(fake)
|
||||||
self.mock.ReplayAll()
|
self.mock.ReplayAll()
|
||||||
|
|
||||||
resp, body = self.client.raw_request('GET', '/v1/images/detail')
|
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')
|
client = http.HTTPClient(endpoint, token=u'abc123')
|
||||||
self.assertEqual(client.endpoint_path, '/customized-path')
|
self.assertEqual(client.endpoint_path, '/customized-path')
|
||||||
|
|
||||||
httplib.HTTPConnection.request(
|
http_client.HTTPConnection.request(
|
||||||
mox.IgnoreArg(),
|
mox.IgnoreArg(),
|
||||||
mox.IgnoreArg(),
|
mox.IgnoreArg(),
|
||||||
headers=mox.IgnoreArg()).WithSideEffects(check_request)
|
headers=mox.IgnoreArg()).WithSideEffects(check_request)
|
||||||
|
|
||||||
# fake the response returned by httplib
|
# fake the response returned by httplib
|
||||||
fake = utils.FakeResponse({}, StringIO.StringIO('Ok'))
|
fake = utils.FakeResponse({}, StringIO.StringIO('Ok'))
|
||||||
httplib.HTTPConnection.getresponse().AndReturn(fake)
|
http_client.HTTPConnection.getresponse().AndReturn(fake)
|
||||||
self.mock.ReplayAll()
|
self.mock.ReplayAll()
|
||||||
|
|
||||||
resp, body = client.raw_request('GET', '/v1/images/detail')
|
resp, body = client.raw_request('GET', '/v1/images/detail')
|
||||||
@@ -204,7 +204,7 @@ class TestClient(testtools.TestCase):
|
|||||||
"""
|
"""
|
||||||
endpoint = 'http://example.com:9292'
|
endpoint = 'http://example.com:9292'
|
||||||
client = http.HTTPClient(endpoint, token=u'abc123')
|
client = http.HTTPClient(endpoint, token=u'abc123')
|
||||||
httplib.HTTPConnection.request(mox.IgnoreArg(), mox.IgnoreArg(),
|
http_client.HTTPConnection.request(mox.IgnoreArg(), mox.IgnoreArg(),
|
||||||
headers=mox.IgnoreArg()
|
headers=mox.IgnoreArg()
|
||||||
).AndRaise(socket.error())
|
).AndRaise(socket.error())
|
||||||
self.mock.ReplayAll()
|
self.mock.ReplayAll()
|
||||||
|
Reference in New Issue
Block a user