diff --git a/.travis.yml b/.travis.yml index 1331ca5..71805cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,3 +10,4 @@ install: # command to run the dependencies script: - "pytest" + - "black responder tests setup.py --check" \ No newline at end of file diff --git a/responder/api.py b/responder/api.py index 94f1b71..e42a4ee 100644 --- a/responder/api.py +++ b/responder/api.py @@ -251,9 +251,7 @@ class API: cont = True except Exception: self.background( - self.default_response, - websocket=route.uses_websocket, - error=True + self.default_response, websocket=route.uses_websocket, error=True ) raise @@ -587,7 +585,6 @@ class API: def static_url(asset): return f"{self.static_route}/{asset}" - # return asset return template.render( document=document, @@ -652,6 +649,6 @@ class API: spawn() def run(self, **kwargs): - if 'debug' not in kwargs: - kwargs.update({'debug': self.debug}) + if "debug" not in kwargs: + kwargs.update({"debug": self.debug}) self.serve(**kwargs) diff --git a/responder/cli.py b/responder/cli.py index bb4cdbe..8c5548b 100644 --- a/responder/cli.py +++ b/responder/cli.py @@ -40,4 +40,4 @@ def cli(): prop = "api" app = __import__(module) - getattr(app, prop).run() \ No newline at end of file + getattr(app, prop).run() diff --git a/responder/models.py b/responder/models.py index 4dfe8ff..b2f2ef6 100644 --- a/responder/models.py +++ b/responder/models.py @@ -89,9 +89,16 @@ class QueryDict(dict): yield from super().items() -# TODO: add slots class Request: - __slots__ = ["_starlette", "formats", "_headers", "_encoding", "api", "_content", "_cookies"] + __slots__ = [ + "_starlette", + "formats", + "_headers", + "_encoding", + "api", + "_content", + "_cookies", + ] def __init__(self, scope, receive, api=None): self._starlette = StarletteRequest(scope, receive) @@ -263,7 +270,7 @@ class Response: {} ) #: A Python dictionary of ``{key: value}``, representing the headers of the response. self.formats = formats - self.cookies = SimpleCookie() #: The cookies set in the Response, as a dictionary + self.cookies = SimpleCookie() #: The cookies set in the Response self.session = ( req.session.copy() ) #: The cookie-based session data, in dict form, to add to the Response. @@ -295,7 +302,7 @@ class Response: domain=None, max_age=None, secure=False, - httponly=True + httponly=True, ): self.cookies[key] = value morsel = self.cookies[key] @@ -317,7 +324,6 @@ class Response: ) starlette_response.raw_headers.extend(cookie_header) - async def __call__(self, receive, send): body, headers = await self.body if self.headers: diff --git a/responder/routes.py b/responder/routes.py index eb77071..490c2f5 100644 --- a/responder/routes.py +++ b/responder/routes.py @@ -55,7 +55,7 @@ class Route: def _weight(self): params = set(self._param_pattern.findall(self.route)) params_count = len(params) - w = len(self.route.rsplit('}', 1)[-1].strip('/')) + w = len(self.route.rsplit("}", 1)[-1].strip("/")) return params_count != 0, w == 0, -params_count @property diff --git a/responder/statics.py b/responder/statics.py index ff5c461..c10acfa 100644 --- a/responder/statics.py +++ b/responder/statics.py @@ -4,11 +4,11 @@ DEFAULT_SESSION_COOKIE = "Responder-Session" DEFAULT_SECRET_KEY = "NOTASECRET" DEFAULT_CORS_PARAMS = { - "allow_origins": (), - "allow_methods": ("GET",), - "allow_headers": (), - "allow_credentials": False, - "allow_origin_regex": None, - "expose_headers": (), - "max_age": 600, + "allow_origins": (), + "allow_methods": ("GET",), + "allow_headers": (), + "allow_credentials": False, + "allow_origin_regex": None, + "expose_headers": (), + "max_age": 600, } diff --git a/tests/conftest.py b/tests/conftest.py index c7aabf9..547a388 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,10 +18,7 @@ def current_dir(): @pytest.fixture def api(): - return responder.API( - debug=False, - allowed_hosts=[";"] - ) + return responder.API(debug=False, allowed_hosts=[";"]) @pytest.fixture @@ -49,6 +46,7 @@ def flask(): return app + @pytest.fixture def schema(): class Query(graphene.ObjectType): diff --git a/tests/test_responder.py b/tests/test_responder.py index c1e02a9..e195d5f 100644 --- a/tests/test_responder.py +++ b/tests/test_responder.py @@ -429,7 +429,7 @@ def test_cookies(api): path="/", max_age=123, secure=False, - httponly=True + httponly=True, ) r = api.requests.get(api.url_for(cookies), cookies={"hello": "universe"}) @@ -438,7 +438,7 @@ def test_cookies(api): assert "hello" in r.cookies r = api.requests.get(api.url_for(cookies)) - assert r.json() == {'cookies': {'hello': 'world', 'sent': 'true'}} + assert r.json() == {"cookies": {"hello": "world", "sent": "true"}} @pytest.mark.xfail @@ -449,11 +449,11 @@ def test_sessions(api): resp.media = resp.session r = api.requests.get(api.url_for(view)) - assert "Responder-Session" in r.cookies + assert api.session_cookie in r.cookies r = api.requests.get(api.url_for(view)) assert ( - r.cookies["Responder-Session"] + r.cookies[api.session_cookie] == '{"hello": "world"}.r3EB04hEEyLYIJaAXCEq3d4YEbs' ) assert r.json() == {"hello": "world"} @@ -489,8 +489,9 @@ def test_500(api): def view(req, resp): raise ValueError - dumb_client = responder.api.TestClient(api, base_url="http://;", - raise_server_exceptions=False) + dumb_client = responder.api.TestClient( + api, base_url="http://;", raise_server_exceptions=False + ) r = dumb_client.get(api.url_for(view)) assert not r.ok assert r.status_code == responder.status_codes.HTTP_500 diff --git a/tests/test_routes.py b/tests/test_routes.py index e1b79f4..02230d1 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -89,25 +89,41 @@ def test_does_match_with_route(route, match, expected): [ pytest.param("/{greetings}", (True, True, -1), id="with one param"), pytest.param( - "/{greetings}.{name}", (True, True, -2), id="with 2 params and dot in the middle" + "/{greetings}.{name}", + (True, True, -2), + id="with 2 params and dot in the middle", ), - pytest.param("/{greetings}/{name}", (True, True, -2), id="with 2 params and subpath"), pytest.param( - "/{greetings}/{name}/{hello}", (True, True, -3), id="with 3 params and subpath" + "/{greetings}/{name}", (True, True, -2), id="with 2 params and subpath" + ), + pytest.param( + "/{greetings}/{name}/{hello}", + (True, True, -3), + id="with 3 params and subpath", ), pytest.param( "/{greetings}_{name}", (True, True, -2), id="with 2 params and underscore" ), pytest.param("/{greetings}/test", (True, False, -1), id="with one param"), pytest.param( - "/{greetings}.{name}/test", (True, False, -2), id="with 2 params and dot in the middle" - ), - pytest.param("/{greetings}/{name}/test", (True, False, -2), id="with 2 params and subpath"), - pytest.param( - "/{greetings}/{name}/{hello}/test", (True, False, -3), id="with 3 params and subpath" + "/{greetings}.{name}/test", + (True, False, -2), + id="with 2 params and dot in the middle", ), pytest.param( - "/{greetings}_{name}/test", (True, False, -2), id="with 2 params and underscore" + "/{greetings}/{name}/test", + (True, False, -2), + id="with 2 params and subpath", + ), + pytest.param( + "/{greetings}/{name}/{hello}/test", + (True, False, -3), + id="with 3 params and subpath", + ), + pytest.param( + "/{greetings}_{name}/test", + (True, False, -2), + id="with 2 params and underscore", ), pytest.param("/hello", (False, False, 0), id="without params"), ],