def test_cache_expiration(self): with self.settings(RESTCLIENTS_DAO_CACHE_CLASS=CACHE): cache = BridgeAccountCache() ok_response = MockHTTP() ok_response.status = 200 ok_response.data = "xx" url = '/group/uw_member/effective_member' response = cache.getCache('gws', url, {}) self.assertEquals(response, None) cache.processResponse("gws", url, ok_response) response = cache.getCache("gws", url, {}) self.assertEquals(response["response"].data, 'xx') cache_entry = CacheEntryTimed.objects.get(service='gws', url=url) orig_time_saved = cache_entry.time_saved cache_entry.time_saved = (orig_time_saved - timedelta(minutes=(60) - 2)) cache_entry.save() response = cache.getCache("gws", url, {}) self.assertNotEquals(response, None) cache_entry.time_saved = (orig_time_saved - timedelta(minutes=(60) + 1)) cache_entry.save() response = cache.getCache("gws", url, {}) self.assertEquals(response, None)
def get_response(obj, service, url): problems = PerformanceDegradation.get_problems() if not problems: return delay = problems.get_load_time(service) if delay: time.sleep(float(delay)) status = problems.get_status(service) content = problems.get_content(service) if content and not status: status = 200 if status: response = MockHTTP() response.status = int(status) if content: response.data = content return response return None
def load_resource_from_path(resource_dir, service_name, implementation_name, url, headers): RESOURCE_ROOT = os.path.join(resource_dir, service_name, implementation_name) if url == "///": # Just a placeholder to put everything else in an else. # If there are things that need dynamic work, they'd go here pass else: orig_file_path = RESOURCE_ROOT + url handle = open_file(orig_file_path) header_handle = open_file(orig_file_path + ".http-headers") # attempt to open query permutations even on success # so that if there are multiple files we throw an exception if "?" in url: handle = attempt_open_query_permutations(url, orig_file_path, False) if "?" in url: header_handle = attempt_open_query_permutations( url, orig_file_path, True) if handle is None and header_handle is None: return None if handle is not None: data = handle.read() response = MockHTTP() response.status = 200 if handle is not None: response.data = data response.headers = { "X-Data-Source": service_name + " file mock data", } if header_handle is not None: try: data = header_handle.read() file_values = json.loads(data) if "headers" in file_values: response.headers.update(file_values['headers']) if 'status' in file_values: response.status = file_values['status'] else: response.headers.update(file_values) except UnicodeDecodeError: pass except IOError: pass return response
def process_response(self, headers, data): response = MockHTTP() response.status = int(headers['status']) response.data = data response.headers = headers return response
def load(self, method, url, headers, body): response = MockHTTP() response.status = 200 response.data = "ok - %s" % method return response