From 6b0ff05010fb1fdcbccc22c085c34a82186279d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Abdelkader=20Mart=C3=ADnez=20P=C3=A9rez?= Date: Fri, 23 Aug 2019 15:18:25 +0200 Subject: [PATCH] Implement get route details. Closes #12 --- poc/bin/kapow | 22 +++++++++++++++++-- .../control/get/error_notfound.feature | 14 ++++++------ .../test/features/control/get/success.feature | 14 ++++++------ spec/test/features/steps/steps.py | 18 ++++++++++++++- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/poc/bin/kapow b/poc/bin/kapow index dc104b9..91bc60d 100755 --- a/poc/bin/kapow +++ b/poc/bin/kapow @@ -285,6 +285,23 @@ def get_routes(app): return _get_routes +def get_route(app): + async def _get_route(request): + """Return requested registered route.""" + id = request.match_info["id"] + for idx, r in enumerate(app["user_routes"]): + if r.id == id: + return web.json_response({"index": idx, + "method": r.method, + "id": r.id, + "url_pattern": r.path, + "entrypoint": r.entrypoint, + "command": r.command}) + else: + return web.Response(status=404, reason="Not Found") + return _get_route + + def insert_route(app): async def _insert_route(request): """Insert a new Kapow! route.""" @@ -468,8 +485,9 @@ async def start_kapow_server(bind, scripts, certfile=None, interactive=False, ke control_app.add_routes([ # Control API web.get('/routes', get_routes(user_app)), - web.post('/routes', append_route(user_app)), # TODO: return route info - web.put('/routes', insert_route(user_app)), # TODO: return route info + web.get('/routes/{id}', get_route(user_app)), + web.post('/routes', append_route(user_app)), + web.put('/routes', insert_route(user_app)), web.delete('/routes/{id}', delete_route(user_app)), # Data API diff --git a/spec/test/features/control/get/error_notfound.feature b/spec/test/features/control/get/error_notfound.feature index 5a2bea9..2a85b17 100644 --- a/spec/test/features/control/get/error_notfound.feature +++ b/spec/test/features/control/get/error_notfound.feature @@ -1,12 +1,12 @@ -Feature: Fail to retrieve a route info in Kapow! server. - When trying to get a route info in the server, if it - does no exists the server respons with an error. +Feature: Fail to retrieve route details in Kapow! server. + When trying to get route details for a route that + does no exist the server responds with an error. - Scenario: Try to get info for a non-existing route. - A request of retrieving a non-existing route info - will trigger a not found error. + Scenario: Try to get details for a nonexistent route. + A request for retrieving details for a nonexistent + route will trigger a not found error. Given I have a just started Kapow! server - When I get the info for route with id "xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx" + When I get the route with id "xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx" Then I get 404 as response code And I get "Not Found" as response reason phrase diff --git a/spec/test/features/control/get/success.feature b/spec/test/features/control/get/success.feature index d71def9..0e3ebe2 100644 --- a/spec/test/features/control/get/success.feature +++ b/spec/test/features/control/get/success.feature @@ -1,15 +1,15 @@ -Feature: Retrieve route info in Kapow! server. - Users can retrieve route info from the server by - specifying its id. +Feature: Retrieve route details in Kapow! server. + Users can retrieve route details from the server + by specifying its id. - Scenario: Retrieve route info. - Get route info by spscifying its id. + Scenario: Retrieve route details. + Get route details by id. - Given I have a Kapow! server whith the following routes: + Given I have a Kapow! server with the following routes: | method | url_pattern | entrypoint | command | | GET | /listRootDir | /bin/sh -c | ls -la / \| response /body | | GET | /listDir/{dirname} | /bin/sh -c | ls -la /request/params/dirname \| response /body | - When I get the first route info + When I get the first route Then I get 200 as response code And I get "OK" as response reason phrase And I get the following response body: diff --git a/spec/test/features/steps/steps.py b/spec/test/features/steps/steps.py index a8d8371..a163a1a 100644 --- a/spec/test/features/steps/steps.py +++ b/spec/test/features/steps/steps.py @@ -12,6 +12,10 @@ import jsonexample import logging + +WORD2POS = {"first": 0, "second": 1, "last": -1} + + class Env(EnvironConfig): #: How to run Kapow! server KAPOW_SERVER_CMD = StringVar(default="kapow server") @@ -137,7 +141,7 @@ def step_impl(context): @when('I delete the {order} route') def step_impl(context, order): - idx = {"first": 0, "second": 1, "last": -1}.get(order) + idx = WORD2POS.get(order) routes = requests.get(f"{Env.KAPOW_CONTROLAPI_URL}/routes") id = routes.json()[idx]["id"] context.response = requests.delete(f"{Env.KAPOW_CONTROLAPI_URL}/routes/{id}") @@ -149,3 +153,15 @@ def step_impl(context): f"{Env.KAPOW_CONTROLAPI_URL}/routes", headers={"Content-Type": "application/json"}, data=context.text) + +@when('I get the route with id "{id}"') +def step_impl(context, id): + context.response = requests.get(f"{Env.KAPOW_CONTROLAPI_URL}/routes/{id}") + + +@when('I get the {order} route') +def step_impl(context, order): + idx = WORD2POS.get(order) + routes = requests.get(f"{Env.KAPOW_CONTROLAPI_URL}/routes") + id = routes.json()[idx]["id"] + context.response = requests.get(f"{Env.KAPOW_CONTROLAPI_URL}/routes/{id}")