Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import os | 5 import os |
| 6 import socket | 6 import socket |
| 7 import time | 7 import time |
| 8 import unittest | 8 import unittest |
| 9 | 9 |
| 10 import infra_libs | 10 import infra_libs |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 # for the other cases. | 125 # for the other cases. |
| 126 def test_malformed_credentials(self): | 126 def test_malformed_credentials(self): |
| 127 with self.assertRaises(infra_libs.AuthError): | 127 with self.assertRaises(infra_libs.AuthError): |
| 128 infra_libs.get_authenticated_http( | 128 infra_libs.get_authenticated_http( |
| 129 'creds_malformed.json', | 129 'creds_malformed.json', |
| 130 service_accounts_creds_root=DATA_DIR) | 130 service_accounts_creds_root=DATA_DIR) |
| 131 | 131 |
| 132 class RetriableHttplib2Test(unittest.TestCase): | 132 class RetriableHttplib2Test(unittest.TestCase): |
| 133 def setUp(self): | 133 def setUp(self): |
| 134 super(RetriableHttplib2Test, self).setUp() | 134 super(RetriableHttplib2Test, self).setUp() |
| 135 self.http = infra_libs.RetriableHttp() | 135 self.http = infra_libs.RetriableHttp(httplib2.Http()) |
| 136 self.http._request = mock.create_autospec(self.http._request, spec_set=True) | 136 self.http._http.request = mock.create_autospec(self.http._http.request, |
| 137 spec_set=True) | |
| 137 | 138 |
| 138 _MOCK_REQUEST = mock.call(*([mock.ANY] * 9)) | 139 _MOCK_REQUEST = mock.call('http://foo/', 'GET', None) |
| 140 | |
| 141 def test_delegate_get_attr(self): | |
| 142 """RetriableHttp should delegate getting attribute except request() to | |
| 143 Http""" | |
| 144 self.http._http.clear_credentials = mock.create_autospec( | |
| 145 self.http._http.clear_credentials, spec_set=True) | |
| 146 self.http.clear_credentials() | |
| 147 self.http._http.clear_credentials.assert_called_once_with() | |
| 148 | |
| 149 def test_delegate_set_attr(self): | |
| 150 """RetriableHttp should delegate setting attributes to Http""" | |
| 151 self.http.ignore_etag = False | |
| 152 self.assertFalse(self.http.ignore_etag) | |
| 153 self.assertFalse(self.http._http.ignore_etag) | |
| 154 self.http.ignore_etag = True | |
| 155 # TODO(tanin): fix delegating set attribute | |
|
Sergey Berezin
2016/07/07 22:28:53
Let's fix it in this CL.
| |
| 156 self.assertFalse(self.http._http.ignore_etag) | |
| 139 | 157 |
| 140 def test_succeed(self): | 158 def test_succeed(self): |
| 141 self.http._request.return_value = ( | 159 self.http._http.request.return_value = ( |
| 142 httplib2.Response({'status': 400}), 'content') | 160 httplib2.Response({'status': 400}), 'content') |
| 143 response, _ = self.http.request('http://foo/') | 161 response, _ = self.http.request('http://foo/') |
| 144 self.assertEqual(400, response.status) | 162 self.assertEqual(400, response.status) |
| 145 self.http._request.assert_has_calls([ self._MOCK_REQUEST ]) | 163 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ]) |
| 146 | 164 |
| 147 def test_retry_succeed(self): | 165 def test_retry_succeed(self): |
| 148 self.http._request.side_effect = iter([ | 166 self.http._http.request.side_effect = iter([ |
| 149 (httplib2.Response({'status': 500}), 'content'), | 167 (httplib2.Response({'status': 500}), 'content'), |
| 150 httplib2.HttpLib2Error, | 168 httplib2.HttpLib2Error, |
| 151 (httplib2.Response({'status': 200}), 'content') | 169 (httplib2.Response({'status': 200}), 'content') |
| 152 ]) | 170 ]) |
| 153 response, _ = self.http.request('http://foo/') | 171 response, _ = self.http.request('http://foo/') |
| 154 self.assertEqual(200, response.status) | 172 self.assertEqual(200, response.status) |
| 155 self.http._request.assert_has_calls([ self._MOCK_REQUEST ] * 3) | 173 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ] * 3) |
| 156 | 174 |
| 157 def test_fail_exception(self): | 175 def test_fail_exception(self): |
| 158 self.http._request.side_effect = httplib2.HttpLib2Error() | 176 self.http._http.request.side_effect = httplib2.HttpLib2Error() |
| 159 self.assertRaises(httplib2.HttpLib2Error, self.http.request, 'http://foo/') | 177 self.assertRaises(httplib2.HttpLib2Error, self.http.request, 'http://foo/') |
| 160 self.http._request.assert_has_calls([ self._MOCK_REQUEST ] * 5) | 178 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ] * 5) |
| 161 | 179 |
| 162 def test_fail_status_code(self): | 180 def test_fail_status_code(self): |
| 163 self.http._request.return_value = ( | 181 self.http._http.request.return_value = ( |
| 164 httplib2.Response({'status': 500}), 'content') | 182 httplib2.Response({'status': 500}), 'content') |
| 165 response, _ = self.http.request('http://foo/') | 183 response, _ = self.http.request('http://foo/') |
| 166 self.assertEqual(500, response.status) | 184 self.assertEqual(500, response.status) |
| 167 self.http._request.assert_has_calls([ self._MOCK_REQUEST ] * 5) | 185 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ] * 5) |
| 168 | 186 |
| 169 | 187 |
| 170 class InstrumentedHttplib2Test(unittest.TestCase): | 188 class InstrumentedHttplib2Test(unittest.TestCase): |
| 171 def setUp(self): | 189 def setUp(self): |
| 172 super(InstrumentedHttplib2Test, self).setUp() | 190 super(InstrumentedHttplib2Test, self).setUp() |
| 173 self.mock_time = mock.create_autospec(time.time, spec_set=True) | 191 self.mock_time = mock.create_autospec(time.time, spec_set=True) |
| 174 self.mock_time.return_value = 42 | 192 self.mock_time.return_value = 42 |
| 175 self.http = infra_libs.InstrumentedHttp('test', time_fn=self.mock_time) | 193 self.http = infra_libs.InstrumentedHttp('test', time_fn=self.mock_time) |
| 176 self.http._request = mock.Mock() | 194 self.http._request = mock.Mock() |
| 177 ts_mon.reset_for_unittest() | 195 ts_mon.reset_for_unittest() |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 326 ('.*', {'status': 404}, '')]) | 344 ('.*', {'status': 404}, '')]) |
| 327 response, body = http.request('https://mywebserver.woo.hoo', 'GET') | 345 response, body = http.request('https://mywebserver.woo.hoo', 'GET') |
| 328 self.assertIsInstance(response, httplib2.Response) | 346 self.assertIsInstance(response, httplib2.Response) |
| 329 self.assertEqual(response.status, 404) | 347 self.assertEqual(response.status, 404) |
| 330 self.assertEqual(body, '') | 348 self.assertEqual(body, '') |
| 331 | 349 |
| 332 self.assertEqual(http.requests_made[0].uri, 'https://mywebserver.woo.hoo') | 350 self.assertEqual(http.requests_made[0].uri, 'https://mywebserver.woo.hoo') |
| 333 self.assertEqual(http.requests_made[0].method, 'GET') | 351 self.assertEqual(http.requests_made[0].method, 'GET') |
| 334 self.assertEqual(http.requests_made[0].body, None) | 352 self.assertEqual(http.requests_made[0].body, None) |
| 335 self.assertEqual(http.requests_made[0].headers, None) | 353 self.assertEqual(http.requests_made[0].headers, None) |
| OLD | NEW |