Skip to content

Commit

Permalink
Fix mocking of HTTPClient.request() method.
Browse files Browse the repository at this point in the history
The test_extend_list_exceed_max_uri_len test case mocks
the request() method of HTTPClient class so that it raises
the RequestURITooLong exception. But the real method can't
possibly raise it (Client.do_request() does this instead).
Mocks should emulate the behavior of methods they stub out
and not change it in any way.

Fixes bug 1192197.

Change-Id: I62b2db111ef251f95eb9aa9c9cc00b53fdbccc68
  • Loading branch information
Roman Podolyaka committed Jun 18, 2013
1 parent cfd1e50 commit e7593e1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 8 additions & 4 deletions quantumclient/v2_0/client.py
Expand Up @@ -907,6 +907,12 @@ def _handle_fault_response(self, status_code, response_body):
# Raise the appropriate exception
exception_handler_v20(status_code, des_error_body)

def _check_uri_length(self, action):
uri_len = len(self.httpclient.endpoint_url) + len(action)
if uri_len > self.MAX_URI_LEN:
raise exceptions.RequestURITooLong(
excess=uri_len - self.MAX_URI_LEN)

def do_request(self, method, action, body=None, headers=None, params=None):
# Add format and tenant_id
action += ".%s" % self.format
Expand All @@ -916,10 +922,8 @@ def do_request(self, method, action, body=None, headers=None, params=None):
action += '?' + urllib.urlencode(params, doseq=1)
# Ensure client always has correct uri - do not guesstimate anything
self.httpclient.authenticate_and_fetch_endpoint_url()
uri_len = len(self.httpclient.endpoint_url) + len(action)
if uri_len > self.MAX_URI_LEN:
raise exceptions.RequestURITooLong(
excess=uri_len - self.MAX_URI_LEN)
self._check_uri_length(action)

if body:
body = self.serialize(body)
self.httpclient.content_type = self.content_type()
Expand Down
13 changes: 6 additions & 7 deletions tests/unit/test_cli20_network.py
Expand Up @@ -509,16 +509,15 @@ def test_extend_list_exceed_max_uri_len(self):
def mox_calls(path, data):
sub_data_lists = [data[:len(data) - 1], data[len(data) - 1:]]
filters, response = self._build_test_data(data)

# 1 char of extra URI len will cause a split in 2 requests
self.client.httpclient.request(
test_cli20.end_url(path, 'fields=id&fields=cidr%s' % filters),
'GET',
body=None,
headers=mox.ContainsKeyValue(
'X-Auth-Token', test_cli20.TOKEN)).AndRaise(
exceptions.RequestURITooLong(excess=1))
self.mox.StubOutWithMock(self.client, "_check_uri_length")
self.client._check_uri_length(mox.IgnoreArg()).AndRaise(
exceptions.RequestURITooLong(excess=1))

for data in sub_data_lists:
filters, response = self._build_test_data(data)
self.client._check_uri_length(mox.IgnoreArg()).AndReturn(None)
self.client.httpclient.request(
test_cli20.end_url(path,
'fields=id&fields=cidr%s' % filters),
Expand Down

0 comments on commit e7593e1

Please sign in to comment.