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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 self.assertIsInstance(http, infra_libs.InstrumentedHttp) | 122 self.assertIsInstance(http, infra_libs.InstrumentedHttp) |
123 | 123 |
124 # Only test one malformed case and rely on LoadJsonCredentialsTest | 124 # Only test one malformed case and rely on LoadJsonCredentialsTest |
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): |
| 133 def setUp(self): |
| 134 super(RetriableHttplib2Test, self).setUp() |
| 135 self.http = infra_libs.RetriableHttp() |
| 136 self.http._request = mock.create_autospec(self.http._request, spec_set=True) |
| 137 |
| 138 _MOCK_REQUEST = mock.call(*([mock.ANY] * 9)) |
| 139 |
| 140 def test_succeed(self): |
| 141 self.http._request.return_value = ( |
| 142 httplib2.Response({'status': 400}), 'content') |
| 143 response, _ = self.http.request('http://foo/') |
| 144 self.assertEqual(400, response.status) |
| 145 self.http._request.assert_has_calls([ self._MOCK_REQUEST ]) |
| 146 |
| 147 def test_retry_succeed(self): |
| 148 self.http._request.side_effect = iter([ |
| 149 (httplib2.Response({'status': 500}), 'content'), |
| 150 httplib2.HttpLib2Error, |
| 151 (httplib2.Response({'status': 200}), 'content') |
| 152 ]) |
| 153 response, _ = self.http.request('http://foo/') |
| 154 self.assertEqual(200, response.status) |
| 155 self.http._request.assert_has_calls([ self._MOCK_REQUEST ] * 3) |
| 156 |
| 157 def test_fail_exception(self): |
| 158 self.http._request.side_effect = httplib2.HttpLib2Error() |
| 159 self.assertRaises(httplib2.HttpLib2Error, self.http.request, 'http://foo/') |
| 160 self.http._request.assert_has_calls([ self._MOCK_REQUEST ] * 5) |
| 161 |
| 162 def test_fail_status_code(self): |
| 163 self.http._request.return_value = ( |
| 164 httplib2.Response({'status': 500}), 'content') |
| 165 response, _ = self.http.request('http://foo/') |
| 166 self.assertEqual(500, response.status) |
| 167 self.http._request.assert_has_calls([ self._MOCK_REQUEST ] * 5) |
| 168 |
132 | 169 |
133 class InstrumentedHttplib2Test(unittest.TestCase): | 170 class InstrumentedHttplib2Test(unittest.TestCase): |
134 def setUp(self): | 171 def setUp(self): |
135 super(InstrumentedHttplib2Test, self).setUp() | 172 super(InstrumentedHttplib2Test, self).setUp() |
136 self.mock_time = mock.create_autospec(time.time, spec_set=True) | 173 self.mock_time = mock.create_autospec(time.time, spec_set=True) |
137 self.mock_time.return_value = 42 | 174 self.mock_time.return_value = 42 |
138 self.http = infra_libs.InstrumentedHttp('test', time_fn=self.mock_time) | 175 self.http = infra_libs.InstrumentedHttp('test', time_fn=self.mock_time) |
139 self.http._request = mock.Mock() | 176 self.http._request = mock.Mock() |
140 ts_mon.reset_for_unittest() | 177 ts_mon.reset_for_unittest() |
141 | 178 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 ('.*', {'status': 404}, '')]) | 326 ('.*', {'status': 404}, '')]) |
290 response, body = http.request('https://mywebserver.woo.hoo', 'GET') | 327 response, body = http.request('https://mywebserver.woo.hoo', 'GET') |
291 self.assertIsInstance(response, httplib2.Response) | 328 self.assertIsInstance(response, httplib2.Response) |
292 self.assertEqual(response.status, 404) | 329 self.assertEqual(response.status, 404) |
293 self.assertEqual(body, '') | 330 self.assertEqual(body, '') |
294 | 331 |
295 self.assertEqual(http.requests_made[0].uri, 'https://mywebserver.woo.hoo') | 332 self.assertEqual(http.requests_made[0].uri, 'https://mywebserver.woo.hoo') |
296 self.assertEqual(http.requests_made[0].method, 'GET') | 333 self.assertEqual(http.requests_made[0].method, 'GET') |
297 self.assertEqual(http.requests_made[0].body, None) | 334 self.assertEqual(http.requests_made[0].body, None) |
298 self.assertEqual(http.requests_made[0].headers, None) | 335 self.assertEqual(http.requests_made[0].headers, None) |
OLD | NEW |