Skip to content

Commit

Permalink
Merge pull request #25 from Overseas-Student-Living/better-response-h…
Browse files Browse the repository at this point in the history
…andling

Handling non werkzeug responses. Sensitive arguments compatibility wi…
  • Loading branch information
kooba committed Oct 27, 2017
2 parents 9d095a0 + 747b568 commit fe40e53
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
20 changes: 20 additions & 0 deletions nameko_tracer/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from nameko.exceptions import get_module_path
from nameko.utils import get_redacted_args
import six
from werkzeug.wrappers import Response

from nameko_tracer import constants, utils

Expand Down Expand Up @@ -81,6 +82,10 @@ def get_call_args(self, worker_ctx):
entrypoint = worker_ctx.entrypoint

if getattr(entrypoint, 'sensitive_variables', None):
# backwards compatibility with nameko < 2.7.0
entrypoint.sensitive_arguments = entrypoint.sensitive_variables

if getattr(entrypoint, 'sensitive_arguments', None):
call_args = get_redacted_args(
entrypoint, *worker_ctx.args, **worker_ctx.kwargs)
redacted = True
Expand Down Expand Up @@ -154,6 +159,21 @@ def get_call_args(self, worker_ctx):
def get_result(self, result):
""" Transform response object to serialisable dictionary
"""
if not isinstance(result, Response):
if isinstance(result, tuple):
if len(result) == 3:
status, _, payload = result
else:
status, payload = result
else:
payload = result
status = 200

result = Response(
payload,
status=status,
)

return {
'content_type': result.content_type,
'data': result.get_data(),
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='nameko-tracer',
version='1.0.6',
version='1.0.7',
description='Nameko extension logging entrypoint processing metrics',
author='student.com',
author_email='[email protected]',
Expand Down
50 changes: 32 additions & 18 deletions tests/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,18 @@ def test_call_args_data(
assert data['call_args'] == {'spam': 'some-arg'}
assert data['call_args_redacted'] is False

@pytest.fixture(params=['sensitive_variables', 'sensitive_arguments'])
def compatibility_shim(self, request):
return request.param

@pytest.mark.parametrize(
'stage',
(constants.Stage.request, constants.Stage.response),
)
def test_sensitive_call_args_data(
self, adapter, tracker, worker_ctx, stage
self, adapter, tracker, worker_ctx, stage, compatibility_shim
):

worker_ctx.entrypoint.sensitive_variables = ('spam')
setattr(worker_ctx.entrypoint, compatibility_shim, ('spam'))

extra = {
'stage': stage,
Expand Down Expand Up @@ -553,34 +556,45 @@ def test_can_get_request_data(
assert call_args['request']['headers']['content_type'] == content_type

@pytest.mark.parametrize(
('data', 'status_code', 'content_type'),
('response', 'data', 'status_code', 'content_type'),
(
(
(Response(
'{"value": 1}',
200,
'application/json',
status=200,
mimetype='application/json',
), '{"value": 1}', 200, 'application/json',
),
(
(Response(
'foo',
202,
'text/plain',
status=202,
mimetype='text/plain',
), 'foo', 202, 'text/plain',
),
(
(Response(
'some error',
400,
'text/plain',
status=400,
mimetype='text/plain',
), 'some error', 400, 'text/plain',
),
(
(200, {}, 'foo'),
'foo', 200, 'text/plain',
),
(
(200, 'foo'),
'foo', 200, 'text/plain',
),
(
'foo',
'foo', 200, 'text/plain',
)
)
)
def test_result_data(
self, adapter, data, status_code, content_type
self, adapter, response, data, status_code, content_type
):

response = Response(
data, status=status_code, mimetype=content_type)

result = adapter.get_result(response)

assert result['data'] == data.encode('utf-8')
assert result['status_code'] == status_code
assert result['content_type'].startswith(content_type)

0 comments on commit fe40e53

Please sign in to comment.