diff --git a/spec/test/features/data/request/success.feature b/spec/test/features/data/request/success.feature index dd46dd4..1e60e60 100644 --- a/spec/test/features/data/request/success.feature +++ b/spec/test/features/data/request/success.feature @@ -11,12 +11,13 @@ Feature: Retrieve request resources from a handler in Kapow! server. Given I have a Kapow! server with the following testing routes: | method | url_pattern | | GET | /listRootDir/{path} | - When I send a request to the testing route "/listRootDir/otro" adding: - | fieldType | name | value | - | parameter | "par1" | "paramVal1" | - | header | "head1" | "headVal1" | - | cookie | "cook1" | "cookieVal1" | - And I get the resource + When I send a request to the testing route "/listRootDir/matchVal1" adding: + | fieldType | name | value | + | parameter | par1 | paramVal1 | + | header | head1 | headVal1 | + | cookie | cook1 | cookieVal1 | + | body | | bodyVal1 | + And I get the resource "" Then I get 200 as response code And I get "OK" as response reason phrase And I get the following response raw body: @@ -25,12 +26,12 @@ Feature: Retrieve request resources from a handler in Kapow! server. """ Examples: - | resourcePath | value | - | resource/method | "GET" | - | resource/path | "/listRootDir/otro" | - | resource/host | "localhost:8080" | - | resource/matches/path | "otro" | - | resource/params/par1 | "paramVal1" | - | resource/headers/head1 | "headVal1" | - | resource/cookies/cook1 | "cookieVal1" | - | resource/body | empty | + | resourcePath | value | + | /request/method | GET | + | /request/path | /listRootDir/matchVal1 | + | /request/host | localhost:8080 | + | /request/matches/path | matchVal1 | + | /request/params/par1 | paramVal1 | + | /request/headers/head1 | headVal1 | + | /request/cookies/cook1 | cookieVal1 | + | /request/body | bodyVal1 | diff --git a/spec/test/features/steps/steps.py b/spec/test/features/steps/steps.py index 859847d..55422de 100644 --- a/spec/test/features/steps/steps.py +++ b/spec/test/features/steps/steps.py @@ -31,7 +31,7 @@ class Env(EnvironConfig): KAPOW_CONTROLAPI_URL = StringVar(default="http://localhost:8081") #: Where the Data API is - KAPOW_DATAAPI_URL = StringVar(default="http://localhost:8080") + KAPOW_DATAAPI_URL = StringVar(default="http://localhost:8081") #: Where the User Interface is KAPOW_USER_URL = StringVar(default="http://localhost:8080") @@ -124,12 +124,10 @@ def step_impl(context): **{h: row[h] for h in row.headings}}) response.raise_for_status() - -@when('I send a request to the testing route "{path}"') -def step_impl(context, path): +def testing_request(context, request_fn): # Run the request in background def _testing_request(): - context.testing_response = requests.get(f"{Env.KAPOW_USER_URL}{path}") + context.testing_response = request_fn() context.testing_request = threading.Thread(target=_testing_request) context.testing_request.start() @@ -137,7 +135,18 @@ def step_impl(context, path): # handler_id with open(context.handler_fifo_path, 'r') as fifo: (context.testing_handler_pid, - context.testing_handler_id) = fifo.readline().split(';') + context.testing_handler_id) = fifo.readline().rstrip('\n').split(';') + + +@when('I send a request to the testing route "{path}"') +def step_impl(context, path): + def _request(): + try: + return requests.get(f"{Env.KAPOW_USER_URL}{path}", stream=False) + except: + return None + + testing_request(context, _request) @when('I release the testing request') @@ -168,6 +177,11 @@ def step_impl(context): assert is_subset(jsonexample.loads(context.text), context.response.json()) +@then('I get the following response raw body') +def step_impl(context): + assert context.text == context.response.text, f"{context.text!r} != {context.response.text!r}" + + @when('I delete the route with id "{id}"') def step_impl(context, id): context.response = requests.delete(f"{Env.KAPOW_CONTROLAPI_URL}/routes/{id}") @@ -214,3 +228,45 @@ def step_impl(context, 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}") + + +@when('I get the resource "{resource}"') +def step_impl(context, resource): + context.response = requests.get( + f"{Env.KAPOW_DATAAPI_URL}/handlers/{context.testing_handler_id}{resource}") + + +@when('I set the resource "{resource}" with value "{value}"') +def step_impl(context, resource, value): + context.response = requests.put( + f"{Env.KAPOW_DATAAPI_URL}/handlers/{context.testing_handler_id}{resource}", + data=value.encode("utf-8")) + + +@when('I send a request to the testing route "{path}" adding') +def step_impl(context, path): + if not hasattr(context, 'table'): + raise RuntimeError("A table must be set for this step.") + + params = { + "headers": dict(), + "cookies": dict(), + "params": dict()} + setters = { + "header": params["headers"].setdefault, + "cookie": params["cookies"].setdefault, + "body": lambda _, v: params.setdefault("data", v.encode("utf-8")), + "parameter": params["params"].setdefault,} + + for row in context.table: + setters[row["fieldType"]](row["name"], row["value"]) + + def _request(): + try: + return requests.get(f"{Env.KAPOW_USER_URL}{path}", + stream=False, + **params) + except: + return None + + testing_request(context, _request)