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

Commit

Permalink
Log test requests and responses
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Apr 21, 2015
1 parent 2f166e2 commit cc598d0
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions relengapi/lib/testing/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import inspect
import logging
import relengapi.app
import wrapt

from flask import json
from relengapi.lib import auth

log = logging.getLogger(__name__)


class TestContext(object):

Expand Down Expand Up @@ -76,22 +79,40 @@ def set_user():
self.options['app_setup'](app)
return app

def _wrap_client(self, client):
# create a post_json convenience method
def post_json(path, data):
return client.post(
path, data=json.dumps(data),
headers=[('Content-Type', 'application/json')])
client.post_json = post_json

# patch 'open' to log the request
old_open = client.open

def open(*args, **kwargs):
path = args[0]
method = kwargs.get('method')
log.info('request: {} {}'.format(method, path))
resp = old_open(*args, **kwargs)
log.info('response: {}'.format(resp.status))
return resp
client.open = open

@wrapt.decorator
def __call__(self, wrapped, instance, given_args, kwargs):
arginfo = inspect.getargspec(wrapped)
args = set((arginfo.args if arginfo.args else []) +
(arginfo.keywords if arginfo.keywords else []))

def post_json(path, data):
return kwargs['client'].post(
path, data=json.dumps(data),
headers=[('Content-Type', 'application/json')])
app = self._make_app()
if 'app' in args:
kwargs['app'] = app

if 'client' in args:
kwargs['client'] = app.test_client()
kwargs['client'].post_json = post_json
self._wrap_client(kwargs['client'])

if 'db_setup' in self.options:
self.options['db_setup'](app)
try:
Expand Down

0 comments on commit cc598d0

Please sign in to comment.