def processResponse(self, service, url, response): query = CacheEntryTimed.objects.filter(service=service, url=url) cache_entry = CacheEntryTimed() if len(query): cache_entry = query[0] if response.status == 304: if cache_entry is None: raise Exception("304, but no content??") response = MockHTTP() response.status = cache_entry.status response.data = cache_entry.content response.headers = cache_entry.headers return {"response": response} else: now = make_aware(datetime.now(), get_current_timezone()) cache_entry.service = service cache_entry.url = url cache_entry.status = response.status cache_entry.content = response.data cache_entry.headers = response.headers cache_entry.time_saved = now store_cache_entry(cache_entry) return
def putURL(self, url, headers, body): response = MockHTTP() if body is not None: if "If-Match" in headers: response.status = 200 # update else: response.status = 201 # create response.headers = {"X-Data-Source": "GWS file mock data", "Content-Type": headers["Content-Type"]} # insert response.data time values now = int(round(time() * 1000)) for t in ['createtime', 'modifytime', 'membermodifytime']: span = '<span class="%s">' % t if not re.search(r'%s' % span, body): body = re.sub(r'(<div class="group">)', r'\1\n %s%s</span>\n' % (span, now), body) response.data = body else: response.status = 400 response.data = "Bad Request: no POST body" return response
def putURL(self, url, headers, body): response = MockHTTP() if body is not None: if "If-Match" in headers: response.status = 200 # update else: response.status = 201 # create response.headers = { "X-Data-Source": "GWS file mock data", "Content-Type": headers["Content-Type"] } # insert response.data time values now = int(round(time() * 1000)) for t in ['createtime', 'modifytime', 'membermodifytime']: span = '<span class="%s">' % t if not re.search(r'%s' % span, body): body = re.sub(r'(<div class="group">)', r'\1\n %s%s</span>\n' % (span, now), body) response.data = body else: response.status = 400 response.data = "Bad Request: no POST body" return response
def test_saved_headers(self): with self.settings(RESTCLIENTS_SWS_DAO_CLASS='restclients.dao_implementation.sws.File', RESTCLIENTS_PWS_DAO_CLASS='restclients.dao_implementation.pws.File', RESTCLIENTS_DAO_CACHE_CLASS='restclients.cache_implementation.TimeSimpleCache'): cache = TimeSimpleCache() response = MockHTTP() response.status = 200 response.data = "Cache testing" response.headers = { "link": "next,http://somewhere", "Content-type": "text", "random": "stuff", "and": "more", } cache._process_response("cache_test", "/v1/headers", response) from_db = cache._response_from_cache("cache_test", "/v1/headers", {}, 10) self.assertEquals(from_db["response"].status, 200) self.assertEquals(from_db["response"].data, "Cache testing") self.assertEquals(from_db["response"].getheader("random"), "stuff") self.assertEquals(from_db["response"].getheader("and"), "more") self.assertEquals(from_db["response"].getheader("Content-type"), "text") self.assertEquals(from_db["response"].getheader("link"), "next,http://somewhere")
def putURL(self, url, headers, body): # For developing against crashes in grade submission if re.match('/student/v\d/graderoster/2013,spring,ZERROR,101,S1,', url): response = MockHTTP() response.data = "No employee found for ID 1234567890" response.status = 500 return response # Submitted too late, sad. if re.match('/student/v\d/graderoster/2013,spring,ZERROR,101,S2,', url): response = MockHTTP() response.data = "grading period not active for year/quarter" response.status = 404 return response response = MockHTTP() if body is not None: response.status = 200 response.headers = {"X-Data-Source": "SWS file mock data"} response.data = self._make_grade_roster_submitted(body) else: response.status = 400 response.data = "Bad Request: no PUT body" return response
def _response_from_cache(self, service, url, headers, max_age_in_seconds, max_error_age=60 * 5): # If max_age_in_seconds is 0, # make sure we don't get a hit from this same second. if not max_age_in_seconds: return None now = make_aware(datetime.now(), get_current_timezone()) time_limit = now - timedelta(seconds=max_age_in_seconds) query = CacheEntryTimed.objects.filter(service=service, url=url, time_saved__gte=time_limit) if len(query): hit = query[0] if hit.status != 200 and ( now - timedelta(seconds=max_error_age) > hit.time_saved): return None response = MockHTTP() response.status = hit.status response.data = hit.content response.headers = hit.getHeaders() return { "response": response, } return None
def post_mockdata_url(service_name, implementation_name, url, headers, body, dir_base=dirname(__file__)): """ :param service_name: possible "sws", "pws", "book", "hfs", etc. :param implementation_name: possible values: "file", etc. """ # Currently this post method does not return a response body response = MockHTTP() if body is not None: if "dispatch" in url: response.status = 200 else: response.status = 201 response.headers = { "X-Data-Source": service_name + " file mock data", "Content-Type": headers['Content-Type'] } else: response.status = 400 response.data = "Bad Request: no POST body" return response
def _response_from_cache(self, service, url, headers, max_age_in_seconds, max_error_age=60 * 5): # If max_age_in_seconds is 0, # make sure we don't get a hit from this same second. if not max_age_in_seconds: return None now = make_aware(datetime.now(), get_current_timezone()) time_limit = now - timedelta(seconds=max_age_in_seconds) query = CacheEntryTimed.objects.filter(service=service, url=url, time_saved__gte=time_limit) if len(query): hit = query[0] if hit.status != 200 and (now - timedelta(seconds=max_error_age) > hit.time_saved): return None response = MockHTTP() response.status = hit.status response.data = hit.content response.headers = hit.getHeaders() return { "response": response, } return None
def test_saved_headers(self): with self.settings(RESTCLIENTS_SWS_DAO_CLASS= 'restclients.dao_implementation.sws.File', RESTCLIENTS_PWS_DAO_CLASS= 'restclients.dao_implementation.pws.File', RESTCLIENTS_DAO_CACHE_CLASS= 'restclients.cache_implementation.TimeSimpleCache'): cache = TimeSimpleCache() response = MockHTTP() response.status = 200 response.data = "Cache testing" response.headers = { "link": "next,http://somewhere", "Content-type": "text", "random": "stuff", "and": "more", } cache._process_response("cache_test", "/v1/headers", response) from_db = cache._response_from_cache("cache_test", "/v1/headers", {}, 10) self.assertEquals(from_db["response"].status, 200) self.assertEquals(from_db["response"].data, "Cache testing") self.assertEquals(from_db["response"].getheader("random"), "stuff") self.assertEquals(from_db["response"].getheader("and"), "more") self.assertEquals(from_db["response"].getheader("Content-type"), "text") self.assertEquals(from_db["response"].getheader("link"), "next,http://somewhere")
def getURL(self, url, headers): if url in File._cache: response = MockHTTP() response.status = 200 response.data = File._cache[url] response.headers = {'Content-Type': 'application/json'} else: response = get_mockdata_url("irws", "file", url, headers) File._cache[url] = response.data return response
def getURL(self, url, headers): if "If-None-Match" in headers and url == "/same": response = MockHTTP() response.status = 304 return response else: response = MockHTTP() response.status = 200 response.data = "Body Content" response.headers = {"ETag": "A123BBB"} return response
def putURL(self, url, headers, body): response = MockHTTP() if body is not None: if "If-Match" in headers: response.status = 200 # update else: response.status = 201 # create response.headers = {"X-Data-Source": "GWS file mock data", "Content-Type": headers["Content-Type"]} response.data = body else: response.status = 400 response.data = "Bad Request: no POST body" return response
def put_mockdata_url(service_name, implementation_name, url, headers, body, dir_base = dirname(__file__)): """ :param service_name: possible "sws", "pws", "book", "hfs", etc. :param implementation_name: possible values: "file", etc. """ #Currently this put method does not return a response body response = MockHTTP() if body is not None: response.status = 204 response.headers = {"X-Data-Source": service_name + " file mock data", "Content-Type": headers['Content-Type']} else: response.status = 400 response.data = "Bad Request: no POST body" return response
def putURL(self, url, headers, body): response = MockHTTP() logger.debug('made it to putURL') response.headers = {"Content-Type": 'application/json'} if url in File._cache: cache = json.loads(File._cache[url]) request = json.loads(body) type = request.keys()[0] for attr in request[type][0].keys(): cache[type][0][attr] = request[type][0][attr] File._cache[url] = json.dumps(cache) response.data = File._cache[url] response.status = 200 else: response.data = body response.status = 404 return response
def getCache(self, service, url, headers): client = self._get_client() key = self._get_key(service, url) try: data = client.get(key) except bmemcached.exceptions.MemcachedException as ex: logger.warning("MemCached Err on get with key '%s' ==> '%s'", key, str(ex)) return if not data: return values = json.loads(data) response = MockHTTP() response.status = values["status"] response.data = values["data"] response.headers = values["headers"] return {"response": response}
def _load_resource_from_path(resource_dir, service_name, implementation_name, url, headers): RESOURCE_ROOT = os.path.join(resource_dir['path'], service_name, implementation_name) app = resource_dir['app'] 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 unquoted = unquote(orig_file_path) paths = [ convert_to_platform_safe(orig_file_path), "%s/index.html" % (convert_to_platform_safe(orig_file_path)), orig_file_path, "%s/index.html" % orig_file_path, convert_to_platform_safe(unquoted), "%s/index.html" % (convert_to_platform_safe(unquoted)), unquoted, "%s/index.html" % unquoted, ] file_path = None handle = None for path in paths: try: file_path = path handle = open(path) break except IOError as ex: pass if handle is None: return None logger = logging.getLogger(__name__) logger.debug("URL: %s; App: %s; File: %s" % (url, app, file_path)) response = MockHTTP() response.status = 200 response.data = handle.read() response.headers = {"X-Data-Source": service_name + " file mock data", } try: headers = open(handle.name + '.http-headers') file_values = json.loads(headers.read()) if "headers" in file_values: response.headers = dict(response.headers.items() + file_values['headers'].items()) if 'status' in file_values: response.status = file_values['status'] else: response.headers = dict(response.headers.items() + file_values.items()) except IOError: pass return response
def _load_resource_from_path(resource_dir, service_name, implementation_name, url, headers): RESOURCE_ROOT = os.path.join(resource_dir['path'], service_name, implementation_name) app = resource_dir['app'] 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 unquoted = unquote(orig_file_path) paths = [ convert_to_platform_safe(orig_file_path), "%s/index.html" % (convert_to_platform_safe(orig_file_path)), orig_file_path, "%s/index.html" % orig_file_path, convert_to_platform_safe(unquoted), "%s/index.html" % (convert_to_platform_safe(unquoted)), unquoted, "%s/index.html" % unquoted, ] file_path = None handle = None for path in paths: try: file_path = path handle = open(path) break except IOError as ex: pass if handle is None: return None logger = logging.getLogger(__name__) logger.debug("URL: %s; App: %s; File: %s" % (url, app, file_path)) response = MockHTTP() response.status = 200 response.data = handle.read() response.headers = { "X-Data-Source": service_name + " file mock data", } try: headers = open(handle.name + '.http-headers') file_values = json.loads(headers.read()) if "headers" in file_values: response.headers = dict(response.headers.items() + file_values['headers'].items()) if 'status' in file_values: response.status = file_values['status'] else: response.headers = dict(response.headers.items() + file_values.items()) except IOError: pass return response