2012-09-12 09:40:04 -04:00
|
|
|
# Copyright 2012 OpenStack LLC.
|
|
|
|
# All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
|
|
|
import httplib
|
|
|
|
import socket
|
2012-09-11 16:22:56 -07:00
|
|
|
import StringIO
|
2012-12-24 22:37:01 -06:00
|
|
|
import testtools
|
2012-09-12 09:40:04 -04:00
|
|
|
|
|
|
|
import mox
|
|
|
|
|
|
|
|
from glanceclient import exc
|
|
|
|
from glanceclient.common import http
|
2012-09-11 16:22:56 -07:00
|
|
|
from tests import utils
|
2012-09-12 09:40:04 -04:00
|
|
|
|
|
|
|
|
2012-12-24 22:37:01 -06:00
|
|
|
class TestClient(testtools.TestCase):
|
2012-09-12 09:40:04 -04:00
|
|
|
def test_connection_refused(self):
|
|
|
|
"""
|
|
|
|
Should receive a CommunicationError if connection refused.
|
|
|
|
And the error should list the host and port that refused the
|
|
|
|
connection
|
|
|
|
"""
|
|
|
|
endpoint = 'http://example.com:9292'
|
|
|
|
client = http.HTTPClient(endpoint, token=u'abc123')
|
|
|
|
m = mox.Mox()
|
|
|
|
m.StubOutWithMock(httplib.HTTPConnection, 'request')
|
|
|
|
httplib.HTTPConnection.request(
|
2012-11-21 12:03:07 -06:00
|
|
|
mox.IgnoreArg(),
|
|
|
|
mox.IgnoreArg(),
|
|
|
|
headers=mox.IgnoreArg(),
|
|
|
|
).AndRaise(socket.error())
|
2012-09-12 09:40:04 -04:00
|
|
|
m.ReplayAll()
|
|
|
|
try:
|
|
|
|
client.json_request('GET', '/v1/images/detail?limit=20')
|
|
|
|
#NOTE(alaski) We expect exc.CommunicationError to be raised
|
|
|
|
# so we should never reach this point. try/except is used here
|
|
|
|
# rather than assertRaises() so that we can check the body of
|
|
|
|
# the exception.
|
|
|
|
self.fail('An exception should have bypassed this line.')
|
|
|
|
except exc.CommunicationError, comm_err:
|
|
|
|
fail_msg = ("Exception message '%s' should contain '%s'" %
|
2012-11-21 12:03:07 -06:00
|
|
|
(comm_err.message, endpoint))
|
2012-09-12 09:40:04 -04:00
|
|
|
self.assertTrue(endpoint in comm_err.message, fail_msg)
|
|
|
|
finally:
|
|
|
|
m.UnsetStubs()
|
2012-09-11 16:22:56 -07:00
|
|
|
|
|
|
|
|
2012-12-24 22:37:01 -06:00
|
|
|
class TestResponseBodyIterator(testtools.TestCase):
|
2012-09-11 16:22:56 -07:00
|
|
|
def test_iter_default_chunk_size_64k(self):
|
|
|
|
resp = utils.FakeResponse({}, StringIO.StringIO('X' * 98304))
|
|
|
|
iterator = http.ResponseBodyIterator(resp)
|
|
|
|
chunks = list(iterator)
|
|
|
|
self.assertEqual(chunks, ['X' * 65536, 'X' * 32768])
|