Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(215)

Side by Side Diff: infra_libs/test/httplib2_utils_test.py

Issue 2114073002: Add HttpsMonitor (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Add retryable and other things Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 RetryableHttplib2Test(unittest.TestCase):
133 def setUp(self):
134 super(RetryableHttplib2Test, self).setUp()
135 self.http = infra_libs.RetryableHttp(httplib2.Http(), max_tries=5,
136 retrying_statuses=[500])
137 self.http.http_obj.request = mock.Mock()
Sergey Berezin 2016/07/06 20:13:29 Please try to use mock.create_autospec(..., spec_s
tnn 2016/07/06 22:05:18 Done.
138
139 def test_succeed(self):
140 self.http.http_obj.request.return_value = (
141 httplib2.Response({'status': 400}), 'content')
142 response, _ = self.http.request('http://foo/')
143 self.assertEqual(400, response.status)
144 self.http.http_obj.request.assert_called_once_with('http://foo/',
145 'GET', None)
146
147 def test_retry_succeed(self):
148 self.http.http_obj.request.side_effect = [
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.http_obj.request.assert_has_calls(
156 [ mock.call('http://foo/', 'GET', None) ] * 3)
157
158 def test_fail_exception(self):
159 self.http.http_obj.request.side_effect = httplib2.HttpLib2Error
160 self.assertRaises(httplib2.HttpLib2Error, self.http.request, 'http://foo/')
161 self.http.http_obj.request.assert_has_calls(
162 [ mock.call('http://foo/', 'GET', None) ] * 5)
163
164 def test_fail_status_code(self):
165 self.http.http_obj.request.return_value = (
166 httplib2.Response({'status': 500}), 'content')
167 response, _ = self.http.request('http://foo/')
168 self.assertEqual(500, response.status)
169 self.http.http_obj.request.assert_has_calls(
170 [ mock.call('http://foo/', 'GET', None) ] * 5)
171
132 172
133 class InstrumentedHttplib2Test(unittest.TestCase): 173 class InstrumentedHttplib2Test(unittest.TestCase):
134 def setUp(self): 174 def setUp(self):
135 super(InstrumentedHttplib2Test, self).setUp() 175 super(InstrumentedHttplib2Test, self).setUp()
136 self.mock_time = mock.create_autospec(time.time, spec_set=True) 176 self.mock_time = mock.create_autospec(time.time, spec_set=True)
137 self.mock_time.return_value = 42 177 self.mock_time.return_value = 42
138 self.http = infra_libs.InstrumentedHttp('test', time_fn=self.mock_time) 178 self.http = infra_libs.InstrumentedHttp('test', time_fn=self.mock_time)
139 self.http._request = mock.Mock() 179 self.http._request = mock.Mock()
140 ts_mon.reset_for_unittest() 180 ts_mon.reset_for_unittest()
141 181
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 ('.*', {'status': 404}, '')]) 329 ('.*', {'status': 404}, '')])
290 response, body = http.request('https://mywebserver.woo.hoo', 'GET') 330 response, body = http.request('https://mywebserver.woo.hoo', 'GET')
291 self.assertIsInstance(response, httplib2.Response) 331 self.assertIsInstance(response, httplib2.Response)
292 self.assertEqual(response.status, 404) 332 self.assertEqual(response.status, 404)
293 self.assertEqual(body, '') 333 self.assertEqual(body, '')
294 334
295 self.assertEqual(http.requests_made[0].uri, 'https://mywebserver.woo.hoo') 335 self.assertEqual(http.requests_made[0].uri, 'https://mywebserver.woo.hoo')
296 self.assertEqual(http.requests_made[0].method, 'GET') 336 self.assertEqual(http.requests_made[0].method, 'GET')
297 self.assertEqual(http.requests_made[0].body, None) 337 self.assertEqual(http.requests_made[0].body, None)
298 self.assertEqual(http.requests_made[0].headers, None) 338 self.assertEqual(http.requests_made[0].headers, None)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698