From 7a93c16deca463c6ca33b05172f4abf08a4cdbba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Abdelkader=20Mart=C3=ADnez=20P=C3=A9rez?= Date: Wed, 21 Aug 2019 08:52:13 +0200 Subject: [PATCH] Implement JSON validation in append. --- poc/bin/kapow | 7 ++++++- spec/test/features/steps/steps.py | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/poc/bin/kapow b/poc/bin/kapow index bd3c16f..fa85b16 100755 --- a/poc/bin/kapow +++ b/poc/bin/kapow @@ -279,7 +279,12 @@ def append_route(app): async def _append_route(request): """Create a new Kapow! route.""" app.router._frozen = False - content = await request.json() + + try: + content = await request.json() + except Exception as exc: + return web.Response(status=400, reason="Malformed JSON") + name = "ROUTE_" + str(uuid4()).replace('-', '_') try: app.router.add_route(content["method"], diff --git a/spec/test/features/steps/steps.py b/spec/test/features/steps/steps.py index 191d927..eaf73c9 100644 --- a/spec/test/features/steps/steps.py +++ b/spec/test/features/steps/steps.py @@ -170,4 +170,7 @@ def step_impl(context): @when('I try to append with this malformed JSON document') @when('I try to append with this JSON document') def step_impl(context): - raise NotImplementedError('STEP: When I try to append with this JSON document') + context.response = requests.post( + f"{Env.KAPOW_CONTROLAPI_URL}/routes", + headers={"Content-Type": "application/json"}, + data=context.text)