| OLD | NEW |
| (Empty) |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import os | |
| 6 | |
| 7 from testing_utils import testing | |
| 8 | |
| 9 from lib import time_util | |
| 10 from libs.http import retry_http_client | |
| 11 | |
| 12 | |
| 13 class MockHttpClient(retry_http_client.RetryHttpClient): # pragma: no cover. | |
| 14 | |
| 15 def __init__(self, response_for_url=None): | |
| 16 super(MockHttpClient, self).__init__() | |
| 17 if response_for_url is None: | |
| 18 self.response_for_url = {} | |
| 19 | |
| 20 def SetResponseForUrl(self, url, response): | |
| 21 self.response_for_url[url] = response | |
| 22 | |
| 23 def GetBackoff(self, *_): | |
| 24 """Override to avoid sleep.""" | |
| 25 return 0 | |
| 26 | |
| 27 def _Get(self, url, *_): | |
| 28 response = self.response_for_url.get(url) | |
| 29 if response is None: | |
| 30 return 404, 'Not Found' | |
| 31 else: | |
| 32 return 200, response | |
| 33 | |
| 34 def _Post(self, *_): | |
| 35 pass | |
| 36 | |
| 37 def _Put(self, *_): | |
| 38 pass | |
| 39 | |
| 40 | |
| 41 class TestCase(testing.AppengineTestCase): # pragma: no cover. | |
| 42 # Setup the customized queues. | |
| 43 taskqueue_stub_root_path = os.path.join( | |
| 44 os.path.dirname(__file__), os.path.pardir) | |
| 45 | |
| 46 def MockPipeline( | |
| 47 self, pipeline_class, result, expected_args, expected_kwargs=None): | |
| 48 """Mocks a pipeline to return a value and asserts the expected parameters. | |
| 49 | |
| 50 Args: | |
| 51 pipeline_class (class): The class of the pipeline to be mocked. | |
| 52 result (object): The result to be returned by the pipeline. | |
| 53 expected_args (list): The list of positional parameters expected by the | |
| 54 pipeline. | |
| 55 expected_kwargs (dict): The dict of key-value parameters expected by the | |
| 56 pipeline. Default is None. | |
| 57 """ | |
| 58 expected_kwargs = expected_kwargs or {} | |
| 59 | |
| 60 def Mocked_run(_, *args, **kwargs): | |
| 61 self.assertEqual(list(args), expected_args) | |
| 62 self.assertEqual(kwargs, expected_kwargs) | |
| 63 return result | |
| 64 | |
| 65 self.mock(pipeline_class, 'run', Mocked_run) | |
| 66 | |
| 67 def MockUTCNow(self, mocked_utcnow): | |
| 68 """Mocks utcnow with the given value for testing.""" | |
| 69 self.mock(time_util, 'GetUTCNow', lambda: mocked_utcnow) | |
| 70 | |
| 71 def MockUTCNowWithTimezone(self, mocked_utcnow): | |
| 72 """Mocks utcnow with the given value for testing.""" | |
| 73 self.mock(time_util, 'GetUTCNowWithTimezone', lambda: mocked_utcnow) | |
| 74 | |
| 75 def GetMockHttpClient(self, response_for_url=None): | |
| 76 """Return mocked http client class.""" | |
| 77 return MockHttpClient(response_for_url) | |
| OLD | NEW |