From 4bfc44efbd8412c25d6b643a855d46a20321d8c2 Mon Sep 17 00:00:00 2001 From: Kushal Das Date: Mon, 30 Mar 2020 16:33:05 +0530 Subject: [PATCH] Fixes#68 uses incoming timeout value from JSON Now we are using the incoming timeout value from the JSON input. --- securedrop_proxy/main.py | 3 +++ tests/test_main.py | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/securedrop_proxy/main.py b/securedrop_proxy/main.py index 69abf48d7..ff979053a 100644 --- a/securedrop_proxy/main.py +++ b/securedrop_proxy/main.py @@ -42,4 +42,7 @@ def __main__(incoming: str, p: Proxy) -> None: p.req = req + if "timeout" in client_req: + p.timeout = client_req["timeout"] + p.proxy() diff --git a/tests/test_main.py b/tests/test_main.py index 19bc4e5a8..05f158623 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -55,6 +55,48 @@ def on_done(self): for item in json.loads(response["body"]): self.assertEqual(item["userId"], 1) + @vcr.use_cassette("fixtures/main_json_response_with_timeout.yaml") + def test_json_response_with_timeout(self): + test_input_json = """{ "method": "GET", + "path_query": "/posts?userId=1", + "timeout": 40.0 }""" + + req = proxy.Req() + req.method = "GET" + req.path_query = "" + req.headers = {"Accept": "application/json"} + + # Use custom callbacks + def on_save(self, fh, res): + pass + + def on_done(self): + assert self.res.status == http.HTTPStatus.OK + print(json.dumps(self.res.__dict__)) + + self.p = proxy.Proxy(self.conf_path, req) + + # Patching on_save and on_done + + self.p.on_done = types.MethodType(on_done, self.p) + self.p.on_save = types.MethodType(on_save, self.p) + + saved_stdout = sys.stdout + try: + out = StringIO() + sys.stdout = out + main.__main__(test_input_json, self.p) + output = out.getvalue().strip() + finally: + sys.stdout = saved_stdout + + # Test that the right timeout was set in proxy object + assert self.p.timeout == 40.0 + + response = json.loads(output) + for item in json.loads(response["body"]): + self.assertEqual(item["userId"], 1) + @vcr.use_cassette("fixtures/main_non_json_response.yaml") def test_non_json_response(self): test_input_json = """{ "method": "GET",