Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #105 from mfelsche/m/traffic0
Browse files Browse the repository at this point in the history
fix 400 errors on lizzy traffic
  • Loading branch information
jmcs committed Apr 20, 2016
2 parents 5932dec + b3e2082 commit 08f7898
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lizzy/apps/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def __init__(self, application: str,
self.extra_parameters = extra_parameters or [] # type: Iterable[str]

def _execute(self, subcommand: str, *args: Iterable[str],
expect_json: bool=False):
expect_json: bool=False,
accept_empty: bool=True):
command = [self.application, subcommand]
command.extend(self.extra_parameters)
if expect_json:
Expand All @@ -30,11 +31,13 @@ def _execute(self, subcommand: str, *args: Iterable[str],
stdout, _ = process.communicate()
output = stdout.decode()
if process.returncode == 0:
if expect_json:
if expect_json and (output or not accept_empty):
try:
return json.loads(output)
except ValueError:
raise ExecutionError('JSON ERROR', output)
elif not output and not accept_empty:
raise ExecutionError('EMPTY OUTPUT', output)
else:
return output
else:
Expand Down
70 changes: 70 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import pytest
from unittest.mock import MagicMock
import json

from lizzy.apps.common import Application
from lizzy.exceptions import ExecutionError


@pytest.fixture
def popen(monkeypatch):
mock_popen = MagicMock()
mock_popen.return_value = mock_popen
mock_popen.returncode = 0
monkeypatch.setattr('lizzy.apps.common.Popen', mock_popen)
return mock_popen


def test_json_output(monkeypatch, popen):
expected = {"foo": "bar"}
popen.communicate.return_value = json.dumps(expected).encode(), b'stderr'

app = Application("foo")
output = app._execute("bar", "a", "b", "c", expect_json=True, accept_empty=True)
assert output == expected

output = app._execute("bar", "a", "b", "c", expect_json=False, accept_empty=True)
assert output == json.dumps(expected)

output = app._execute("bar", "a", "b", "c", expect_json=True, accept_empty=False)
assert output == expected

output = app._execute("bar", "a", "b", "c", expect_json=False, accept_empty=False)
assert output == json.dumps(expected)


def test_empty_output(monkeypatch, popen):
expected = ""
popen.communicate.return_value = expected.encode(), b'stderr'

app = Application("foo")

output = app._execute("bar", "a", "b", "c", expect_json=False, accept_empty=True)
assert output == expected

output = app._execute("bar", "a", "b", "c", expect_json=True, accept_empty=True)
assert output == expected

with pytest.raises(ExecutionError):
app._execute("bar", "a", "b", "c", expect_json=True, accept_empty=False)

with pytest.raises(ExecutionError):
app._execute("bar", "a", "b", "c", expect_json=False, accept_empty=False)


def test_invalid_json_output(monkeypatch, popen):
expected = "[[}"
popen.communicate.return_value = expected.encode(), b'stderr'

app = Application("foo")
output = app._execute("bar", "a", "b", "c", expect_json=False, accept_empty=True)
assert output == expected

output = app._execute("bar", "a", "b", "c", expect_json=False, accept_empty=False)
assert output == expected

with pytest.raises(ExecutionError):
app._execute("bar", "a", "b", "c", expect_json=True, accept_empty=False)

with pytest.raises(ExecutionError):
app._execute("bar", "a", "b", "c", expect_json=True, accept_empty=True)

0 comments on commit 08f7898

Please sign in to comment.