From 0118a6c70ce69095364700a46076625e451be3fe Mon Sep 17 00:00:00 2001 From: Erick Daniszewski Date: Mon, 9 Aug 2021 14:52:46 -0400 Subject: [PATCH] test: add regression tests for [VIO-1278] --- tests/unit/api/test_http.py | 45 +++++++++++++++++++++++++++++++++++++ tests/unit/test_utils.py | 5 +++++ 2 files changed, 50 insertions(+) diff --git a/tests/unit/api/test_http.py b/tests/unit/api/test_http.py index a6ab4640..099a8d1a 100644 --- a/tests/unit/api/test_http.py +++ b/tests/unit/api/test_http.py @@ -1111,6 +1111,51 @@ async def mock_read_cache(*args, **kwargs): mock_cmd.assert_called_once() mock_cmd.assert_called_with('', '') + def test_ok_with_bytes(self, synse_app): + """Ensure that streaming responses works when values are provided as bytes instead + of as strings. + + Regression test for: https://vaporio.atlassian.net/browse/VIO-1278 + """ + + # Need to define a side-effect function for the test rather than utilizing + # asynctest's implicit behavior for iterable side_effects because the function + # we are mocking (cmd.read_cache) is an async generator, and the implicit + # handling via asynctest does not appear to to handle that case well. + async def mock_read_cache(*args, **kwargs): + values = [ + { + 'value': 1, + 'type': b'temperature', + }, + { + b'value': 2, + 'type': 'temperature', + }, + { + b'value': 3, + b'type': b'temperature', + }, + ] + + for v in values: + yield v + + with asynctest.patch('synse_server.api.http.cmd.read_cache') as mock_cmd: + mock_cmd.side_effect = mock_read_cache + + _, resp = synse_app.test_client.get('/v3/readcache', gather_request=False) + assert resp.status == 200 + assert resp.headers['Transfer-Encoding'] == 'chunked' + assert resp.headers['Content-Type'] == 'application/json; charset=utf-8' + + # The response is streamed, so we cannot simply load it (it will not be + # a valid single JSON document), so we compare just the body. + assert resp.body == b'{"value":1,"type":"temperature"}\n{"value":2,"type":"temperature"}\n{"value":3,"type":"temperature"}\n' # noqa: E501 + + mock_cmd.assert_called_once() + mock_cmd.assert_called_with('', '') + def test_error(self, synse_app): # Need to define a side-effect function for the test rather than utilizing # asynctest's implicit behavior for iterable side_effects because the function diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index bbcd915f..99bcb995 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -45,6 +45,11 @@ def test_rfc3339now(): ([1, 2, 3], '[1,2,3]\n'), ([1, 2, [2, 3, 4]], '[1,2,[2,3,4]]\n'), ([{'one': 1}, {'two': 2}], '[{"one":1},{"two":2}]\n'), + + # -- Regression tests for https://vaporio.atlassian.net/browse/VIO-1278 + ({b'foo': 'bar'}, '{"foo":"bar"}\n'), + ({'foo': b'bar'}, '{"foo":"bar"}\n'), + ({b'foo': b'bar'}, '{"foo":"bar"}\n'), ], ) def test_dumps(data, expected):