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