class Webapp(object): def __init__(self, app): super(Webapp, self).__init__() self.app = app self.loopback = FlaskLoopback(self.app) self.hostname = str(uuid4()) self.url = URL('http://{0}'.format(self.hostname)) def activate(self): self.loopback.activate_address((self.hostname, 80)) def deactivate(self): self.loopback.deactivate_address((self.hostname, 80)) def _request(self, method, path, *args, **kwargs): raw_response = kwargs.pop("raw_response", False) if path.startswith("/"): path = path[1:] assert not path.startswith("/") returned = requests.request( method, "http://{0}/{1}".format(self.hostname, path), *args, **kwargs) if raw_response: return returned returned.raise_for_status() return returned.json()
class App(object): def __init__(self, flask_app): super(App, self).__init__() self.flask_app = flask_app self.loopback = FlaskLoopback(self.flask_app) self.hostname = str(uuid1()) self.url = URLObject("http://{0}".format(self.hostname)) def activate(self): self.loopback.activate_address((self.hostname, 80)) def deactivate(self): self.loopback.deactivate_address((self.hostname, 80)) def get_page(self, page_size, page, path=None): if path is None: path = "objects" response = requests.get(self.url.add_path(path).set_query_param("page", str(page)).set_query_param("page_size", str(page_size))) response.raise_for_status() data = response.json() assert data["metadata"]["page"] == page assert data["metadata"]["page_size"] == page_size return data["result"] def get_all_paged(self, page_size, path=None): return list(itertools.chain.from_iterable(self.get_page(page_size, page, path=path) for page in range(1, int(self.num_objects / page_size) + 5)))
class App(object): def __init__(self, flask_app): super(App, self).__init__() self.flask_app = flask_app self.loopback = FlaskLoopback(self.flask_app) self.hostname = str(uuid1()) self.url = URLObject("http://{0}".format(self.hostname)) def activate(self): self.loopback.activate_address((self.hostname, 80)) def deactivate(self): self.loopback.deactivate_address((self.hostname, 80)) def get_page(self, page_size, page, path=None): if path is None: path = "objects" response = requests.get( self.url.add_path(path).set_query_param("page", str(page)).set_query_param( "page_size", str(page_size))) response.raise_for_status() data = response.json() assert data["metadata"]["page"] == page assert data["metadata"]["page_size"] == page_size return data["result"] def get_all_paged(self, page_size, path=None): return list( itertools.chain.from_iterable( self.get_page(page_size, page, path=path) for page in range(1, int(self.num_objects / page_size) + 5)))
class Webapp(object): def __init__(self): super(Webapp, self).__init__() self.hostname = str(uuid.uuid1()) def __enter__(self): self.activate() return self def __exit__(self, *args): self.deactivate() def activate(self): self.app = build_app(use_cached=True) self.app.config["SECRET_KEY"] = "testing_key" self.app.config["TESTING"] = True self.loopback = FlaskLoopback(self.app) self.loopback.activate_address((self.hostname, 80)) def deactivate(self): self.loopback.deactivate_address((self.hostname, 80)) def request(self, method, path, *args, **kwargs): """Performs a request against the server app All arguments are similar to requests' ``request``, except for a few exceptions listed below :keyword raw_response: if ``True``, returns the raw response object and avoids checking for success :keyword data_json: if provided, sends the value provided serialized to JSON, and sets the content type to be json accordingly """ if not isinstance(path, str): path = str(path) raw_response = kwargs.pop("raw_response", False) data_json = kwargs.pop('data_json', NOTHING) if data_json is not NOTHING: if 'data' in kwargs: raise RuntimeError('`data` is not supported when combined with `data_json`') kwargs['data'] = json.dumps(data_json) headers = kwargs['headers'] = dict(kwargs.get('headers', ())) if not any(k.lower() == 'content-type' for k in headers): headers['Content-type'] = 'application/json' if path.startswith("/"): path = path[1:] assert not path.startswith("/") returned = requests.request(method, f"http://{self.hostname}/{path}", *args, **kwargs) if raw_response: return returned returned.raise_for_status() return returned.json() def get(self, *args, **kwargs): """Shortcut for self.request('get', ...) """ return self.request("get", *args, **kwargs) def post(self, *args, **kwargs): """Shortcut for self.request('post', ...) """ return self.request("post", *args, **kwargs) def delete(self, *args, **kwargs): """Shortcut for self.request('delete', ...) """ return self.request("delete", *args, **kwargs) def put(self, *args, **kwargs): """Shortcut for self.request('put', ...) """ return self.request("put", *args, **kwargs)