From e7593e1a8401de7b8d9e3dc3f67533cb2455132f Mon Sep 17 00:00:00 2001 From: Roman Podolyaka Date: Tue, 18 Jun 2013 17:47:13 +0300 Subject: [PATCH] Fix mocking of HTTPClient.request() method. 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 --- quantumclient/v2_0/client.py | 12 ++++++++---- tests/unit/test_cli20_network.py | 13 ++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/quantumclient/v2_0/client.py b/quantumclient/v2_0/client.py index 9303ca967..15b0193b2 100644 --- a/quantumclient/v2_0/client.py +++ b/quantumclient/v2_0/client.py @@ -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 @@ -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() diff --git a/tests/unit/test_cli20_network.py b/tests/unit/test_cli20_network.py index a64740455..7df54ffbf 100644 --- a/tests/unit/test_cli20_network.py +++ b/tests/unit/test_cli20_network.py @@ -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),