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