Skip to content

Commit

Permalink
Merge pull request #69 from freedomofpress/use_proper_timeout
Browse files Browse the repository at this point in the history
Uses incoming timeout value from JSON
  • Loading branch information
sssoleileraaa authored Apr 2, 2020
2 parents 714159d + 4bfc44e commit d78144a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions securedrop_proxy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
42 changes: 42 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d78144a

Please sign in to comment.