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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 | 155 |
| 156 def test_delegate_set_attr(self): | 156 def test_delegate_set_attr(self): |
| 157 """RetriableHttp should delegate setting attributes to Http""" | 157 """RetriableHttp should delegate setting attributes to Http""" |
| 158 self.http.ignore_etag = False | 158 self.http.ignore_etag = False |
| 159 self.assertFalse(self.http.ignore_etag) | 159 self.assertFalse(self.http.ignore_etag) |
| 160 self.assertFalse(self.http._http.ignore_etag) | 160 self.assertFalse(self.http._http.ignore_etag) |
| 161 self.http.ignore_etag = True | 161 self.http.ignore_etag = True |
| 162 self.assertTrue(self.http.ignore_etag) | 162 self.assertTrue(self.http.ignore_etag) |
| 163 self.assertTrue(self.http._http.ignore_etag) | 163 self.assertTrue(self.http._http.ignore_etag) |
| 164 | 164 |
| 165 def test_succeed(self): | 165 @mock.patch('time.sleep', autospec=True) |
|
Sergey Berezin
2016/07/14 21:10:17
nit: in this case it's likely not as relevant, but
| |
| 166 def test_succeed(self, _sleep): | |
| 166 self.http._http.request.return_value = ( | 167 self.http._http.request.return_value = ( |
| 167 httplib2.Response({'status': 400}), 'content') | 168 httplib2.Response({'status': 400}), 'content') |
| 168 response, _ = self.http.request('http://foo/') | 169 response, _ = self.http.request('http://foo/') |
| 169 self.assertEqual(400, response.status) | 170 self.assertEqual(400, response.status) |
| 170 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ]) | 171 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ]) |
| 171 | 172 |
| 172 def test_retry_succeed(self): | 173 @mock.patch('time.sleep', autospec=True) |
| 174 def test_retry_succeed(self, _sleep): | |
| 173 self.http._http.request.side_effect = iter([ | 175 self.http._http.request.side_effect = iter([ |
| 174 (httplib2.Response({'status': 500}), 'content'), | 176 (httplib2.Response({'status': 500}), 'content'), |
| 175 httplib2.HttpLib2Error, | 177 httplib2.HttpLib2Error, |
| 176 (httplib2.Response({'status': 200}), 'content') | 178 (httplib2.Response({'status': 200}), 'content') |
| 177 ]) | 179 ]) |
| 178 response, _ = self.http.request('http://foo/') | 180 response, _ = self.http.request('http://foo/') |
| 179 self.assertEqual(200, response.status) | 181 self.assertEqual(200, response.status) |
| 180 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ] * 3) | 182 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ] * 3) |
| 181 | 183 |
| 182 def test_fail_exception(self): | 184 @mock.patch('time.sleep', autospec=True) |
| 185 def test_fail_exception(self, _sleep): | |
| 183 self.http._http.request.side_effect = httplib2.HttpLib2Error() | 186 self.http._http.request.side_effect = httplib2.HttpLib2Error() |
| 184 self.assertRaises(httplib2.HttpLib2Error, self.http.request, 'http://foo/') | 187 self.assertRaises(httplib2.HttpLib2Error, self.http.request, 'http://foo/') |
| 185 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ] * 5) | 188 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ] * 5) |
| 186 | 189 |
| 187 def test_fail_status_code(self): | 190 @mock.patch('time.sleep', autospec=True) |
| 191 def test_fail_status_code(self, _sleep): | |
| 188 self.http._http.request.return_value = ( | 192 self.http._http.request.return_value = ( |
| 189 httplib2.Response({'status': 500}), 'content') | 193 httplib2.Response({'status': 500}), 'content') |
| 190 response, _ = self.http.request('http://foo/') | 194 response, _ = self.http.request('http://foo/') |
| 191 self.assertEqual(500, response.status) | 195 self.assertEqual(500, response.status) |
| 192 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ] * 5) | 196 self.http._http.request.assert_has_calls([ self._MOCK_REQUEST ] * 5) |
| 193 | 197 |
| 194 | 198 |
| 195 class InstrumentedHttplib2Test(unittest.TestCase): | 199 class InstrumentedHttplib2Test(unittest.TestCase): |
| 196 def setUp(self): | 200 def setUp(self): |
| 197 super(InstrumentedHttplib2Test, self).setUp() | 201 super(InstrumentedHttplib2Test, self).setUp() |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 ('.*', {'status': 404}, '')]) | 355 ('.*', {'status': 404}, '')]) |
| 352 response, body = http.request('https://mywebserver.woo.hoo', 'GET') | 356 response, body = http.request('https://mywebserver.woo.hoo', 'GET') |
| 353 self.assertIsInstance(response, httplib2.Response) | 357 self.assertIsInstance(response, httplib2.Response) |
| 354 self.assertEqual(response.status, 404) | 358 self.assertEqual(response.status, 404) |
| 355 self.assertEqual(body, '') | 359 self.assertEqual(body, '') |
| 356 | 360 |
| 357 self.assertEqual(http.requests_made[0].uri, 'https://mywebserver.woo.hoo') | 361 self.assertEqual(http.requests_made[0].uri, 'https://mywebserver.woo.hoo') |
| 358 self.assertEqual(http.requests_made[0].method, 'GET') | 362 self.assertEqual(http.requests_made[0].method, 'GET') |
| 359 self.assertEqual(http.requests_made[0].body, None) | 363 self.assertEqual(http.requests_made[0].body, None) |
| 360 self.assertEqual(http.requests_made[0].headers, None) | 364 self.assertEqual(http.requests_made[0].headers, None) |
| OLD | NEW |