Cleanup and black

This commit is contained in:
taoufik07
2019-02-17 19:43:47 +01:00
parent a0dd7481ec
commit 1ec567cabf
9 changed files with 58 additions and 39 deletions
+1
View File
@@ -10,3 +10,4 @@ install:
# command to run the dependencies
script:
- "pytest"
- "black responder tests setup.py --check"
+3 -6
View File
@@ -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)
+1 -1
View File
@@ -40,4 +40,4 @@ def cli():
prop = "api"
app = __import__(module)
getattr(app, prop).run()
getattr(app, prop).run()
+11 -5
View File
@@ -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:
+1 -1
View File
@@ -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
+7 -7
View File
@@ -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,
}
+2 -4
View File
@@ -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):
+7 -6
View File
@@ -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
+25 -9
View File
@@ -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"),
],